feat: update lc problems (#3806)

This commit is contained in:
Libin YANG 2024-11-23 22:58:36 +08:00 committed by GitHub
parent b3fd3a07be
commit da940a9958
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 540 additions and 323 deletions

View File

@ -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 {
@ -190,7 +190,7 @@ class Solution {
}
ans = min(ans, x + y)
}
return ans
}
}

View File

@ -242,12 +242,12 @@ 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 {
@ -255,9 +255,9 @@ class Solution {
}
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])

View File

@ -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] == "." {
@ -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])

View File

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

View File

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

View File

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

View File

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

View File

@ -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:
- 设计

View File

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

View File

@ -60,6 +60,12 @@ 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
@ -67,22 +73,15 @@ tags:
```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
```
@ -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;
}
@ -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;
}
@ -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
}
```
@ -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]++;
}
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]]--;
}
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());
}
return Number(ans.join(''));
return ans;
}
```

View File

@ -58,7 +58,13 @@ Note that there may be other sequences of swaps but it can be shown that 87655 i
<!-- solution:start -->
### Solution 1
### Solution 1: Counting
We can use an array $\textit{cnt}$ of length $10$ to count the occurrences of each digit in the integer $\textit{num}$. We also use an index array $\textit{idx}$ to record the largest available even and odd digits, initially set to $[8, 9]$.
Next, we traverse each digit of the integer $\textit{num}$. If the digit is odd, we take the digit corresponding to index $1$ in $\textit{idx}$; otherwise, we take the digit corresponding to index $0$. If the count of the digit is $0$, we decrement the digit by $2$ and continue checking until we find a digit that meets the condition. Then, we update the answer and the count of the digit, and continue traversing until we have processed all digits of the integer $\textit{num}$.
The time complexity is $O(\log \textit{num})$, and the space complexity is $O(\log \textit{num})$.
<!-- tabs:start -->
@ -67,22 +73,15 @@ Note that there may be other sequences of swaps but it can be shown that 87655 i
```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
```
@ -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;
}
@ -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;
}
@ -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
}
```
@ -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]++;
}
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]]--;
}
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());
}
return Number(ans.join(''));
return ans;
}
```

View File

@ -1,27 +1,21 @@
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;
}
};
};

View File

@ -1,23 +1,22 @@
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
}
}

View File

@ -1,26 +1,20 @@
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;
}
}
}

View File

@ -1,19 +1,12 @@
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

View File

@ -1,19 +1,22 @@
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]++;
}
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]]--;
}
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());
}
return Number(ans.join(''));
return ans;
}

View File

@ -28,7 +28,7 @@ tags:
<li>同样,对 <code>nums = [7]</code> 而言,按位与等于 <code>7</code></li>
</ul>
<p>给你一个正整数数组 <code>candidates</code> 。计算 <code>candidates</code> 中的数字每种组合下 <strong>按位与</strong> 的结果。 <code>candidates</code> 中的每个数字在每种组合中只能使用 <strong>一次</strong></p>
<p>给你一个正整数数组 <code>candidates</code> 。计算 <code>candidates</code> 中的数字每种组合下 <strong>按位与</strong> 的结果。</p>
<p>返回按位与结果大于 <code>0</code><strong>最长</strong> 组合的长度<em></em></p>

View File

@ -28,7 +28,7 @@ tags:
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
</ul>
<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>
<p>You are given an array of positive integers <code>candidates</code>. Compute the <strong>bitwise AND</strong> for all possible <strong>combinations</strong> of elements in the <code>candidates</code> array.</p>
<p>Return <em>the size of the <strong>largest</strong> combination of </em><code>candidates</code><em> with a bitwise AND <strong>greater</strong> than </em><code>0</code>.</p>

View File

@ -29,7 +29,7 @@ tags:
<p>如果句子满足下述全部条件,则认为它是一个 <strong>回环句</strong> </p>
<ul>
<li>单词的最后一个字符和下一个单词的第一个字符相等</li>
<li>句子中每个单词的最后一个字符等于下一个单词的第一个字符</li>
<li>最后一个单词的最后一个字符和第一个单词的第一个字符相等。</li>
</ul>

View File

@ -29,7 +29,7 @@ tags:
<p>A sentence is <strong>circular </strong>if:</p>
<ul>
<li>The last character of a word is equal to the first character of the next word.</li>
<li>The last character of each word in the sentence is equal to the first character of its next word.</li>
<li>The last character of the last word is equal to the first character of the first word.</li>
</ul>

View File

@ -42,8 +42,8 @@ tags:
<ul>
<li>如果工人 <code>x</code> 到达桥边时,工人 <code>y</code> 正在过桥,那么工人 <code>x</code> 需要在桥边等待。</li>
<li>如果没有正在过桥的工人,那么在桥右边等待的工人可以先过桥。如果同时有多个工人在右边等待,那么 <strong>效率最低</strong> 的工人会先过桥</li>
<li>如果没有正在过桥的工人,且桥右边也没有在等待的工人,同时旧仓库还剩下至少一个箱子需要搬运,此时在桥左边的工人可以过桥。如果同时有多个工人在左边等待,那么 <strong>效率最低</strong> 的工人会先过桥</li>
<li>当桥梁未被使用时,优先让右侧 <strong>效率最低</strong> 的工人(已经拿起盒子的工人)过桥。如果不是,优先让左侧 <strong>效率最低</strong> 的工人通过</li>
<li>如果左侧已经派出足够的工人来拾取所有剩余的箱子,则 <strong>不会</strong> 再从左侧派出工人</li>
</ul>
<p>所有 <code>n</code> 个盒子都需要放入新仓库,<span class="text-only" data-eleid="8" style="white-space: pre;">请你返回最后一个搬运箱子的工人 </span><strong><span class="text-only" data-eleid="9" style="white-space: pre;">到达河左岸</span></strong><span class="text-only" data-eleid="10" style="white-space: pre;"> 的时间。</span></p>
@ -52,37 +52,37 @@ tags:
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]
<strong>输出:</strong>6
<strong>解释:</strong>
从 0 到 1 :工人 2 从左岸过桥到达右岸。
从 1 到 2 :工人 2 从旧仓库搬起一个箱子。
从 2 到 6 :工人 2 从右岸过桥到达左岸。
从 6 到 7 :工人 2 将箱子放入新仓库。
整个过程在 7 分钟后结束。因为问题关注的是最后一个工人到达左岸的时间,所以返回 6 。
</pre>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]</span></p>
<p><strong class="example">示例 2</strong></p>
<p><span class="example-io"><b>输出:</b>6</span></p>
<p><b>解释:</b></p>
<pre>
<strong>输入:</strong>n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]
<strong>输出:</strong>50
<strong>解释:</strong>
从 0 到 10 :工人 1 从左岸过桥到达右岸。
从 10 到 20 :工人 1 从旧仓库搬起一个箱子。
从 10 到 11 :工人 0 从左岸过桥到达右岸。
从 11 到 20 :工人 0 从旧仓库搬起一个箱子。
从 20 到 30 :工人 1 从右岸过桥到达左岸。
从 30 到 40 :工人 1 将箱子放入新仓库。
从 30 到 31 :工人 0 从右岸过桥到达左岸。
从 31 到 39 :工人 0 将箱子放入新仓库。
从 39 到 40 :工人 0 从左岸过桥到达右岸。
从 40 到 49 :工人 0 从旧仓库搬起一个箱子。
从 49 到 50 :工人 0 从右岸过桥到达左岸。
从 50 到 58 :工人 0 将箱子放入新仓库。
整个过程在 58 分钟后结束。因为问题关注的是最后一个工人到达左岸的时间,所以返回 50 。
从 0 到 1 分钟:工人 2 通过桥到达右侧。
从 1 到 2 分钟:工人 2 从右侧仓库拿起箱子。
从 2 到 6 分钟:工人 2 通过桥到达左侧。
从 6 到 7 分钟:工人 2 向左侧仓库放下箱子。
整个过程在 7 分钟后结束。我们返回 6 因为该问题要求的是最后一名工人到达桥梁左侧的时间。
</pre>
</div>
<p><strong class="example">示例&nbsp;2</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">n = 3, k = 2, time =</span> [[1,5,1,8],[10,10,10,10]]</p>
<p><b>输出:</b>37</p>
<p><strong>解释:</strong></p>
<pre>
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2500-2599/2532.Time%20to%20Cross%20a%20Bridge/images/378539249-c6ce3c73-40e7-4670-a8b5-7ddb9abede11.png" style="width: 450px; height: 176px;" />
</pre>
<p>最后一个盒子在37秒时到达左侧。请注意我们并 <strong>没有</strong> 放下最后一个箱子,因为那样会花费更多时间,而且它们已经和工人们一起在左边。</p>
</div>
<p>&nbsp;</p>

View File

@ -42,7 +42,7 @@ tags:
<ul>
<li>Only one worker can use the bridge at a time.</li>
<li>When the bridge is unused prioritize the <strong>least efficient</strong> worker on the right side to cross. If there are no workers on the right side, prioritize the <strong>least efficient</strong> worker on the left side to cross.</li>
<li>When the bridge is unused prioritize the <strong>least efficient</strong> worker (who have picked up the box) on the right side to cross. If not,&nbsp;prioritize the <strong>least efficient</strong> worker on the left side to cross.</li>
<li>If enough workers have already been dispatched from the left side to pick up all the remaining boxes, <strong>no more</strong> workers will be sent from the left side.</li>
</ul>
@ -70,25 +70,17 @@ The whole process ends after 7 minutes. We return 6 because the problem asks for
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]</span></p>
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 2, time =</span> [[1,5,1,8],[10,10,10,10]]</p>
<p><strong>Output:</strong> <span class="example-io">50</span></p>
<p><strong>Output:</strong> 37</p>
<p><strong>Explanation:</strong></p>
<pre>
From 0 to 10: worker 1 crosses the bridge to the right.
From 10 to 20: worker 1 picks up a box from the right warehouse.
From 10 to 11: worker 0 crosses the bridge to the right.
From 11 to 20: worker 0 picks up a box from the right warehouse.
From 20 to 30: worker 1 crosses the bridge to the left.
From 30 to 40: worker 1 puts a box at the left warehouse.
From 30 to 31: worker 0 crosses the bridge to the left.
From 31 to 39: worker 0 puts a box at the left warehouse.
From 39 to 40: worker 0 crosses the bridge to the right.
From 40 to 49: worker 0 picks up a box from the right warehouse.
From 49 to 50: worker 0 crosses the bridge to the left.
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2500-2599/2532.Time%20to%20Cross%20a%20Bridge/images/378539249-c6ce3c73-40e7-4670-a8b5-7ddb9abede11.png" style="width: 450px; height: 176px;" />
</pre>
<p>The last box reaches the left side at 37 seconds. Notice, how we <strong>do not</strong> put the last boxes down, as that would take more time, and they are already on the left with the workers.</p>
</div>
<p>&nbsp;</p>
@ -98,7 +90,7 @@ From 49 to 50: worker 0 crosses the bridge to the left.
<li><code>1 &lt;= n, k &lt;= 10<sup>4</sup></code></li>
<li><code>time.length == k</code></li>
<li><code>time[i].length == 4</code></li>
<li><code>1 &lt;= leftToRight<sub>i</sub>, pickOld<sub>i</sub>, rightToLeft<sub>i</sub>, putNew<sub>i</sub> &lt;= 1000</code></li>
<li><code>1 &lt;= left<sub>i</sub>, pick<sub>i</sub>, right<sub>i</sub>, put<sub>i</sub> &lt;= 1000</code></li>
</ul>
<!-- description:end -->

View File

@ -16,7 +16,7 @@ tags:
<!-- description:start -->
<p>Enhance all functions to have the&nbsp;<code>callPolyfill</code>&nbsp;method. The method accepts an object&nbsp;<code>obj</code>&nbsp;as it&#39;s first parameter and any number of additional arguments. The&nbsp;<code>obj</code>&nbsp;becomes the&nbsp;<code>this</code>&nbsp;context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code>&nbsp;method belongs on).</p>
<p>Enhance all functions to have the&nbsp;<code>callPolyfill</code>&nbsp;method. The method accepts an object&nbsp;<code>obj</code>&nbsp;as its first parameter and any number of additional arguments. The&nbsp;<code>obj</code>&nbsp;becomes the&nbsp;<code>this</code>&nbsp;context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code>&nbsp;method belongs on).</p>
<p>For example if you had the function:</p>

View File

@ -22,7 +22,7 @@ tags:
<p>For each <code>queries[i]</code>:</p>
<ul>
<li>Select a subset of indices within the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in <code>nums</code>.</li>
<li>Select a <span data-keyword="subset">subset</span> of indices within the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in <code>nums</code>.</li>
<li>Decrement the values at the selected indices by 1.</li>
</ul>
@ -30,8 +30,6 @@ tags:
<p>Return <code>true</code> if it is <em>possible</em> to transform <code>nums</code> into a <strong>Zero Array </strong>after processing all the queries sequentially, otherwise return <code>false</code>.</p>
<p>A <strong>subset</strong> of an array is a selection of elements (possibly none) of the array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

View File

@ -0,0 +1,125 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README.md
---
<!-- problem:start -->
# [3359. Find Sorted Submatrices With Maximum Element at Most K 🔒](https://leetcode.cn/problems/find-sorted-submatrices-with-maximum-element-at-most-k)
[English Version](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README_EN.md)
## 题目描述
<!-- description:start -->
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. You are also given a <strong>non-negative</strong> integer <code>k</code>.</p>
<p>Return the number of <strong>submatrices</strong> of <code>grid</code> that satisfy the following conditions:</p>
<ul>
<li>The maximum element in the submatrix <strong>less than or equal to</strong> <code>k</code>.</li>
<li>Each row in the submatrix is sorted in <strong>non-increasing</strong> order.</li>
</ul>
<p>A submatrix <code>(x1, y1, x2, y2)</code> is a matrix that forms by choosing all cells <code>grid[x][y]</code> where <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/images/mine.png" style="width: 360px; height: 200px;" /></strong></p>
<p>The 8 submatrices are:</p>
<ul>
<li><code>[[1]]</code></li>
<li><code>[[1]]</code></li>
<li><code>[[2,1]]</code></li>
<li><code>[[3,2,1]]</code></li>
<li><code>[[1],[1]]</code></li>
<li><code>[[2]]</code></li>
<li><code>[[3]]</code></li>
<li><code>[[3,2]]</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">36</span></p>
<p><strong>Explanation:</strong></p>
<p>There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= n == grid[i].length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>
<p>&nbsp;</p>
<!-- description:end -->
## 解法
<!-- solution:start -->
### 方法一
<!-- tabs:start -->
#### Python3
```python
```
#### Java
```java
```
#### C++
```cpp
```
#### Go
```go
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

View File

@ -0,0 +1,125 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README_EN.md
---
<!-- problem:start -->
# [3359. Find Sorted Submatrices With Maximum Element at Most K 🔒](https://leetcode.com/problems/find-sorted-submatrices-with-maximum-element-at-most-k)
[中文文档](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README.md)
## Description
<!-- description:start -->
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. You are also given a <strong>non-negative</strong> integer <code>k</code>.</p>
<p>Return the number of <strong>submatrices</strong> of <code>grid</code> that satisfy the following conditions:</p>
<ul>
<li>The maximum element in the submatrix <strong>less than or equal to</strong> <code>k</code>.</li>
<li>Each row in the submatrix is sorted in <strong>non-increasing</strong> order.</li>
</ul>
<p>A submatrix <code>(x1, y1, x2, y2)</code> is a matrix that forms by choosing all cells <code>grid[x][y]</code> where <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/images/mine.png" style="width: 360px; height: 200px;" /></strong></p>
<p>The 8 submatrices are:</p>
<ul>
<li><code>[[1]]</code></li>
<li><code>[[1]]</code></li>
<li><code>[[2,1]]</code></li>
<li><code>[[3,2,1]]</code></li>
<li><code>[[1],[1]]</code></li>
<li><code>[[2]]</code></li>
<li><code>[[3]]</code></li>
<li><code>[[3,2]]</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">36</span></p>
<p><strong>Explanation:</strong></p>
<p>There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= n == grid[i].length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>
<p>&nbsp;</p>
<!-- description:end -->
## Solutions
<!-- solution:start -->
### Solution 1
<!-- tabs:start -->
#### Python3
```python
```
#### Java
```java
```
#### C++
```cpp
```
#### Go
```go
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -26,6 +26,9 @@ comments: true
## 往期竞赛
#### 第 144 场双周赛(2024-11-23 22:30, 90 分钟) 参赛人数 1636
#### 第 424 场周赛(2024-11-17 10:30, 90 分钟) 参赛人数 2622
- [3354. 使数组元素等于零](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README.md)

View File

@ -29,6 +29,9 @@ If you want to estimate your score changes after the contest ends, you can visit
## Past Contests
#### Biweekly Contest 144
#### Weekly Contest 424
- [3354. Make Array Elements Equal to Zero](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README_EN.md)

View File

@ -318,7 +318,7 @@
| 0305 | [岛屿数量 II](/solution/0300-0399/0305.Number%20of%20Islands%20II/README.md) | `并查集`,`数组`,`哈希表` | 困难 | 🔒 |
| 0306 | [累加数](/solution/0300-0399/0306.Additive%20Number/README.md) | `字符串`,`回溯` | 中等 | |
| 0307 | [区域和检索 - 数组可修改](/solution/0300-0399/0307.Range%20Sum%20Query%20-%20Mutable/README.md) | `设计`,`树状数组`,`线段树`,`数组` | 中等 | |
| 0308 | [二维区域和检索 - 矩阵可修改](/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README.md) | `设计`,`树状数组`,`线段树`,`数组`,`矩阵` | 困难 | 🔒 |
| 0308 | [二维区域和检索 - 矩阵可修改](/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README.md) | `设计`,`树状数组`,`线段树`,`数组`,`矩阵` | 中等 | 🔒 |
| 0309 | [买卖股票的最佳时机含冷冻期](/solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README.md) | `数组`,`动态规划` | 中等 | |
| 0310 | [最小高度树](/solution/0300-0399/0310.Minimum%20Height%20Trees/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`拓扑排序` | 中等 | |
| 0311 | [稀疏矩阵的乘法](/solution/0300-0399/0311.Sparse%20Matrix%20Multiplication/README.md) | `数组`,`哈希表`,`矩阵` | 中等 | 🔒 |
@ -3369,6 +3369,7 @@
| 3356 | [零数组变换 II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README.md) | `数组`,`二分查找`,`前缀和` | 中等 | 第 424 场周赛 |
| 3357 | [最小化相邻元素的最大差值](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README.md) | `贪心`,`数组`,`二分查找` | 困难 | 第 424 场周赛 |
| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 |
| 3359 | [Find Sorted Submatrices With Maximum Element at Most K](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README.md) | | 困难 | 🔒 |
## 版权

View File

@ -316,7 +316,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 0305 | [Number of Islands II](/solution/0300-0399/0305.Number%20of%20Islands%20II/README_EN.md) | `Union Find`,`Array`,`Hash Table` | Hard | 🔒 |
| 0306 | [Additive Number](/solution/0300-0399/0306.Additive%20Number/README_EN.md) | `String`,`Backtracking` | Medium | |
| 0307 | [Range Sum Query - Mutable](/solution/0300-0399/0307.Range%20Sum%20Query%20-%20Mutable/README_EN.md) | `Design`,`Binary Indexed Tree`,`Segment Tree`,`Array` | Medium | |
| 0308 | [Range Sum Query 2D - Mutable](/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README_EN.md) | `Design`,`Binary Indexed Tree`,`Segment Tree`,`Array`,`Matrix` | Hard | 🔒 |
| 0308 | [Range Sum Query 2D - Mutable](/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README_EN.md) | `Design`,`Binary Indexed Tree`,`Segment Tree`,`Array`,`Matrix` | Medium | 🔒 |
| 0309 | [Best Time to Buy and Sell Stock with Cooldown](/solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README_EN.md) | `Array`,`Dynamic Programming` | Medium | |
| 0310 | [Minimum Height Trees](/solution/0300-0399/0310.Minimum%20Height%20Trees/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Topological Sort` | Medium | |
| 0311 | [Sparse Matrix Multiplication](/solution/0300-0399/0311.Sparse%20Matrix%20Multiplication/README_EN.md) | `Array`,`Hash Table`,`Matrix` | Medium | 🔒 |
@ -3367,6 +3367,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3356 | [Zero Array Transformation II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README_EN.md) | `Array`,`Binary Search`,`Prefix Sum` | Medium | Weekly Contest 424 |
| 3357 | [Minimize the Maximum Adjacent Element Difference](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README_EN.md) | `Greedy`,`Array`,`Binary Search` | Hard | Weekly Contest 424 |
| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | `Database` | Easy | 🔒 |
| 3359 | [Find Sorted Submatrices With Maximum Element at Most K](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README_EN.md) | | Hard | 🔒 |
## Copyright

File diff suppressed because one or more lines are too long