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] 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 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 } ``` 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 9d13067e..33e3e953 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -585,7 +585,7 @@ impl Solution { } } ``` -## C +### C ```c int getSum(int * nums, int numsSize){ 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 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),得出最值。