mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -308,7 +308,7 @@ class Solution:
|
|||||||
class Solution:
|
class Solution:
|
||||||
def maxProfit(self, prices: List[int]) -> int:
|
def maxProfit(self, prices: List[int]) -> int:
|
||||||
length = len(prices)
|
length = len(prices)
|
||||||
if len == 0:
|
if length == 0:
|
||||||
return 0
|
return 0
|
||||||
dp = [[0] * 2 for _ in range(length)]
|
dp = [[0] * 2 for _ in range(length)]
|
||||||
dp[0][0] = -prices[0]
|
dp[0][0] = -prices[0]
|
||||||
|
@ -345,6 +345,37 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### 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
|
```go
|
||||||
func canCompleteCircuit(gas []int, cost []int) int {
|
func canCompleteCircuit(gas []int, cost []int) int {
|
||||||
curSum := 0
|
curSum := 0
|
||||||
|
@ -226,21 +226,36 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
```golang
|
|
||||||
//排序后,局部最优
|
版本一 大饼干优先
|
||||||
|
```Go
|
||||||
func findContentChildren(g []int, s []int) int {
|
func findContentChildren(g []int, s []int) int {
|
||||||
sort.Ints(g)
|
sort.Ints(g)
|
||||||
sort.Ints(s)
|
sort.Ints(s)
|
||||||
|
index := len(s) - 1
|
||||||
// 从小到大
|
result := 0
|
||||||
child := 0
|
for i := len(g) - 1; i >= 0; i-- {
|
||||||
for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ {
|
if index >= 0 && s[index] >= g[i] {
|
||||||
if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合
|
result++
|
||||||
child++
|
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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -615,7 +615,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
@ -585,7 +585,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
int getSum(int * nums, int numsSize){
|
int getSum(int * nums, int numsSize){
|
||||||
|
@ -355,6 +355,62 @@ public class Main {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### 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
|
### Go
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
* 陷阱二
|
* 陷阱二
|
||||||
|
|
||||||
在一个有序序列求最值的时候,不要定义一个全局遍历,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。
|
在一个有序序列求最值的时候,不要定义一个全局变量,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。
|
||||||
|
|
||||||
推荐要通过前一个数值(pre)和后一个数值比较(cur),得出最值。
|
推荐要通过前一个数值(pre)和后一个数值比较(cur),得出最值。
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user