Skip to content

Commit

Permalink
feat: update lc problems (#3806)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Nov 23, 2024
1 parent b3fd3a0 commit da940a9
Show file tree
Hide file tree
Showing 32 changed files with 538 additions and 321 deletions.
6 changes: 3 additions & 3 deletions lcp/LCP 33. 蓄水/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ class Solution {
if maxVat == 0 {
return 0
}

let n = vat.count
var ans = Int.max

for x in 1...maxVat {
var y = 0
for i in 0..<n {
Expand All @@ -190,7 +190,7 @@ class Solution {
}
ans = min(ans, x + y)
}

return ans
}
}
Expand Down
8 changes: 4 additions & 4 deletions lcp/LCP 34. 二叉树染色/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,22 @@ var maxValue = function (root, k) {

class Solution {
private var k: Int = 0

func maxValue(_ root: TreeNode?, _ k: Int) -> Int {
self.k = k
return dfs(root).max() ?? 0
}

private func dfs(_ root: TreeNode?) -> [Int] {
var ans = [Int](repeating: 0, count: k + 1)
guard let root = root else {
return ans
}
let l = dfs(root.left)
let r = dfs(root.right)

ans[0] = (l.max() ?? 0) + (r.max() ?? 0)

for i in 0..<k {
for j in 0..<k - i {
ans[i + j + 1] = max(ans[i + j + 1], root.val + l[i] + r[j])
Expand Down
16 changes: 8 additions & 8 deletions lcp/LCP 41. 黑白翻转棋/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ class Solution {
private var m = 0
private var n = 0
private var chessboard: [String] = []

func flipChess(_ chessboard: [String]) -> Int {
self.m = chessboard.count
self.n = chessboard[0].count
self.chessboard = chessboard
var ans = 0

for i in 0..<m {
for j in 0..<n {
if Array(chessboard[i])[j] == "." {
Expand All @@ -315,32 +315,32 @@ class Solution {
}
return ans
}

private func bfs(_ i: Int, _ j: Int) -> Int {
var queue: [[Int]] = [[i, j]]
var g = chessboard.map { Array($0) }
g[i][j] = "X"
var count = 0

while !queue.isEmpty {
let p = queue.removeFirst()
let i = p[0], j = p[1]

for a in -1...1 {
for b in -1...1 {
if a == 0 && b == 0 { continue }

var x = i + a, y = j + b
while x >= 0 && x < m && y >= 0 && y < n && g[x][y] == "O" {
x += a
y += b
}

if x >= 0 && x < m && y >= 0 && y < n && g[x][y] == "X" {
x -= a
y -= b
count += max(abs(x - i), abs(y - j))

while x != i || y != j {
g[x][y] = "X"
queue.append([x, y])
Expand Down
4 changes: 2 additions & 2 deletions lcp/LCP 44. 开幕式焰火/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ func dfs(root *TreeNode) {

class Solution {
private var uniqueColors: Set<Int> = []

func numColor(_ root: TreeNode?) -> Int {
dfs(root)
return uniqueColors.count
}

private func dfs(_ node: TreeNode?) {
guard let node = node else { return }
uniqueColors.insert(node.val)
Expand Down
4 changes: 2 additions & 2 deletions lcp/LCP 50. 宝石补给/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ function giveGem(gem: number[], operations: number[][]): number {
class Solution {
func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int {
var gem = gem

for op in operations {
let x = op[0], y = op[1]
let v = gem[x] / 2
gem[y] += v
gem[x] -= v
}

let maxGem = gem.max() ?? 0
let minGem = gem.min() ?? 0
return maxGem - minGem
Expand Down
6 changes: 5 additions & 1 deletion solution/0200-0299/0289.Game of Life/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ tags:
<li>如果死细胞周围正好有三个活细胞,则该位置死细胞复活;</li>
</ol>

<p>下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 <code>m x n</code> 网格面板 <code>board</code> 的当前状态,返回下一个状态。</p>
<p>下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是 <strong>同时</strong> 发生的。给你 <code>m x n</code> 网格面板 <code>board</code> 的当前状态,返回下一个状态。</p>

<p>给定当前&nbsp;<code>board</code>&nbsp;的状态,<strong>更新</strong>&nbsp;<code>board</code>&nbsp;到下一个状态。</p>

<p><strong>注意</strong> 你不需要返回任何东西。</p>

<p>&nbsp;</p>

Expand Down
8 changes: 6 additions & 2 deletions solution/0200-0299/0289.Game of Life/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:

<!-- description:start -->

<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>
<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>

<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>

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

<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>
<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>

<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>

<p><strong>Note</strong> that you do not need to return anything.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
comments: true
difficulty: 困难
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README.md
tags:
- 设计
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
comments: true
difficulty: Hard
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README_EN.md
tags:
- Design
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,28 @@ tags:

### 方法一:计数

我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 统计整数 $\textit{num}$ 中所有数字出现的次数,用一个下标数组 $\textit{idx}$ 记录当前最大可用偶数和奇数,初始时 $\textit{idx}$ 为 $[8, 9]$。

接下来,我们依次遍历整数 $\textit{num}$ 的每个数字,如果数字为奇数,则取 $\textit{idx}$ 下标为 $1$ 对应的数字,否则取下标为 $0$ 对应的数字。如果该数字出现的次数为 $0$,则需要将数字减 $2$,继续判断,直到存在满足条件的数。然后,我们更新答案,以及数字出现的次数,继续遍历,直到遍历到整数 $\textit{num}$。

时间复杂度 $O(\log \textit{num})$,空间复杂度 $O(\log \textit{num})$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def largestInteger(self, num: int) -> int:
cnt = Counter()
x = num
while x:
x, v = divmod(x, 10)
cnt[v] += 1
x = num
nums = [int(c) for c in str(num)]
cnt = Counter(nums)
idx = [8, 9]
ans = 0
t = 1
while x:
x, v = divmod(x, 10)
for y in range(10):
if ((v ^ y) & 1) == 0 and cnt[y]:
ans += y * t
t *= 10
cnt[y] -= 1
break
for x in nums:
while cnt[idx[x & 1]] == 0:
idx[x & 1] -= 2
ans = ans * 10 + idx[x & 1]
cnt[idx[x & 1]] -= 1
return ans
```

Expand All @@ -91,26 +90,20 @@ class Solution:
```java
class Solution {
public int largestInteger(int num) {
char[] s = String.valueOf(num).toCharArray();
int[] cnt = new int[10];
int x = num;
while (x != 0) {
cnt[x % 10]++;
x /= 10;
for (char c : s) {
++cnt[c - '0'];
}
x = num;
int[] idx = {8, 9};
int ans = 0;
int t = 1;
while (x != 0) {
int v = x % 10;
x /= 10;
for (int y = 0; y < 10; ++y) {
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
cnt[y]--;
ans += y * t;
t *= 10;
break;
}
for (char c : s) {
int x = c - '0';
while (cnt[idx[x & 1]] == 0) {
idx[x & 1] -= 2;
}
ans = ans * 10 + idx[x & 1];
cnt[idx[x & 1]]--;
}
return ans;
}
Expand All @@ -123,26 +116,20 @@ class Solution {
class Solution {
public:
int largestInteger(int num) {
vector<int> cnt(10);
int x = num;
while (x) {
cnt[x % 10]++;
x /= 10;
string s = to_string(num);
int cnt[10] = {0};
for (char c : s) {
cnt[c - '0']++;
}
x = num;
int idx[2] = {8, 9};
int ans = 0;
long t = 1;
while (x) {
int v = x % 10;
x /= 10;
for (int y = 0; y < 10; ++y) {
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
cnt[y]--;
ans += y * t;
t *= 10;
break;
}
for (char c : s) {
int x = c - '0';
while (cnt[idx[x & 1]] == 0) {
idx[x & 1] -= 2;
}
ans = ans * 10 + idx[x & 1];
cnt[idx[x & 1]]--;
}
return ans;
}
Expand All @@ -153,26 +140,25 @@ public:
```go
func largestInteger(num int) int {
cnt := make([]int, 10)
x := num
for x != 0 {
cnt[x%10]++
x /= 10
s := []byte(fmt.Sprint(num))
cnt := [10]int{}
for _, c := range s {
cnt[c-'0']++
}
x = num
ans, t := 0, 1
for x != 0 {
v := x % 10
x /= 10
for y := 0; y < 10; y++ {
if ((v^y)&1) == 0 && cnt[y] > 0 {
cnt[y]--
ans += y * t
t *= 10
break
}
idx := [2]int{8, 9}
ans := 0
for _, c := range s {
x := int(c - '0')
for cnt[idx[x&1]] == 0 {
idx[x&1] -= 2
}
ans = ans*10 + idx[x&1]
cnt[idx[x&1]]--
}
return ans
}
```
Expand All @@ -181,23 +167,26 @@ func largestInteger(num int) int {

```ts
function largestInteger(num: number): number {
const arrs: number[] = String(num).split('').map(Number);
const odds: number[] = []; // 奇数
const evens: number[] = [];
for (const i of arrs) {
if ((i & 1) == 1) {
odds.push(i);
} else {
evens.push(i);
}
const s = num.toString().split('');
const cnt = Array(10).fill(0);

for (const c of s) {
cnt[+c]++;
}
odds.sort((a, b) => a - b);
evens.sort((a, b) => a - b);
const ans: number[] = [];
for (const i of arrs) {
ans.push((i & 1) === 1 ? odds.pop() : evens.pop());

const idx = [8, 9];
let ans = 0;

for (const c of s) {
const x = +c;
while (cnt[idx[x % 2]] === 0) {
idx[x % 2] -= 2;
}
ans = ans * 10 + idx[x % 2];
cnt[idx[x % 2]]--;
}
return Number(ans.join(''));

return ans;
}
```

Expand Down
Loading

0 comments on commit da940a9

Please sign in to comment.