Skip to content

Commit 2007b77

Browse files
committed
feat: add solutions to lc problems: No.2404~2407
* No.2404.Most Frequent Even Element * No.2405.Optimal Partition of String * No.2406.Divide Intervals Into Minimum Number of Groups * No.2407.Longest Increasing Subsequence II
1 parent 105a4af commit 2007b77

File tree

46 files changed

+2304
-25
lines changed

Some content is hidden

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

46 files changed

+2304
-25
lines changed

lcci/03.03.Stack of Plates/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</pre>
2424

2525
## 解法
26+
2627
<!-- 这里可写通用的实现逻辑 -->
2728

2829
<!-- tabs:start -->
@@ -34,6 +35,7 @@
3435
```python
3536

3637
```
38+
3739
### **Java**
3840

3941
<!-- 这里可写当前语言的特殊实现逻辑 -->

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/0400-0499/0484.Find Permutation/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ def findPermutation(self, s: str) -> List[int]:
77
j = i
88
while j < n and s[j] == 'D':
99
j += 1
10-
ans[i: j + 1] = ans[i: j + 1][::-1]
10+
ans[i : j + 1] = ans[i : j + 1][::-1]
1111
i = max(i + 1, j)
1212
return ans

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/0500-0599/0576.Out of Boundary Paths/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
2+
def findPaths(
3+
self, m: int, n: int, maxMove: int, startRow: int, startColumn: int
4+
) -> int:
35
@cache
46
def dfs(i, j, k):
57
if i < 0 or j < 0 or i >= m or j >= n:

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/0700-0799/0776.Split BST/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def splitBST(self, root: Optional[TreeNode], target: int) -> List[Optional[TreeNode]]:
8+
def splitBST(
9+
self, root: Optional[TreeNode], target: int
10+
) -> List[Optional[TreeNode]]:
911
def dfs(root):
1012
if root is None:
1113
return [None, None]

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/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def mincostToHireWorkers(self, quality: List[int], wage: List[int], k: int) -> float:
2+
def mincostToHireWorkers(
3+
self, quality: List[int], wage: List[int], k: int
4+
) -> float:
35
t = sorted(zip(quality, wage), key=lambda x: x[1] / x[0])
46
ans, tot = inf, 0
57
h = []

solution/1000-1099/1032.Stream of Characters/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def search(self, w):
2525

2626

2727
class StreamChecker:
28-
2928
def __init__(self, words: List[str]):
3029
self.trie = Trie()
3130
self.s = []
@@ -36,6 +35,7 @@ def query(self, letter: str) -> bool:
3635
self.s.append(letter)
3736
return self.trie.search(self.s[-201:])
3837

38+
3939
# Your StreamChecker object will be instantiated and called as such:
4040
# obj = StreamChecker(words)
4141
# param_1 = obj.query(letter)

solution/1500-1599/1593.Split a String Into the Max Number of Unique Substrings/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ def dfs(i, t):
66
ans = max(ans, t)
77
return
88
for j in range(i + 1, len(s) + 1):
9-
if s[i: j] not in vis:
10-
vis.add(s[i: j])
9+
if s[i:j] not in vis:
10+
vis.add(s[i:j])
1111
dfs(j, t + 1)
12-
vis.remove(s[i: j])
12+
vis.remove(s[i:j])
1313

1414
vis = set()
1515
ans = 1

solution/1600-1699/1600.Throne Inheritance/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ThroneInheritance:
2-
32
def __init__(self, kingName: str):
43
self.g = defaultdict(list)
54
self.dead = set()
@@ -22,6 +21,7 @@ def dfs(x):
2221
dfs(self.king)
2322
return ans
2423

24+
2525
# Your ThroneInheritance object will be instantiated and called as such:
2626
# obj = ThroneInheritance(kingName)
2727
# obj.birth(parentName,childName)

solution/1600-1699/1603.Design Parking System/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ParkingSystem:
2-
32
def __init__(self, big: int, medium: int, small: int):
43
self.cnt = [0, big, medium, small]
54

solution/2400-2499/2403.Minimum Time to Kill All Monsters/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ func min(a, b int64) int64 {
297297
}
298298
```
299299

300-
301300
### **TypeScript**
302301

303302
```ts

solution/2400-2499/2403.Minimum Time to Kill All Monsters/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public:
177177
n = power.size();
178178
f.assign(1 << n, -1);
179179
this->power = power;
180-
return dfs(0);
180+
return dfs(0);
181181
}
182182

183183
ll dfs(int mask) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# [2404. 出现最频繁的偶数元素](https://leetcode.cn/problems/most-frequent-even-element)
2+
3+
[English Version](/solution/2400-2499/2404.Most%20Frequent%20Even%20Element/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组 <code>nums</code> ,返回出现最频繁的偶数元素。</p>
10+
11+
<p>如果存在多个满足条件的元素,只需要返回 <strong>最小</strong> 的一个。如果不存在这样的元素,返回 <code>-1</code> 。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><strong>输入:</strong>nums = [0,1,2,2,4,4,1]
18+
<strong>输出:</strong>2
19+
<strong>解释:</strong>
20+
数组中的偶数元素为 0、2 和 4 ,在这些元素中,2 和 4 出现次数最多。
21+
返回最小的那个,即返回 2 。</pre>
22+
23+
<p><strong>示例 2:</strong></p>
24+
25+
<pre><strong>输入:</strong>nums = [4,4,4,9,2,4]
26+
<strong>输出:</strong>4
27+
<strong>解释:</strong>4 是出现最频繁的偶数元素。
28+
</pre>
29+
30+
<p><strong>示例 3:</strong></p>
31+
32+
<pre><strong>输入:</strong>nums = [29,47,21,41,13,37,25,7]
33+
<strong>输出:</strong>-1
34+
<strong>解释:</strong>不存在偶数元素。
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= nums.length &lt;= 2000</code></li>
43+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
44+
</ul>
45+
46+
## 解法
47+
48+
<!-- 这里可写通用的实现逻辑 -->
49+
50+
**方法一:哈希表**
51+
52+
用哈希表统计所有偶数元素出现的次数,然后找出出现次数最多且值最小的偶数元素。
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
class Solution:
64+
def mostFrequentEven(self, nums: List[int]) -> int:
65+
cnt = Counter(v for v in nums if v % 2 == 0)
66+
ans, mx = -1, 0
67+
for v, t in cnt.items():
68+
if mx < t or (mx == t and ans > v):
69+
mx = t
70+
ans = v
71+
return ans
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
public int mostFrequentEven(int[] nums) {
81+
Map<Integer, Integer> cnt = new HashMap<>();
82+
for (int v : nums) {
83+
if (v % 2 == 0) {
84+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
85+
}
86+
}
87+
int ans = -1, mx = 0;
88+
for (var e : cnt.entrySet()) {
89+
int v = e.getKey(), t = e.getValue();
90+
if (mx < t || (mx == t && ans > v)) {
91+
mx = t;
92+
ans = v;
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int mostFrequentEven(vector<int>& nums) {
106+
unordered_map<int, int> cnt;
107+
for (int v : nums) {
108+
if (v % 2 == 0) {
109+
++cnt[v];
110+
}
111+
}
112+
int ans = -1, mx = 0;
113+
for (auto [v, t] : cnt) {
114+
if (mx < t || (mx == t && ans > v)) {
115+
mx = t;
116+
ans = v;
117+
}
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func mostFrequentEven(nums []int) int {
128+
cnt := map[int]int{}
129+
for _, v := range nums {
130+
if v%2 == 0 {
131+
cnt[v]++
132+
}
133+
}
134+
ans, mx := -1, 0
135+
for v, t := range cnt {
136+
if mx < t || (mx == t && ans > v) {
137+
mx = t
138+
ans = v
139+
}
140+
}
141+
return ans
142+
}
143+
```
144+
145+
### **TypeScript**
146+
147+
```ts
148+
149+
```
150+
151+
### **...**
152+
153+
```
154+
155+
156+
```
157+
158+
<!-- tabs:end -->

0 commit comments

Comments
 (0)