From 6eadd3eafc7c55b2bd945041e4752c09b20b69ed Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 28 Dec 2023 10:03:01 +0800 Subject: [PATCH 1/9] =?UTF-8?q?Update.=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA2=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 2c2ab225..69706e36 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -406,6 +406,21 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int MaxProfit(int[] prices) + { + int res = 0; + for (int i = 0; i < prices.Length - 1; i++) + { + res += Math.Max(0, prices[i + 1] - prices[i]); + } + return res; + } +} +```

From e209d3514302398515061906102cf3a42a4588ad Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 29 Dec 2023 16:45:31 +0800 Subject: [PATCH 2/9] =?UTF-8?q?Update0055.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0055.跳跃游戏.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index bedb09ab..086fd64f 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -258,6 +258,23 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool CanJump(int[] nums) + { + int cover = 0; + if (nums.Length == 1) return true; + for (int i = 0; i <= cover; i++) + { + cover = Math.Max(i + nums[i], cover); + if (cover >= nums.Length - 1) return true; + } + return false; + } +} +```

From a66e5e39ba70ab6d3fecaa29296d19245582ca00 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 30 Dec 2023 10:16:43 +0800 Subject: [PATCH 3/9] =?UTF-8?q?Update.0045=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F2=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index aab27b27..e006caa2 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -464,6 +464,27 @@ impl Solution { } } ``` +### C# +```csharp +// 版本二 +public class Solution +{ + public int Jump(int[] nums) + { + int cur = 0, next = 0, step = 0; + for (int i = 0; i < nums.Length - 1; i++) + { + next = Math.Max(next, i + nums[i]); + if (i == cur) + { + cur = next; + step++; + } + } + return step; + } +} +```

From 85f8efeef6085920de4905787ae5773012e053a9 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 31 Dec 2023 09:35:51 +0800 Subject: [PATCH 4/9] =?UTF-8?q?Update1005.K=E6=AC=A1=E5=8F=96=E5=8F=8D?= =?UTF-8?q?=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1005.K次取反后最大化的数组和.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index bed11c7a..182402d2 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -322,6 +322,29 @@ object Solution { } ``` +### C# +```csharp +public class Solution +{ + public int LargestSumAfterKNegations(int[] nums, int k) + { + int res = 0; + Array.Sort(nums, (a, b) => Math.Abs(b) - Math.Abs(a)); + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] < 0 && k > 0) + { + nums[i] *= -1; + k--; + } + } + if (k % 2 == 1) nums[nums.Length - 1] *= -1; + foreach (var item in nums) res += item; + return res; + } +} +``` +

From c9bff6e42c5f61f1f1ca873131eb2de4aff01003 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 1 Jan 2024 10:34:33 +0800 Subject: [PATCH 5/9] =?UTF-8?q?Update0134.=E5=8A=A0=E6=B2=B9=E7=AB=99?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 2f9539e8..c093023d 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -630,6 +630,29 @@ object Solution { } } ``` +### C# +```csharp +// 贪心算法,方法二 +public class Solution +{ + public int CanCompleteCircuit(int[] gas, int[] cost) + { + int curSum = 0, totalSum = 0, start = 0; + for (int i = 0; i < gas.Length; i++) + { + curSum += gas[i] - cost[i]; + totalSum += gas[i] - cost[i]; + if (curSum < 0) + { + start = i + 1; + curSum = 0; + } + } + if (totalSum < 0) return -1; + return start; + } +} +```

From 4f2b2b2618d522d73ef15ac200799453691170c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=9B=20=E9=99=88?= <761050293@qq.com> Date: Tue, 2 Jan 2024 09:50:19 +0800 Subject: [PATCH 6/9] =?UTF-8?q?Update0135.=E5=88=86=E5=8F=91=E7=B3=96?= =?UTF-8?q?=E6=9E=9C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index d130bd68..210f4995 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -370,6 +370,35 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int Candy(int[] ratings) + { + int[] candies = new int[ratings.Length]; + for (int i = 0; i < candies.Length; i++) + { + candies[i] = 1; + } + for (int i = 1; i < ratings.Length; i++) + { + if (ratings[i] > ratings[i - 1]) + { + candies[i] = candies[i - 1] + 1; + } + } + for (int i = ratings.Length - 2; i >= 0; i--) + { + if (ratings[i] > ratings[i + 1]) + { + candies[i] = Math.Max(candies[i], candies[i + 1] + 1); + } + } + return candies.Sum(); + } +} +```

From c736cffe22143965ffc50896ca5b11a3e65df536 Mon Sep 17 00:00:00 2001 From: zhqiao Date: Tue, 2 Jan 2024 21:16:29 +0800 Subject: [PATCH 7/9] =?UTF-8?q?0377=20=E6=96=B0=E5=A2=9Epython=20=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4DP=E6=95=B0=E7=BB=84=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0377.组合总和Ⅳ.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index d9699c54..05f852b1 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -206,6 +206,34 @@ class Solution: ``` +二维DP版 +```python +class Solution: + def combinationSum4(self, nums: List[int], target: int) -> int: + # dp[][j]和为j的组合的总数 + dp = [[0] * (target+1) for _ in nums] + + for i in range(len(nums)): + dp[i][0] = 1 + + # 这里不能初始化dp[0][j]。dp[0][j]的值依赖于dp[-1][j-nums[0]] + + for j in range(1, target+1): + for i in range(len(nums)): + + if j - nums[i] >= 0: + dp[i][j] = ( + # 不放nums[i] + # i = 0 时,dp[-1][j]恰好为0,所以没有特殊处理 + dp[i-1][j] + + # 放nums[i]。对于和为j的组合,只有试过全部物品,才能知道有几种组合方式。所以取最后一个物品dp[-1][j-nums[i]] + dp[-1][j-nums[i]] + ) + else: + dp[i][j] = dp[i-1][j] + return dp[-1][-1] +``` + ### Go: ```go From 499d2afb618298ddb3e0e4f8d88f6f13284f0d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=9B=20=E9=99=88?= <761050293@qq.com> Date: Wed, 3 Jan 2024 09:21:34 +0800 Subject: [PATCH 8/9] =?UTF-8?q?Update=200860.=E6=9F=A0=E6=AA=AC=E6=B0=B4?= =?UTF-8?q?=E6=89=BE=E9=9B=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0860.柠檬水找零.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index 50a0c31a..db70112d 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -397,6 +397,46 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool LemonadeChange(int[] bills) + { + int five = 0, ten = 0, twenty = 0; + foreach (var bill in bills) + { + if (bill == 5) five++; + if (bill == 10) + { + if (five == 0) return false; + five--; + ten++; + } + if (bill == 20) + { + if (ten > 0 && five > 0) + { + ten--; + five--; + twenty++; + } + else if (five >= 3) + { + five -= 3; + twenty++; + } + else + { + return false; + } + + } + } + return true; + } +} +```

From d479e755cbd39cfdc5b3dd9ba97f3c5ffdf84604 Mon Sep 17 00:00:00 2001 From: Relsola Date: Wed, 3 Jan 2024 20:31:05 +0800 Subject: [PATCH 9/9] =?UTF-8?q?update=20130.=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F=EF=BC=8C417.=E5=A4=AA=E5=B9=B3?= =?UTF-8?q?=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E6=96=B0=E5=A2=9E=20JS=20=E5=B9=BF=E6=90=9C=E5=92=8C?= =?UTF-8?q?=E6=B7=B1=E6=90=9C=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0130.被围绕的区域.md | 126 ++++++++++++++++++ .../0417.太平洋大西洋水流问题.md | 111 +++++++++++++++ 2 files changed, 237 insertions(+) diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index e8a1f02f..8014c0c8 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -435,6 +435,132 @@ class Solution: ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function dfs(board, x, y) { + board[x][y] = 'A'; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + dfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + dfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + dfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + dfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} + +/** + * @description 广度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function bfs(board, x, y) { + board[x][y] = 'A'; + const stack = [y, x]; + + while (stack.length !== 0) { + const top = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + top[0]; + const nextY = dir[i][1] + top[1]; + + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + + if (board[nextX][nextY] === 'O') { + board[nextX][nextY] = 'A'; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + bfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + bfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + bfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + bfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} +``` +

diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 6777e2d9..53ae14ed 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -450,6 +450,117 @@ class Solution: return ans ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function dfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY]) { + continue; + } + dfs(heights, visited, nextX, nextY, sign); + } + } + + for (let i = 0; i < rowSize; i++) { + dfs(heights, visited, i, 0, 0); + dfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + dfs(heights, visited, 0, i, 0); + dfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} + +/** + * @description 广度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function bfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + const stack = [y, x]; + while (stack.length !== 0) { + [x, y] = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY] || visited[nextX][nextY][sign]) { + continue; + } + visited[nextX][nextY][sign] = true; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < rowSize; i++) { + bfs(heights, visited, i, 0, 0); + bfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + bfs(heights, visited, 0, i, 0); + bfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} +```