diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README.md b/solution/0200-0299/0232.Implement Queue using Stacks/README.md index e99499c36958f..dceef8fe5a15c 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README.md @@ -71,7 +71,7 @@ myQueue.empty(); // return false ### 方法一:双栈 -使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。 +我们使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。 入队时,直接将元素入栈 `stk1`。时间复杂度 $O(1)$。 @@ -282,7 +282,7 @@ class MyQueue { peek(): number { this.move(); - return this.stk2[this.stk2.length - 1]; + return this.stk2.at(-1); } empty(): boolean { @@ -292,7 +292,7 @@ class MyQueue { move(): void { if (!this.stk2.length) { while (this.stk1.length) { - this.stk2.push(this.stk1.pop()); + this.stk2.push(this.stk1.pop()!); } } } diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md index 9dd02a423f6f5..629daa6eb2317 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md @@ -260,7 +260,7 @@ class MyQueue { peek(): number { this.move(); - return this.stk2[this.stk2.length - 1]; + return this.stk2.at(-1); } empty(): boolean { @@ -270,7 +270,7 @@ class MyQueue { move(): void { if (!this.stk2.length) { while (this.stk1.length) { - this.stk2.push(this.stk1.pop()); + this.stk2.push(this.stk1.pop()!); } } } diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts index ec285a5f6671a..6ca26efbc57f9 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts +++ b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts @@ -18,7 +18,7 @@ class MyQueue { peek(): number { this.move(); - return this.stk2[this.stk2.length - 1]; + return this.stk2.at(-1); } empty(): boolean { @@ -28,7 +28,7 @@ class MyQueue { move(): void { if (!this.stk2.length) { while (this.stk1.length) { - this.stk2.push(this.stk1.pop()); + this.stk2.push(this.stk1.pop()!); } } } diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md index 155e4a0b2a2ab..eba57fc794a69 100644 --- a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md @@ -57,24 +57,103 @@ ## 解法 -### 方法一 +### 方法一:模拟 + +我们创建两个数组 `arr1` 和 `arr2`,分别存放 `nums` 中的元素,初始时 `arr1` 中只有 `nums[0]`,`arr2` 中只有 `nums[1]`。 + +然后遍历 `nums` 下标从 $2$ 开始的元素,如果 `arr1` 的最后一个元素大于 `arr2` 的最后一个元素,就将当前元素追加到 `arr1`,否则追加到 `arr2`。 + +最后将 `arr2` 中的元素追加到 `arr1` 中,返回 `arr1`。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。 ```python - +class Solution: + def resultArray(self, nums: List[int]) -> List[int]: + arr1 = [nums[0]] + arr2 = [nums[1]] + for x in nums[2:]: + if arr1[-1] > arr2[-1]: + arr1.append(x) + else: + arr2.append(x) + return arr1 + arr2 ``` ```java - +class Solution { + public int[] resultArray(int[] nums) { + int n = nums.length; + int[] arr1 = new int[n]; + int[] arr2 = new int[n]; + arr1[0] = nums[0]; + arr2[0] = nums[1]; + int i = 0, j = 0; + for (int k = 2; k < n; ++k) { + if (arr1[i] > arr2[j]) { + arr1[++i] = nums[k]; + } else { + arr2[++j] = nums[k]; + } + } + for (int k = 0; k <= j; ++k) { + arr1[++i] = arr2[k]; + } + return arr1; + } +} ``` ```cpp - +class Solution { +public: + vector resultArray(vector& nums) { + int n = nums.size(); + vector arr1 = {nums[0]}; + vector arr2 = {nums[1]}; + for (int k = 2; k < n; ++k) { + if (arr1.back() > arr2.back()) { + arr1.push_back(nums[k]); + } else { + arr2.push_back(nums[k]); + } + } + arr1.insert(arr1.end(), arr2.begin(), arr2.end()); + return arr1; + } +}; ``` ```go +func resultArray(nums []int) []int { + arr1 := []int{nums[0]} + arr2 := []int{nums[1]} + for _, x := range nums[2:] { + if arr1[len(arr1)-1] > arr2[len(arr2)-1] { + arr1 = append(arr1, x) + } else { + arr2 = append(arr2, x) + } + } + return append(arr1, arr2...) +} +``` +```ts +function resultArray(nums: number[]): number[] { + const arr1: number[] = [nums[0]]; + const arr2: number[] = [nums[1]]; + for (const x of nums.slice(2)) { + if (arr1.at(-1)! > arr2.at(-1)!) { + arr1.push(x); + } else { + arr2.push(x); + } + } + return arr1.concat(arr2); +} ``` diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md index 531da39ac54d8..f8768693f59df 100644 --- a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md @@ -53,24 +53,103 @@ Hence, the array result formed by concatenation is [5,3,4,8]. ## Solutions -### Solution 1 +### Solution 1: Simulation + +We create two arrays `arr1` and `arr2`, which store the elements in `nums`. Initially, `arr1` only contains `nums[0]`, and `arr2` only contains `nums[1]`. + +Then we traverse the elements of `nums` starting from index 2. If the last element of `arr1` is greater than the last element of `arr2`, we append the current element to `arr1`, otherwise we append it to `arr2`. + +Finally, we append the elements in `arr2` to `arr1` and return `arr1`. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`. ```python - +class Solution: + def resultArray(self, nums: List[int]) -> List[int]: + arr1 = [nums[0]] + arr2 = [nums[1]] + for x in nums[2:]: + if arr1[-1] > arr2[-1]: + arr1.append(x) + else: + arr2.append(x) + return arr1 + arr2 ``` ```java - +class Solution { + public int[] resultArray(int[] nums) { + int n = nums.length; + int[] arr1 = new int[n]; + int[] arr2 = new int[n]; + arr1[0] = nums[0]; + arr2[0] = nums[1]; + int i = 0, j = 0; + for (int k = 2; k < n; ++k) { + if (arr1[i] > arr2[j]) { + arr1[++i] = nums[k]; + } else { + arr2[++j] = nums[k]; + } + } + for (int k = 0; k <= j; ++k) { + arr1[++i] = arr2[k]; + } + return arr1; + } +} ``` ```cpp - +class Solution { +public: + vector resultArray(vector& nums) { + int n = nums.size(); + vector arr1 = {nums[0]}; + vector arr2 = {nums[1]}; + for (int k = 2; k < n; ++k) { + if (arr1.back() > arr2.back()) { + arr1.push_back(nums[k]); + } else { + arr2.push_back(nums[k]); + } + } + arr1.insert(arr1.end(), arr2.begin(), arr2.end()); + return arr1; + } +}; ``` ```go +func resultArray(nums []int) []int { + arr1 := []int{nums[0]} + arr2 := []int{nums[1]} + for _, x := range nums[2:] { + if arr1[len(arr1)-1] > arr2[len(arr2)-1] { + arr1 = append(arr1, x) + } else { + arr2 = append(arr2, x) + } + } + return append(arr1, arr2...) +} +``` +```ts +function resultArray(nums: number[]): number[] { + const arr1: number[] = [nums[0]]; + const arr2: number[] = [nums[1]]; + for (const x of nums.slice(2)) { + if (arr1.at(-1)! > arr2.at(-1)!) { + arr1.push(x); + } else { + arr2.push(x); + } + } + return arr1.concat(arr2); +} ``` diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.cpp b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.cpp new file mode 100644 index 0000000000000..8e53f9e859a80 --- /dev/null +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector resultArray(vector& nums) { + int n = nums.size(); + vector arr1 = {nums[0]}; + vector arr2 = {nums[1]}; + for (int k = 2; k < n; ++k) { + if (arr1.back() > arr2.back()) { + arr1.push_back(nums[k]); + } else { + arr2.push_back(nums[k]); + } + } + arr1.insert(arr1.end(), arr2.begin(), arr2.end()); + return arr1; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.go b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.go new file mode 100644 index 0000000000000..a37a8a825691d --- /dev/null +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.go @@ -0,0 +1,12 @@ +func resultArray(nums []int) []int { + arr1 := []int{nums[0]} + arr2 := []int{nums[1]} + for _, x := range nums[2:] { + if arr1[len(arr1)-1] > arr2[len(arr2)-1] { + arr1 = append(arr1, x) + } else { + arr2 = append(arr2, x) + } + } + return append(arr1, arr2...) +} \ No newline at end of file diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.java b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.java new file mode 100644 index 0000000000000..185acb9999648 --- /dev/null +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.java @@ -0,0 +1,21 @@ +class Solution { + public int[] resultArray(int[] nums) { + int n = nums.length; + int[] arr1 = new int[n]; + int[] arr2 = new int[n]; + arr1[0] = nums[0]; + arr2[0] = nums[1]; + int i = 0, j = 0; + for (int k = 2; k < n; ++k) { + if (arr1[i] > arr2[j]) { + arr1[++i] = nums[k]; + } else { + arr2[++j] = nums[k]; + } + } + for (int k = 0; k <= j; ++k) { + arr1[++i] = arr2[k]; + } + return arr1; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.py b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.py new file mode 100644 index 0000000000000..408408a6529d6 --- /dev/null +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def resultArray(self, nums: List[int]) -> List[int]: + arr1 = [nums[0]] + arr2 = [nums[1]] + for x in nums[2:]: + if arr1[-1] > arr2[-1]: + arr1.append(x) + else: + arr2.append(x) + return arr1 + arr2 diff --git a/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.ts b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.ts new file mode 100644 index 0000000000000..c28536b726c64 --- /dev/null +++ b/solution/3000-3099/3069.Distribute Elements Into Two Arrays I/Solution.ts @@ -0,0 +1,12 @@ +function resultArray(nums: number[]): number[] { + const arr1: number[] = [nums[0]]; + const arr2: number[] = [nums[1]]; + for (const x of nums.slice(2)) { + if (arr1.at(-1)! > arr2.at(-1)!) { + arr1.push(x); + } else { + arr2.push(x); + } + } + return arr1.concat(arr2); +} diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README.md b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README.md index dcf6ab0bf5f0a..66f365540447d 100644 --- a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README.md +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README.md @@ -43,24 +43,106 @@ ## 解法 -### 方法一 +### 方法一:二维前缀和 + +题目实际上求的是二维矩阵有多少个和小于等于 $k$ 的前缀子矩阵。 + +二维前缀和的计算公式为: + +$$ +s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x +$$ + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 ```python - +class Solution: + def countSubmatrices(self, grid: List[List[int]], k: int) -> int: + s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x + ans += s[i][j] <= k + return ans ``` ```java - +class Solution { + public int countSubmatrices(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] s = new int[m + 1][n + 1]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int countSubmatrices(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + int s[m + 1][n + 1]; + memset(s, 0, sizeof(s)); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func countSubmatrices(grid [][]int, k int) (ans int) { + s := make([][]int, len(grid)+1) + for i := range s { + s[i] = make([]int, len(grid[0])+1) + } + for i, row := range grid { + for j, x := range row { + s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x + if s[i+1][j+1] <= k { + ans++ + } + } + } + return +} +``` +```ts +function countSubmatrices(grid: number[][], k: number): number { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + let ans: number = 0; + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README_EN.md b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README_EN.md index f837ded18091b..d8d733c61a1fd 100644 --- a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README_EN.md +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README_EN.md @@ -39,24 +39,106 @@ ## Solutions -### Solution 1 +### Solution 1: Two-Dimensional Prefix Sum + +The problem is actually asking for the number of prefix submatrices in a two-dimensional matrix whose sum is less than or equal to $k$. + +The calculation formula for the two-dimensional prefix sum is: + +$$ +s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x +$$ + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. ```python - +class Solution: + def countSubmatrices(self, grid: List[List[int]], k: int) -> int: + s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x + ans += s[i][j] <= k + return ans ``` ```java - +class Solution { + public int countSubmatrices(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] s = new int[m + 1][n + 1]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int countSubmatrices(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + int s[m + 1][n + 1]; + memset(s, 0, sizeof(s)); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +}; ``` ```go +func countSubmatrices(grid [][]int, k int) (ans int) { + s := make([][]int, len(grid)+1) + for i := range s { + s[i] = make([]int, len(grid[0])+1) + } + for i, row := range grid { + for j, x := range row { + s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x + if s[i+1][j+1] <= k { + ans++ + } + } + } + return +} +``` +```ts +function countSubmatrices(grid: number[][], k: number): number { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + let ans: number = 0; + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.cpp b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.cpp new file mode 100644 index 0000000000000..0bb4cd9654a45 --- /dev/null +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int countSubmatrices(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + int s[m + 1][n + 1]; + memset(s, 0, sizeof(s)); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.go b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.go new file mode 100644 index 0000000000000..8637b1763e00e --- /dev/null +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.go @@ -0,0 +1,15 @@ +func countSubmatrices(grid [][]int, k int) (ans int) { + s := make([][]int, len(grid)+1) + for i := range s { + s[i] = make([]int, len(grid[0])+1) + } + for i, row := range grid { + for j, x := range row { + s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x + if s[i+1][j+1] <= k { + ans++ + } + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.java b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.java new file mode 100644 index 0000000000000..3d43ff8384226 --- /dev/null +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int countSubmatrices(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] s = new int[m + 1][n + 1]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.py b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.py new file mode 100644 index 0000000000000..7b37f3c48701d --- /dev/null +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def countSubmatrices(self, grid: List[List[int]], k: int) -> int: + s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x + ans += s[i][j] <= k + return ans diff --git a/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.ts b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.ts new file mode 100644 index 0000000000000..64aab224221b0 --- /dev/null +++ b/solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/Solution.ts @@ -0,0 +1,15 @@ +function countSubmatrices(grid: number[][], k: number): number { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + let ans: number = 0; + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + if (s[i][j] <= k) { + ++ans; + } + } + } + return ans; +}