mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -214,6 +214,7 @@ class Solution:
|
||||
return result
|
||||
|
||||
```
|
||||
贪心法
|
||||
```python
|
||||
class Solution:
|
||||
def maxSubArray(self, nums):
|
||||
@ -226,8 +227,18 @@ class Solution:
|
||||
if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
|
||||
count = 0
|
||||
return result
|
||||
|
||||
|
||||
```
|
||||
动态规划
|
||||
```python
|
||||
class Solution:
|
||||
def maxSubArray(self, nums: List[int]) -> int:
|
||||
dp = [0] * len(nums)
|
||||
dp[0] = nums[0]
|
||||
res = nums[0]
|
||||
for i in range(1, len(nums)):
|
||||
dp[i] = max(dp[i-1] + nums[i], nums[i])
|
||||
res = max(res, dp[i])
|
||||
return res
|
||||
```
|
||||
### Go
|
||||
贪心法
|
||||
|
@ -480,7 +480,84 @@ const bfs = (graph, visited, x, y) => {
|
||||
})()
|
||||
```
|
||||
|
||||
```javascript
|
||||
|
||||
// 深搜版
|
||||
|
||||
const r1 = require('readline').createInterface({ input: process.stdin });
|
||||
// 创建readline接口
|
||||
let iter = r1[Symbol.asyncIterator]();
|
||||
// 创建异步迭代器
|
||||
const readline = async () => (await iter.next()).value;
|
||||
|
||||
let graph // 地图
|
||||
let N, M // 地图大小
|
||||
let visited // 访问过的节点
|
||||
let result = 0 // 最大岛屿面积
|
||||
let count = 0 // 岛屿内节点数
|
||||
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向
|
||||
|
||||
// 读取输入,初始化地图
|
||||
const initGraph = async () => {
|
||||
let line = await readline();
|
||||
[N, M] = line.split(' ').map(Number);
|
||||
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
|
||||
visited = new Array(N).fill(false).map(() => new Array(M).fill(false))
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
line = await readline()
|
||||
line = line.split(' ').map(Number)
|
||||
for (let j = 0; j < M; j++) {
|
||||
graph[i][j] = line[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 从(x, y)开始深度优先遍历
|
||||
* @param {*} graph 地图
|
||||
* @param {*} visited 访问过的节点
|
||||
* @param {*} x 开始搜索节点的下标
|
||||
* @param {*} y 开始搜索节点的下标
|
||||
* @return {*}
|
||||
*/
|
||||
const dfs = (graph, visited, x, y) => {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
let nextx = x + dir[i][0]
|
||||
let nexty = y + dir[i][1]
|
||||
if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
|
||||
if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){
|
||||
count++
|
||||
visited[nextx][nexty] = true
|
||||
dfs(graph, visited, nextx, nexty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(async function () {
|
||||
|
||||
// 读取输入,初始化地图
|
||||
await initGraph()
|
||||
|
||||
// 统计最大岛屿面积
|
||||
for (let i = 0; i < N; i++) {
|
||||
for (let j = 0; j < M; j++) {
|
||||
if (!visited[i][j] && graph[i][j] === 1) { //遇到没有访问过的陆地
|
||||
// 重新计算面积
|
||||
count = 1
|
||||
visited[i][j] = true
|
||||
|
||||
// 深度优先遍历,统计岛屿内节点数,并将岛屿标记为已访问
|
||||
dfs(graph, visited, i, j)
|
||||
|
||||
// 更新最大岛屿面积
|
||||
result = Math.max(result, count)
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(result);
|
||||
})()
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
|
@ -307,6 +307,71 @@ for i in range(n):
|
||||
|
||||
print(count)
|
||||
```
|
||||
|
||||
```python
|
||||
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
|
||||
result = 0
|
||||
|
||||
# 深度搜尋
|
||||
def dfs(grid, y, x):
|
||||
grid[y][x] = 0
|
||||
global result
|
||||
result += 1
|
||||
|
||||
for i, j in direction:
|
||||
next_x = x + j
|
||||
next_y = y + i
|
||||
if (next_x < 0 or next_y < 0 or
|
||||
next_x >= len(grid[0]) or next_y >= len(grid)
|
||||
):
|
||||
continue
|
||||
if grid[next_y][next_x] == 1 and not visited[next_y][next_x]:
|
||||
visited[next_y][next_x] = True
|
||||
dfs(grid, next_y, next_x)
|
||||
|
||||
|
||||
# 讀取輸入值
|
||||
n, m = map(int, input().split())
|
||||
grid = []
|
||||
visited = [[False] * m for _ in range(n)]
|
||||
|
||||
for i in range(n):
|
||||
grid.append(list(map(int, input().split())))
|
||||
|
||||
# 處理邊界
|
||||
for j in range(m):
|
||||
# 上邊界
|
||||
if grid[0][j] == 1 and not visited[0][j]:
|
||||
visited[0][j] = True
|
||||
dfs(grid, 0, j)
|
||||
# 下邊界
|
||||
if grid[n - 1][j] == 1 and not visited[n - 1][j]:
|
||||
visited[n - 1][j] = True
|
||||
dfs(grid, n - 1, j)
|
||||
|
||||
for i in range(n):
|
||||
# 左邊界
|
||||
if grid[i][0] == 1 and not visited[i][0]:
|
||||
visited[i][0] = True
|
||||
dfs(grid, i, 0)
|
||||
# 右邊界
|
||||
if grid[i][m - 1] == 1 and not visited[i][m - 1]:
|
||||
visited[i][m - 1] = True
|
||||
dfs(grid, i, m - 1)
|
||||
|
||||
# 計算孤島總面積
|
||||
result = 0 # 初始化,避免使用到處理邊界時所產生的累加值
|
||||
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if grid[i][j] == 1 and not visited[i][j]:
|
||||
visited[i][j] = True
|
||||
dfs(grid, i, j)
|
||||
|
||||
# 輸出孤島的總面積
|
||||
print(result)
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
``` go
|
||||
|
@ -178,6 +178,45 @@ int main() {
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
father = list()
|
||||
|
||||
def find(u):
|
||||
if u == father[u]:
|
||||
return u
|
||||
else:
|
||||
father[u] = find(father[u])
|
||||
return father[u]
|
||||
|
||||
def is_same(u, v):
|
||||
u = find(u)
|
||||
v = find(v)
|
||||
return u == v
|
||||
|
||||
def join(u, v):
|
||||
u = find(u)
|
||||
v = find(v)
|
||||
if u != v:
|
||||
father[u] = v
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 輸入
|
||||
n = int(input())
|
||||
for i in range(n + 1):
|
||||
father.append(i)
|
||||
# 尋找冗余邊
|
||||
result = None
|
||||
for i in range(n):
|
||||
s, t = map(int, input().split())
|
||||
if is_same(s, t):
|
||||
result = str(s) + ' ' + str(t)
|
||||
else:
|
||||
join(s, t)
|
||||
|
||||
# 輸出
|
||||
print(result)
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
@ -351,6 +351,92 @@ public class Main {
|
||||
```
|
||||
### Python
|
||||
|
||||
```python
|
||||
from collections import defaultdict
|
||||
|
||||
father = list()
|
||||
|
||||
|
||||
def find(u):
|
||||
if u == father[u]:
|
||||
return u
|
||||
else:
|
||||
father[u] = find(father[u])
|
||||
return father[u]
|
||||
|
||||
|
||||
def is_same(u, v):
|
||||
u = find(u)
|
||||
v = find(v)
|
||||
return u == v
|
||||
|
||||
|
||||
def join(u, v):
|
||||
u = find(u)
|
||||
v = find(v)
|
||||
if u != v:
|
||||
father[u] = v
|
||||
|
||||
|
||||
def is_tree_after_remove_edge(edges, edge, n):
|
||||
# 初始化并查集
|
||||
global father
|
||||
father = [i for i in range(n + 1)]
|
||||
|
||||
for i in range(len(edges)):
|
||||
if i == edge:
|
||||
continue
|
||||
s, t = edges[i]
|
||||
if is_same(s, t): # 成環,即不是有向樹
|
||||
return False
|
||||
else: # 將s,t放入集合中
|
||||
join(s, t)
|
||||
return True
|
||||
|
||||
|
||||
def get_remove_edge(edges):
|
||||
# 初始化并查集
|
||||
global father
|
||||
father = [i for i in range(n + 1)]
|
||||
|
||||
for s, t in edges:
|
||||
if is_same(s, t):
|
||||
print(s, t)
|
||||
return
|
||||
else:
|
||||
join(s, t)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 輸入
|
||||
n = int(input())
|
||||
edges = list()
|
||||
in_degree = defaultdict(int)
|
||||
|
||||
for i in range(n):
|
||||
s, t = map(int, input().split())
|
||||
in_degree[t] += 1
|
||||
edges.append([s, t])
|
||||
|
||||
# 尋找入度為2的邊,並紀錄其下標(index)
|
||||
vec = list()
|
||||
for i in range(n - 1, -1, -1):
|
||||
if in_degree[edges[i][1]] == 2:
|
||||
vec.append(i)
|
||||
|
||||
# 輸出
|
||||
if len(vec) > 0:
|
||||
# 情況一:刪除輸出順序靠後的邊
|
||||
if is_tree_after_remove_edge(edges, vec[0], n):
|
||||
print(edges[vec[0]][0], edges[vec[0]][1])
|
||||
# 情況二:只能刪除特定的邊
|
||||
else:
|
||||
print(edges[vec[1]][0], edges[vec[1]][1])
|
||||
else:
|
||||
# 情況三: 原圖有環
|
||||
get_remove_edge(edges)
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
Reference in New Issue
Block a user