Skip to content

Commit 42808f4

Browse files
committed
feat: add solutions to lc problems: No.2409~2416
* No.2409.Count Days Spent Together * No.2410.Maximum Matching of Players With Trainers * No.2411.Smallest Subarrays With Maximum Bitwise OR * No.2412.Minimum Money Required Before Transactions * No.2413.Smallest Even Multiple * No.2414.Length of the Longest Alphabetical Continuous Substring * No.2415.Reverse Odd Levels of Binary Tree * No.2416.Sum of Prefix Scores of Strings
1 parent 0380cac commit 42808f4

File tree

72 files changed

+3627
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3627
-34
lines changed

solution/0300-0399/0336.Palindrome Pairs/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
<strong>输出:</strong>[[0,1],[1,0]]
3333
</pre>
3434

35-
36-
3735
<p><strong>提示:</strong></p>
3836

3937
<ul>

solution/0500-0599/0554.Brick Wall/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<strong>输出:</strong>3
2929
</pre>
3030

31-
32-
3331
<p><strong>提示:</strong></p>
3432

3533
<ul>

solution/0700-0799/0775.Global and Local Inversions/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
<strong>解释:</strong>有 2 个全局倒置,和 1 个局部倒置。
4343
</pre>
4444

45-
46-
4745
<p><strong>提示:</strong></p>
4846

4947
<ul>

solution/0800-0899/0830.Positions of Large Groups/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
<strong>输出:</strong>[]
4949
</pre>
5050

51-
52-
5351
<p><strong>提示:</strong></p>
5452

5553
<ul>

solution/1400-1499/1460.Make Two Arrays Equal by Reversing Sub-arrays/README.md solution/1400-1499/1460.Make Two Arrays Equal by Reversing Subarrays/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [1460. 通过翻转子数组使两个数组相等](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays)
1+
# [1460. 通过翻转子数组使两个数组相等](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-subarrays)
22

3-
[English Version](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Sub-arrays/README_EN.md)
3+
[English Version](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/README_EN.md)
44

55
## 题目描述
66

solution/1400-1499/1460.Make Two Arrays Equal by Reversing Sub-arrays/README_EN.md solution/1400-1499/1460.Make Two Arrays Equal by Reversing Subarrays/README_EN.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# [1460. Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays)
1+
# [1460. Make Two Arrays Equal by Reversing Subarrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays)
22

3-
[中文文档](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Sub-arrays/README.md)
3+
[中文文档](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/README.md)
44

55
## Description
66

7-
<p>You are given two integer arrays of equal length <code>target</code> and <code>arr</code>. In one step, you can select any <strong>non-empty sub-array</strong> of <code>arr</code> and reverse it. You are allowed to make any number of steps.</p>
7+
<p>You are given two integer arrays of equal length <code>target</code> and <code>arr</code>. In one step, you can select any <strong>non-empty subarray</strong> of <code>arr</code> and reverse it. You are allowed to make any number of steps.</p>
88

99
<p>Return <code>true</code> <em>if you can make </em><code>arr</code><em> equal to </em><code>target</code><em>&nbsp;or </em><code>false</code><em> otherwise</em>.</p>
1010

@@ -15,9 +15,9 @@
1515
<strong>Input:</strong> target = [1,2,3,4], arr = [2,4,1,3]
1616
<strong>Output:</strong> true
1717
<strong>Explanation:</strong> You can follow the next steps to convert arr to target:
18-
1- Reverse sub-array [2,4,1], arr becomes [1,4,2,3]
19-
2- Reverse sub-array [4,2], arr becomes [1,2,4,3]
20-
3- Reverse sub-array [4,3], arr becomes [1,2,3,4]
18+
1- Reverse subarray [2,4,1], arr becomes [1,4,2,3]
19+
2- Reverse subarray [4,2], arr becomes [1,2,4,3]
20+
3- Reverse subarray [4,3], arr becomes [1,2,3,4]
2121
There are multiple ways to convert arr to target, this is not the only way to do so.
2222
</pre>
2323

solution/1600-1699/1618.Maximum Font to Fit a Sentence in a Screen/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class Solution {
186186
* public:
187187
* // Return the width of char ch when fontSize is used.
188188
* int getWidth(int fontSize, char ch);
189-
*
189+
*
190190
* // Return Height of any char when fontSize is used.
191191
* int getHeight(int fontSize)
192192
* };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# [2409. 统计共同度过的日子数](https://leetcode.cn/problems/count-days-spent-together)
2+
3+
[English Version](/solution/2400-2499/2409.Count%20Days%20Spent%20Together/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Alice 和 Bob 计划分别去罗马开会。</p>
10+
11+
<p>给你四个字符串&nbsp;<code>arriveAlice</code>&nbsp;,<code>leaveAlice</code>&nbsp;,<code>arriveBob</code>&nbsp;&nbsp;<code>leaveBob</code>&nbsp;。Alice 会在日期&nbsp;<code>arriveAlice</code>&nbsp;&nbsp;<code>leaveAlice</code>&nbsp;之间在城市里(<strong>日期为闭区间</strong>),而 Bob 在日期&nbsp;<code>arriveBob</code>&nbsp;&nbsp;<code>leaveBob</code>&nbsp;之间在城市里(<strong>日期为闭区间</strong>)。每个字符串都包含 5 个字符,格式为&nbsp;<code>"MM-DD"</code>&nbsp;,对应着一个日期的月和日。</p>
12+
13+
<p>请你返回 Alice和 Bob 同时在罗马的天数。</p>
14+
15+
<p>你可以假设所有日期都在 <strong>同一个</strong>&nbsp;自然年,而且 <strong>不是</strong>&nbsp;闰年。每个月份的天数分别为:<code>[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]</code>&nbsp;。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<b>输入:</b>arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
23+
<b>输出:</b>3
24+
<b>解释:</b>Alice 从 8 月 15 号到 8 月 18 号在罗马。Bob 从 8 月 16 号到 8 月 19 号在罗马,他们同时在罗马的日期为 8 月 16、17 和 18 号。所以答案为 3 。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre>
30+
<b>输入:</b>arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
31+
<b>输出:</b>0
32+
<b>解释:</b>Alice 和 Bob 没有同时在罗马的日子,所以我们返回 0 。
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong>提示:</strong></p>
38+
39+
<ul>
40+
<li>所有日期的格式均为&nbsp;<code>"MM-DD"</code>&nbsp;。</li>
41+
<li>Alice 和 Bob 的到达日期都 <strong>早于或等于</strong> 他们的离开日期。</li>
42+
<li>题目测试用例所给出的日期均为 <strong>非闰年</strong> 的有效日期。</li>
43+
</ul>
44+
45+
## 解法
46+
47+
<!-- 这里可写通用的实现逻辑 -->
48+
49+
**方法一:模拟**
50+
51+
将日期转换为天数,然后计算两个人在罗马的天数。
52+
53+
时间复杂度 $O(1)$。
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
class Solution:
63+
def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
64+
if leaveAlice < arriveBob or leaveBob < arriveAlice:
65+
return 0
66+
a = max(arriveAlice, arriveBob)
67+
b = min(leaveAlice, leaveBob)
68+
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
69+
x = sum(days[:int(a[:2]) - 1]) + int(a[3:])
70+
y = sum(days[:int(b[:2]) - 1]) + int(b[3:])
71+
return y - x + 1
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
private int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
81+
82+
public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
83+
String a = arriveAlice.compareTo(arriveBob) < 0 ? arriveBob : arriveAlice;
84+
String b = leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob;
85+
int x = f(a), y = f(b);
86+
return Math.max(y - x + 1, 0);
87+
}
88+
89+
private int f(String s) {
90+
int i = Integer.parseInt(s.substring(0, 2)) - 1;
91+
int res = 0;
92+
for (int j = 0; j < i; ++j) {
93+
res += days[j];
94+
}
95+
res += Integer.parseInt(s.substring(3));
96+
return res;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<int> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
107+
108+
int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
109+
string a = arriveAlice < arriveBob ? arriveBob : arriveAlice;
110+
string b = leaveAlice < leaveBob ? leaveAlice : leaveBob;
111+
int x = f(a), y = f(b);
112+
return max(0, y - x + 1);
113+
}
114+
115+
int f(string s) {
116+
int m, d;
117+
sscanf(s.c_str(), "%d-%d", &m, &d);
118+
int res = 0;
119+
for (int i = 0; i < m - 1; ++i) {
120+
res += days[i];
121+
}
122+
res += d;
123+
return res;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func countDaysTogether(arriveAlice string, leaveAlice string, arriveBob string, leaveBob string) int {
132+
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
133+
f := func(s string) int {
134+
m, _ := strconv.Atoi(s[:2])
135+
d, _ := strconv.Atoi(s[3:])
136+
res := 0
137+
for i := 0; i < m-1; i++ {
138+
res += days[i]
139+
}
140+
res += d
141+
return res
142+
}
143+
a, b := arriveAlice, leaveBob
144+
if arriveAlice < arriveBob {
145+
a = arriveBob
146+
}
147+
if leaveAlice < leaveBob {
148+
b = leaveAlice
149+
}
150+
x, y := f(a), f(b)
151+
ans := y - x + 1
152+
if ans < 0 {
153+
return 0
154+
}
155+
return ans
156+
}
157+
```
158+
159+
### **TypeScript**
160+
161+
```ts
162+
163+
```
164+
165+
### **...**
166+
167+
```
168+
169+
170+
```
171+
172+
<!-- tabs:end -->

0 commit comments

Comments
 (0)