Skip to content

Commit da940a9

Browse files
authored
feat: update lc problems (#3806)
1 parent b3fd3a0 commit da940a9

File tree

32 files changed

+538
-321
lines changed

32 files changed

+538
-321
lines changed

lcp/LCP 33. 蓄水/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ class Solution {
177177
if maxVat == 0 {
178178
return 0
179179
}
180-
180+
181181
let n = vat.count
182182
var ans = Int.max
183-
183+
184184
for x in 1...maxVat {
185185
var y = 0
186186
for i in 0..<n {
@@ -190,7 +190,7 @@ class Solution {
190190
}
191191
ans = min(ans, x + y)
192192
}
193-
193+
194194
return ans
195195
}
196196
}

lcp/LCP 34. 二叉树染色/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,22 @@ var maxValue = function (root, k) {
242242

243243
class Solution {
244244
private var k: Int = 0
245-
245+
246246
func maxValue(_ root: TreeNode?, _ k: Int) -> Int {
247247
self.k = k
248248
return dfs(root).max() ?? 0
249249
}
250-
250+
251251
private func dfs(_ root: TreeNode?) -> [Int] {
252252
var ans = [Int](repeating: 0, count: k + 1)
253253
guard let root = root else {
254254
return ans
255255
}
256256
let l = dfs(root.left)
257257
let r = dfs(root.right)
258-
258+
259259
ans[0] = (l.max() ?? 0) + (r.max() ?? 0)
260-
260+
261261
for i in 0..<k {
262262
for j in 0..<k - i {
263263
ans[i + j + 1] = max(ans[i + j + 1], root.val + l[i] + r[j])

lcp/LCP 41. 黑白翻转棋/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ class Solution {
299299
private var m = 0
300300
private var n = 0
301301
private var chessboard: [String] = []
302-
302+
303303
func flipChess(_ chessboard: [String]) -> Int {
304304
self.m = chessboard.count
305305
self.n = chessboard[0].count
306306
self.chessboard = chessboard
307307
var ans = 0
308-
308+
309309
for i in 0..<m {
310310
for j in 0..<n {
311311
if Array(chessboard[i])[j] == "." {
@@ -315,32 +315,32 @@ class Solution {
315315
}
316316
return ans
317317
}
318-
318+
319319
private func bfs(_ i: Int, _ j: Int) -> Int {
320320
var queue: [[Int]] = [[i, j]]
321321
var g = chessboard.map { Array($0) }
322322
g[i][j] = "X"
323323
var count = 0
324-
324+
325325
while !queue.isEmpty {
326326
let p = queue.removeFirst()
327327
let i = p[0], j = p[1]
328-
328+
329329
for a in -1...1 {
330330
for b in -1...1 {
331331
if a == 0 && b == 0 { continue }
332-
332+
333333
var x = i + a, y = j + b
334334
while x >= 0 && x < m && y >= 0 && y < n && g[x][y] == "O" {
335335
x += a
336336
y += b
337337
}
338-
338+
339339
if x >= 0 && x < m && y >= 0 && y < n && g[x][y] == "X" {
340340
x -= a
341341
y -= b
342342
count += max(abs(x - i), abs(y - j))
343-
343+
344344
while x != i || y != j {
345345
g[x][y] = "X"
346346
queue.append([x, y])

lcp/LCP 44. 开幕式焰火/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ func dfs(root *TreeNode) {
176176

177177
class Solution {
178178
private var uniqueColors: Set<Int> = []
179-
179+
180180
func numColor(_ root: TreeNode?) -> Int {
181181
dfs(root)
182182
return uniqueColors.count
183183
}
184-
184+
185185
private func dfs(_ node: TreeNode?) {
186186
guard let node = node else { return }
187187
uniqueColors.insert(node.val)

lcp/LCP 50. 宝石补给/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ function giveGem(gem: number[], operations: number[][]): number {
160160
class Solution {
161161
func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int {
162162
var gem = gem
163-
163+
164164
for op in operations {
165165
let x = op[0], y = op[1]
166166
let v = gem[x] / 2
167167
gem[y] += v
168168
gem[x] -= v
169169
}
170-
170+
171171
let maxGem = gem.max() ?? 0
172172
let minGem = gem.min() ?? 0
173173
return maxGem - minGem

solution/0200-0299/0289.Game of Life/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ tags:
2929
<li>如果死细胞周围正好有三个活细胞,则该位置死细胞复活;</li>
3030
</ol>
3131

32-
<p>下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 <code>m x n</code> 网格面板 <code>board</code> 的当前状态,返回下一个状态。</p>
32+
<p>下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是 <strong>同时</strong> 发生的。给你 <code>m x n</code> 网格面板 <code>board</code> 的当前状态,返回下一个状态。</p>
33+
34+
<p>给定当前&nbsp;<code>board</code>&nbsp;的状态,<strong>更新</strong>&nbsp;<code>board</code>&nbsp;到下一个状态。</p>
35+
36+
<p><strong>注意</strong> 你不需要返回任何东西。</p>
3337

3438
<p>&nbsp;</p>
3539

solution/0200-0299/0289.Game of Life/README_EN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p>
21+
<p>According to <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p>
2222

2323
<p>The board is made up of an <code>m x n</code> grid of cells, where each cell has an initial state: <b>live</b> (represented by a <code>1</code>) or <b>dead</b> (represented by a <code>0</code>). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):</p>
2424

@@ -29,7 +29,11 @@ tags:
2929
<li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li>
3030
</ol>
3131

32-
<p><span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the <code>m x n</code> grid <code>board</code>, return <em>the next state</em>.</span></p>
32+
<p><span>The next state of the board is determined by applying the above rules simultaneously to every cell in the current state of the <code>m x n</code> grid <code>board</code>. In this process, births and deaths occur <strong>simultaneously</strong>.</span></p>
33+
34+
<p><span>Given the current state of the <code>board</code>, <strong>update</strong> the <code>board</code> to reflect its next state.</span></p>
35+
36+
<p><strong>Note</strong> that you do not need to return anything.</p>
3337

3438
<p>&nbsp;</p>
3539
<p><strong class="example">Example 1:</strong></p>

solution/0300-0399/0308.Range Sum Query 2D - Mutable/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
comments: true
3-
difficulty: 困难
3+
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README.md
55
tags:
66
- 设计

solution/0300-0399/0308.Range Sum Query 2D - Mutable/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
comments: true
3-
difficulty: Hard
3+
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README_EN.md
55
tags:
66
- Design

solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/README.md

+69-80
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,28 @@ tags:
6060

6161
### 方法一:计数
6262

63+
我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 统计整数 $\textit{num}$ 中所有数字出现的次数,用一个下标数组 $\textit{idx}$ 记录当前最大可用偶数和奇数,初始时 $\textit{idx}$ 为 $[8, 9]$。
64+
65+
接下来,我们依次遍历整数 $\textit{num}$ 的每个数字,如果数字为奇数,则取 $\textit{idx}$ 下标为 $1$ 对应的数字,否则取下标为 $0$ 对应的数字。如果该数字出现的次数为 $0$,则需要将数字减 $2$,继续判断,直到存在满足条件的数。然后,我们更新答案,以及数字出现的次数,继续遍历,直到遍历到整数 $\textit{num}$。
66+
67+
时间复杂度 $O(\log \textit{num})$,空间复杂度 $O(\log \textit{num})$。
68+
6369
<!-- tabs:start -->
6470

6571
#### Python3
6672

6773
```python
6874
class Solution:
6975
def largestInteger(self, num: int) -> int:
70-
cnt = Counter()
71-
x = num
72-
while x:
73-
x, v = divmod(x, 10)
74-
cnt[v] += 1
75-
x = num
76+
nums = [int(c) for c in str(num)]
77+
cnt = Counter(nums)
78+
idx = [8, 9]
7679
ans = 0
77-
t = 1
78-
while x:
79-
x, v = divmod(x, 10)
80-
for y in range(10):
81-
if ((v ^ y) & 1) == 0 and cnt[y]:
82-
ans += y * t
83-
t *= 10
84-
cnt[y] -= 1
85-
break
80+
for x in nums:
81+
while cnt[idx[x & 1]] == 0:
82+
idx[x & 1] -= 2
83+
ans = ans * 10 + idx[x & 1]
84+
cnt[idx[x & 1]] -= 1
8685
return ans
8786
```
8887

@@ -91,26 +90,20 @@ class Solution:
9190
```java
9291
class Solution {
9392
public int largestInteger(int num) {
93+
char[] s = String.valueOf(num).toCharArray();
9494
int[] cnt = new int[10];
95-
int x = num;
96-
while (x != 0) {
97-
cnt[x % 10]++;
98-
x /= 10;
95+
for (char c : s) {
96+
++cnt[c - '0'];
9997
}
100-
x = num;
98+
int[] idx = {8, 9};
10199
int ans = 0;
102-
int t = 1;
103-
while (x != 0) {
104-
int v = x % 10;
105-
x /= 10;
106-
for (int y = 0; y < 10; ++y) {
107-
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
108-
cnt[y]--;
109-
ans += y * t;
110-
t *= 10;
111-
break;
112-
}
100+
for (char c : s) {
101+
int x = c - '0';
102+
while (cnt[idx[x & 1]] == 0) {
103+
idx[x & 1] -= 2;
113104
}
105+
ans = ans * 10 + idx[x & 1];
106+
cnt[idx[x & 1]]--;
114107
}
115108
return ans;
116109
}
@@ -123,26 +116,20 @@ class Solution {
123116
class Solution {
124117
public:
125118
int largestInteger(int num) {
126-
vector<int> cnt(10);
127-
int x = num;
128-
while (x) {
129-
cnt[x % 10]++;
130-
x /= 10;
119+
string s = to_string(num);
120+
int cnt[10] = {0};
121+
for (char c : s) {
122+
cnt[c - '0']++;
131123
}
132-
x = num;
124+
int idx[2] = {8, 9};
133125
int ans = 0;
134-
long t = 1;
135-
while (x) {
136-
int v = x % 10;
137-
x /= 10;
138-
for (int y = 0; y < 10; ++y) {
139-
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
140-
cnt[y]--;
141-
ans += y * t;
142-
t *= 10;
143-
break;
144-
}
126+
for (char c : s) {
127+
int x = c - '0';
128+
while (cnt[idx[x & 1]] == 0) {
129+
idx[x & 1] -= 2;
145130
}
131+
ans = ans * 10 + idx[x & 1];
132+
cnt[idx[x & 1]]--;
146133
}
147134
return ans;
148135
}
@@ -153,26 +140,25 @@ public:
153140
154141
```go
155142
func largestInteger(num int) int {
156-
cnt := make([]int, 10)
157-
x := num
158-
for x != 0 {
159-
cnt[x%10]++
160-
x /= 10
143+
s := []byte(fmt.Sprint(num))
144+
cnt := [10]int{}
145+
146+
for _, c := range s {
147+
cnt[c-'0']++
161148
}
162-
x = num
163-
ans, t := 0, 1
164-
for x != 0 {
165-
v := x % 10
166-
x /= 10
167-
for y := 0; y < 10; y++ {
168-
if ((v^y)&1) == 0 && cnt[y] > 0 {
169-
cnt[y]--
170-
ans += y * t
171-
t *= 10
172-
break
173-
}
149+
150+
idx := [2]int{8, 9}
151+
ans := 0
152+
153+
for _, c := range s {
154+
x := int(c - '0')
155+
for cnt[idx[x&1]] == 0 {
156+
idx[x&1] -= 2
174157
}
158+
ans = ans*10 + idx[x&1]
159+
cnt[idx[x&1]]--
175160
}
161+
176162
return ans
177163
}
178164
```
@@ -181,23 +167,26 @@ func largestInteger(num int) int {
181167

182168
```ts
183169
function largestInteger(num: number): number {
184-
const arrs: number[] = String(num).split('').map(Number);
185-
const odds: number[] = []; // 奇数
186-
const evens: number[] = [];
187-
for (const i of arrs) {
188-
if ((i & 1) == 1) {
189-
odds.push(i);
190-
} else {
191-
evens.push(i);
192-
}
170+
const s = num.toString().split('');
171+
const cnt = Array(10).fill(0);
172+
173+
for (const c of s) {
174+
cnt[+c]++;
193175
}
194-
odds.sort((a, b) => a - b);
195-
evens.sort((a, b) => a - b);
196-
const ans: number[] = [];
197-
for (const i of arrs) {
198-
ans.push((i & 1) === 1 ? odds.pop() : evens.pop());
176+
177+
const idx = [8, 9];
178+
let ans = 0;
179+
180+
for (const c of s) {
181+
const x = +c;
182+
while (cnt[idx[x % 2]] === 0) {
183+
idx[x % 2] -= 2;
184+
}
185+
ans = ans * 10 + idx[x % 2];
186+
cnt[idx[x % 2]]--;
199187
}
200-
return Number(ans.join(''));
188+
189+
return ans;
201190
}
202191
```
203192

0 commit comments

Comments
 (0)