From e828a4de861b4ecb5f2f68f91feeda1b1f17d2b9 Mon Sep 17 00:00:00 2001 From: SaladDay <92240037+SaladDay@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:26:01 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=90=E6=A0=87?= =?UTF-8?q?=E9=A2=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 2 +- problems/0494.目标和.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 7b46abee..72d62243 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -615,7 +615,7 @@ impl Solution { } } ``` -## C +### C ```c #define max(a, b) ((a) > (b) ? (a) : (b)) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 92616ed1..2e519b96 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -585,7 +585,7 @@ impl Solution { } } ``` -## C +### C ```c int getSum(int * nums, int numsSize){ From 6fa4ad4a91d5cbc80a7d98035a763e1afd2054e7 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 30 Jul 2024 09:35:12 +0800 Subject: [PATCH 2/6] =?UTF-8?q?455.=E5=88=86=E5=8F=91=E9=A5=BC=E5=B9=B2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 39 ++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 9f59d3ad..22dd7570 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -226,21 +226,36 @@ class Solution: ``` ### Go -```golang -//排序后,局部最优 + +版本一 大饼干优先 +```Go func findContentChildren(g []int, s []int) int { - sort.Ints(g) - sort.Ints(s) - - // 从小到大 - child := 0 - for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ { - if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合 - child++ + sort.Ints(g) + sort.Ints(s) + index := len(s) - 1 + result := 0 + for i := len(g) - 1; i >= 0; i-- { + if index >= 0 && s[index] >= g[i] { + result++ + index-- + } } - } + return result +} +``` - return child +版本二 小饼干优先 +```Go +func findContentChildren(g []int, s []int) int { + sort.Ints(g) + sort.Ints(s) + index := 0 + for i := 0; i < len(s); i++ { + if index < len(g) && g[index] <= s[i] { + index++ + } + } + return index } ``` From 573cfcf3c00dd19b4dd4e1f275bb14c3d9f6acb1 Mon Sep 17 00:00:00 2001 From: DengTao <54075362+only-tao@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:51:43 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Update=2020201017=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这里应该是"全局变量"吧 --- problems/周总结/20201017二叉树周末总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/周总结/20201017二叉树周末总结.md b/problems/周总结/20201017二叉树周末总结.md index bbb14502..03148b15 100644 --- a/problems/周总结/20201017二叉树周末总结.md +++ b/problems/周总结/20201017二叉树周末总结.md @@ -40,7 +40,7 @@ * 陷阱二 -在一个有序序列求最值的时候,不要定义一个全局遍历,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。 +在一个有序序列求最值的时候,不要定义一个全局变量,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。 推荐要通过前一个数值(pre)和后一个数值比较(cur),得出最值。 From e0237b5b4f151e84762d66280f464effe48f440d Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 31 Jul 2024 14:39:37 +0800 Subject: [PATCH 4/6] =?UTF-8?q?134.=E5=8A=A0=E6=B2=B9=E7=AB=99=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Go=E8=B4=AA=E5=BF=83=E7=AE=97=E6=B3=95=EF=BC=88?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B8=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 1329bd9a..7ac9f0f9 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -345,6 +345,37 @@ class Solution: ``` ### Go + +贪心算法(方法一) +```go +func canCompleteCircuit(gas []int, cost []int) int { + curSum := 0 + min := math.MaxInt64 + for i := 0; i < len(gas); i++ { + rest := gas[i] - cost[i] + curSum += rest + if curSum < min { + min = curSum + } + } + if curSum < 0 { + return -1 + } + if min >= 0 { + return 0 + } + for i := len(gas) - 1; i > 0; i-- { + rest := gas[i] - cost[i] + min += rest + if min >= 0 { + return i + } + } + return -1 +} +``` + +贪心算法(方法二) ```go func canCompleteCircuit(gas []int, cost []int) int { curSum := 0 From 0d7f8b03150a2114946192ef181fa8e00d5c5bf8 Mon Sep 17 00:00:00 2001 From: kyshen Date: Sat, 3 Aug 2024 23:52:26 +0800 Subject: [PATCH 5/6] feat: python solution, DFS --- problems/kamacoder/0103.水流问题.md | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/kamacoder/0103.水流问题.md b/problems/kamacoder/0103.水流问题.md index b19e4778..9a34bf09 100644 --- a/problems/kamacoder/0103.水流问题.md +++ b/problems/kamacoder/0103.水流问题.md @@ -355,6 +355,62 @@ public class Main { ``` ### Python +```Python +first = set() +second = set() +directions = [[-1, 0], [0, 1], [1, 0], [0, -1]] + +def dfs(i, j, graph, visited, side): + if visited[i][j]: + return + + visited[i][j] = True + side.add((i, j)) + + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(graph) + and 0 <= new_y < len(graph[0]) + and int(graph[new_x][new_y]) >= int(graph[i][j]) + ): + dfs(new_x, new_y, graph, visited, side) + +def main(): + global first + global second + + N, M = map(int, input().strip().split()) + graph = [] + for _ in range(N): + row = input().strip().split() + graph.append(row) + + # 是否可到达第一边界 + visited = [[False] * M for _ in range(N)] + for i in range(M): + dfs(0, i, graph, visited, first) + for i in range(N): + dfs(i, 0, graph, visited, first) + + # 是否可到达第二边界 + visited = [[False] * M for _ in range(N)] + for i in range(M): + dfs(N - 1, i, graph, visited, second) + for i in range(N): + dfs(i, M - 1, graph, visited, second) + + # 可到达第一边界和第二边界 + res = first & second + + for x, y in res: + print(f"{x} {y}") + + +if __name__ == "__main__": + main() +``` ### Go From ba773d93bedd6bdfd7841d5b0aa13244ace303eb Mon Sep 17 00:00:00 2001 From: YimvPi Date: Sat, 3 Aug 2024 14:45:16 -0400 Subject: [PATCH 6/6] Change 'len' to 'length' --- problems/0121.买卖股票的最佳时机.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index df586ff9..f8092503 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -308,7 +308,7 @@ class Solution: class Solution: def maxProfit(self, prices: List[int]) -> int: length = len(prices) - if len == 0: + if length == 0: return 0 dp = [[0] * 2 for _ in range(length)] dp[0][0] = -prices[0]