Skip to content

Commit 0c77a7b

Browse files
committed
feat: update solutions to lc problems: No.2580,2581
* No.2580.Count Ways to Group Overlapping Ranges * No.2581.Count Number of Possible Root Nodes
1 parent c75df9f commit 0c77a7b

File tree

17 files changed

+315
-77
lines changed

17 files changed

+315
-77
lines changed

solution/0700-0799/0716.Max Stack/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def peek(self):
3737

3838

3939
class MaxStack:
40-
4140
def __init__(self):
4241
self.stk = DoubleLinkedList()
4342
self.sl = SortedList(key=lambda x: x.val)
@@ -62,6 +61,7 @@ def popMax(self) -> int:
6261
DoubleLinkedList.remove(node)
6362
return node.val
6463

64+
6565
# Your MaxStack object will be instantiated and called as such:
6666
# obj = MaxStack()
6767
# obj.push(x)

solution/0900-0999/0980.Unique Paths III/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def dfs(i, j, k):
1313
return ans
1414

1515
m, n = len(grid), len(grid[0])
16-
start = next((i, j) for i in range(m)
17-
for j in range(n) if grid[i][j] == 1)
16+
start = next((i, j) for i in range(m) for j in range(n) if grid[i][j] == 1)
1817
dirs = (-1, 0, 1, 0, -1)
1918
cnt = sum(grid[i][j] == 0 for i in range(m) for j in range(n))
2019
vis = {start}

solution/0900-0999/0981.Time Based Key-Value Store/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class TimeMap:
2-
32
def __init__(self):
43
self.ktv = defaultdict(list)
54

solution/1100-1199/1134.Armstrong Number/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ def isArmstrong(self, n: int) -> bool:
33
k = len(str(n))
44
s, x = 0, n
55
while x:
6-
s += (x % 10)**k
6+
s += (x % 10) ** k
77
x //= 10
88
return s == n

solution/1500-1599/1518.Water Bottles/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ class Solution:
22
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
33
ans = numBottles
44
while numBottles >= numExchange:
5-
numBottles -= (numExchange - 1)
5+
numBottles -= numExchange - 1
66
ans += 1
77
return ans

solution/1500-1599/1573.Number of Ways to Split a String/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def find(x):
66
t += int(c == '1')
77
if t == x:
88
return i
9+
910
cnt, m = divmod(sum(c == '1' for c in s), 3)
1011
if m:
1112
return 0

solution/1800-1899/1845.Seat Reservation Manager/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class SeatManager:
2-
32
def __init__(self, n: int):
43
self.q = list(range(1, n + 1))
54
heapify(self.q)

solution/2500-2599/2578.Split With Minimum Sum/README.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
<ul>
1212
<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;直接连起来,得到&nbsp;<code>num</code>&nbsp;各数位的一个排列。
13-
1413
<ul>
1514
<li>换句话说,<code>num1</code> 和&nbsp;<code>num2</code>&nbsp;中所有数字出现的次数之和等于&nbsp;<code>num</code>&nbsp;中所有数字出现的次数。</li>
1615
</ul>
1716
</li>
1817
<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;可以包含前导 0 。</li>
19-
2018
</ul>
2119

2220
<p>请你返回&nbsp;<code>num1</code> 和 <code>num2</code>&nbsp;可以得到的和的 <strong>最小</strong> 值。</p>
@@ -66,6 +64,12 @@
6664

6765
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为 $num$ 的位数;而 $C$ 为 $num$ 中不同数字的个数,本题中 $C \leq 10$。
6866

67+
**方法二:排序 + 贪心**
68+
69+
我们可以将 $num$ 转换成字符串或者字符数组,然后对其进行排序,接下来将排序后的数组中的数字按照从小到大的顺序交替地分配给 $num1$ 和 $num2$,最后返回 $num1$ 和 $num2$ 的和即可。
70+
71+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为 $num$ 的位数。
72+
6973
<!-- tabs:start -->
7074

7175
### **Python3**
@@ -91,6 +95,13 @@ class Solution:
9195
return sum(ans)
9296
```
9397

98+
```python
99+
class Solution:
100+
def splitNum(self, num: int) -> int:
101+
s = sorted(str(num))
102+
return int(''.join(s[::2])) + int(''.join(s[1::2]))
103+
```
104+
94105
### **Java**
95106

96107
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -117,6 +128,20 @@ class Solution {
117128
}
118129
```
119130

131+
```java
132+
class Solution {
133+
public int splitNum(int num) {
134+
char[] s = (num + "").toCharArray();
135+
Arrays.sort(s);
136+
int[] ans = new int[2];
137+
for (int i = 0; i < s.length; ++i) {
138+
ans[i & 1] = ans[i & 1] * 10 + s[i] - '0';
139+
}
140+
return ans[0] + ans[1];
141+
}
142+
}
143+
```
144+
120145
### **C++**
121146

122147
```cpp
@@ -142,6 +167,21 @@ public:
142167
};
143168
```
144169
170+
```cpp
171+
class Solution {
172+
public:
173+
int splitNum(int num) {
174+
string s = to_string(num);
175+
sort(s.begin(), s.end());
176+
int ans[2]{};
177+
for (int i = 0; i < s.size(); ++i) {
178+
ans[i & 1] = ans[i & 1] * 10 + s[i] - '0';
179+
}
180+
return ans[0] + ans[1];
181+
}
182+
};
183+
```
184+
145185
### **Go**
146186

147187
```go
@@ -164,6 +204,18 @@ func splitNum(num int) int {
164204
}
165205
```
166206

207+
```go
208+
func splitNum(num int) int {
209+
s := []byte(strconv.Itoa(num))
210+
sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
211+
ans := [2]int{}
212+
for i, c := range s {
213+
ans[i&1] = ans[i&1]*10 + int(c-'0')
214+
}
215+
return ans[0] + ans[1]
216+
}
217+
```
218+
167219
### **...**
168220

169221
```

solution/2500-2599/2578.Split With Minimum Sum/README_EN.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
<ul>
1010
<li>The concatenation of <code>num1</code> and <code>num2</code> is a permutation of <code>num</code>.
11-
1211
<ul>
1312
<li>In other words, the sum of the number of occurrences of each digit in <code>num1</code> and <code>num2</code> is equal to the number of occurrences of that digit in <code>num</code>.</li>
1413
</ul>
1514
</li>
1615
<li><code>num1</code> and <code>num2</code> can contain leading zeros.</li>
17-
1816
</ul>
1917

2018
<p>Return <em>the <strong>minimum</strong> possible sum of</em> <code>num1</code> <em>and</em> <code>num2</code>.</p>
@@ -75,6 +73,13 @@ class Solution:
7573
return sum(ans)
7674
```
7775

76+
```python
77+
class Solution:
78+
def splitNum(self, num: int) -> int:
79+
s = sorted(str(num))
80+
return int(''.join(s[::2])) + int(''.join(s[1::2]))
81+
```
82+
7883
### **Java**
7984

8085
```java
@@ -99,6 +104,20 @@ class Solution {
99104
}
100105
```
101106

107+
```java
108+
class Solution {
109+
public int splitNum(int num) {
110+
char[] s = (num + "").toCharArray();
111+
Arrays.sort(s);
112+
int[] ans = new int[2];
113+
for (int i = 0; i < s.length; ++i) {
114+
ans[i & 1] = ans[i & 1] * 10 + s[i] - '0';
115+
}
116+
return ans[0] + ans[1];
117+
}
118+
}
119+
```
120+
102121
### **C++**
103122

104123
```cpp
@@ -124,6 +143,21 @@ public:
124143
};
125144
```
126145
146+
```cpp
147+
class Solution {
148+
public:
149+
int splitNum(int num) {
150+
string s = to_string(num);
151+
sort(s.begin(), s.end());
152+
int ans[2]{};
153+
for (int i = 0; i < s.size(); ++i) {
154+
ans[i & 1] = ans[i & 1] * 10 + s[i] - '0';
155+
}
156+
return ans[0] + ans[1];
157+
}
158+
};
159+
```
160+
127161
### **Go**
128162

129163
```go
@@ -146,6 +180,18 @@ func splitNum(num int) int {
146180
}
147181
```
148182

183+
```go
184+
func splitNum(num int) int {
185+
s := []byte(strconv.Itoa(num))
186+
sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
187+
ans := [2]int{}
188+
for i, c := range s {
189+
ans[i&1] = ans[i&1]*10 + int(c-'0')
190+
}
191+
return ans[0] + ans[1]
192+
}
193+
```
194+
149195
### **...**
150196

151197
```

solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为区间个数。
7474

75+
我们也可以不使用快速幂,一旦发现有新的不相交的区间,就将方案数乘 $2$ 后对 $10^9 + 7$ 取模。
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**
@@ -91,6 +93,20 @@ class Solution:
9193
return pow(2, cnt, mod)
9294
```
9395

96+
```python
97+
class Solution:
98+
def countWays(self, ranges: List[List[int]]) -> int:
99+
ranges.sort()
100+
mx = -1
101+
mod = 10**9 + 7
102+
ans = 1
103+
for start, end in ranges:
104+
if start > mx:
105+
ans = ans * 2 % mod
106+
mx = max(mx, end)
107+
return ans
108+
```
109+
94110
### **Java**
95111

96112
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -123,6 +139,24 @@ class Solution {
123139
}
124140
```
125141

142+
```java
143+
class Solution {
144+
public int countWays(int[][] ranges) {
145+
Arrays.sort(ranges, (a, b) -> a[0] - b[0]);
146+
int mx = -1;
147+
int ans = 1;
148+
final int mod = (int) 1e9 + 7;
149+
for (int[] e : ranges) {
150+
if (e[0] > mx) {
151+
ans = ans * 2 % mod;
152+
}
153+
mx = Math.max(mx, e[1]);
154+
}
155+
return ans;
156+
}
157+
}
158+
```
159+
126160
### **C++**
127161

128162
```cpp
@@ -152,6 +186,24 @@ public:
152186
};
153187
```
154188

189+
```cpp
190+
class Solution {
191+
public:
192+
int countWays(vector<vector<int>>& ranges) {
193+
sort(ranges.begin(), ranges.end());
194+
int ans = 1, mx = -1;
195+
const int mod = 1e9 + 7;
196+
for (auto& e : ranges) {
197+
if (e[0] > mx) {
198+
ans = ans * 2 % mod;
199+
}
200+
mx = max(mx, e[1]);
201+
}
202+
return ans;
203+
}
204+
};
205+
```
206+
155207
### **Go**
156208
157209
```go
@@ -182,6 +234,23 @@ func qmi(a, k, p int) int {
182234
}
183235
```
184236

237+
```go
238+
func countWays(ranges [][]int) int {
239+
sort.Slice(ranges, func(i, j int) bool { return ranges[i][0] < ranges[j][0] })
240+
ans, mx := 1, -1
241+
const mod = 1e9 + 7
242+
for _, e := range ranges {
243+
if e[0] > mx {
244+
ans = ans * 2 % mod
245+
}
246+
if mx < e[1] {
247+
mx = e[1]
248+
}
249+
}
250+
return ans
251+
}
252+
```
253+
185254
### **...**
186255

187256
```

0 commit comments

Comments
 (0)