Skip to content

Commit 94e47c2

Browse files
committed
add graph
1 parent afec3d4 commit 94e47c2

File tree

27 files changed

+1034
-84
lines changed

27 files changed

+1034
-84
lines changed

01-matrix/main.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
var (
4+
Dirs = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
5+
)
6+
7+
func maxAreaOfIsland(grid [][]int) int {
8+
area := 0
9+
rows := len(grid)
10+
if rows == 0 {
11+
return 0
12+
}
13+
cols := len(grid[0])
14+
visited := make([][]bool, rows)
15+
for v := range visited {
16+
visited[v] = make([]bool, cols)
17+
}
18+
for i := 0; i < rows; i++ {
19+
for j := 0; j < cols; j++ {
20+
if (grid[i][j] == 1) && !visited[i][j] {
21+
if a := getAreaDFS(grid, visited, i, j, rows, cols); a > area {
22+
area = a
23+
}
24+
}
25+
}
26+
}
27+
return area
28+
}
29+
30+
func getArea(grid [][]int, visited [][]bool, i, j, rows, cols int) int {
31+
var pos [2]int
32+
stack := [][2]int{{i, j}}
33+
visited[i][j] = true
34+
area := 0
35+
for {
36+
if len(stack) == 0 {
37+
break
38+
}
39+
area++
40+
pos, stack = stack[0], stack[1:]
41+
for _, d := range Dirs {
42+
r := pos[0] + d[0]
43+
c := pos[1] + d[1]
44+
if 0 <= r && r < rows && 0 <= c &&
45+
c < cols && grid[r][c] == 1 &&
46+
!visited[r][c] {
47+
stack = append(stack, [2]int{r, c})
48+
visited[r][c] = true
49+
}
50+
}
51+
}
52+
return area
53+
}
54+
55+
func getAreaDFS(grid [][]int, visited [][]bool, i, j, rows, cols int) int {
56+
var area int
57+
if 0 <= i && i < rows && 0 <= j &&
58+
j < cols && grid[i][j] == 1 &&
59+
!visited[i][j] {
60+
area = 1
61+
} else {
62+
return 0
63+
}
64+
visited[i][j] = true
65+
for _, d := range Dirs {
66+
area += getAreaDFS(grid, visited, i+d[0], j+d[1], rows, cols)
67+
}
68+
return area
69+
}

01-matrix/main.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
import unittest
3+
from typing import List, Tuple
4+
5+
6+
"""
7+
https://leetcode.com/problems/01-matrix/
8+
给定一个由 0 和 1 组成的矩阵 mat ,请输出一个大小相同的矩阵,其中每一个格子是 mat 中对应位置元素到最近的 0 的距离。
9+
10+
两个相邻元素间的距离为 1 。
11+
12+
示例 1:
13+
14+
输入:mat = [[0,0,0],[0,1,0],[0,0,0]]
15+
输出:[[0,0,0],[0,1,0],[0,0,0]]
16+
示例 2:
17+
18+
输入:mat = [[0,0,0],[0,1,0],[1,1,1]]
19+
输出:[[0,0,0],[0,1,0],[1,2,1]]
20+
"""
21+
Dirs = ((-1, 0), (1, 0), (0, -1), (0, 1))
22+
23+
class Solution:
24+
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
25+
rows = len(mat)
26+
if not rows:
27+
return []
28+
cols = len(mat[0])
29+
dists = [[0] * cols for _ in range(rows)]
30+
stack: List[Tuple[int, int]] = []
31+
for i in range(rows):
32+
for j in range(cols):
33+
if mat[i][j] == 0:
34+
dists[i][j] = 0
35+
stack.append((i, j))
36+
else:
37+
dists[i][j] = -1
38+
while stack:
39+
i, j= stack.pop()
40+
dist = dists[i][j]
41+
for d in Dirs:
42+
r = i+
43+
return []
44+
45+
46+
cases = [
47+
{
48+
"input": [[[0, 0, 0], [0, 1, 0], [0, 0, 0]]],
49+
"output": [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
50+
},
51+
{
52+
"input": [[[0, 0, 0], [0, 1, 0], [1, 1, 1]]],
53+
"output": [[0, 0, 0], [0, 1, 0], [1, 2, 1]],
54+
},
55+
]
56+
57+
58+
class SolutionTestCase(unittest.TestCase):
59+
def test(self):
60+
for t in cases:
61+
print(f"input: {t['input']}\noutput: {t['output']}\n")
62+
ret = Solution().updateMatrix(*t["input"])
63+
self.assertListEqual(ret, t["output"])
64+
65+
66+
if __name__ == "__main__":
67+
unittest.main()

01-matrix/main_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"testing"
9+
)
10+
11+
func Test_maxAreaOfIsland(t *testing.T) {
12+
type args struct {
13+
grid [][]int
14+
}
15+
tests := []struct {
16+
args args
17+
want int
18+
}{
19+
{args{[][]int{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0},
20+
{args{[][]int{
21+
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
22+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
23+
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
24+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
25+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
26+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
27+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
28+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
29+
}}, 6},
30+
}
31+
for i, tt := range tests {
32+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
33+
if got := maxAreaOfIsland(tt.args.grid); !assert.Equal(t, tt.want, got) {
34+
t.Errorf("main() = %v, want %v", got, tt.want)
35+
}
36+
})
37+
}
38+
}

alien-dictionary/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "math/rand"
4+
5+
func findKthLargest(arr []int, k int) int {
6+
pivot := arr[rand.Int()%(len(arr)-1)]
7+
var larger []int
8+
var equal []int
9+
var less []int
10+
for _, x := range arr {
11+
if x > arr[pivot] {
12+
larger = append(larger, x)
13+
} else if x == arr[pivot] {
14+
equal = append(equal, x)
15+
} else {
16+
less = append(less, x)
17+
}
18+
}
19+
if len(larger) >= k {
20+
return findKthLargest(larger, k)
21+
} else if len(larger)+len(equal) >= k {
22+
return arr[pivot]
23+
} else {
24+
return findKthLargest(append(equal, less...), k-len(larger))
25+
}
26+
}

alien-dictionary/main.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
import unittest
3+
from typing import List, Dict
4+
import random
5+
6+
7+
"""
8+
https://leetcode-cn.com/problems/alien-dictionary/
9+
现有一种使用英语字母的外星文语言,这门语言的字母顺序与英语顺序不同。
10+
11+
给定一个字符串列表 words ,作为这门语言的词典,words 中的字符串已经 按这门新语言的字母顺序进行了排序 。
12+
13+
请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回 "" 。若存在多种可能的合法字母顺序,返回其中 任意一种 顺序即可。
14+
15+
字符串 s 字典顺序小于 字符串 t 有两种情况:
16+
17+
在第一个不同字母处,如果 s 中的字母在这门外星语言的字母顺序中位于 t 中字母之前,那么 s 的字典顺序小于 t 。
18+
如果前面 min(s.length, t.length) 字母都相同,那么 s.length < t.length 时,s 的字典顺序也小于 t 。
19+
 
20+
示例 1:
21+
22+
输入:words = ["wrt","wrf","er","ett","rftt"]
23+
输出:"wertf"
24+
示例 2:
25+
26+
输入:words = ["z","x"]
27+
输出:"zx"
28+
示例 3:
29+
30+
输入:words = ["z","x","z"]
31+
输出:""
32+
解释:不存在合法字母顺序,因此返回 "" 。
33+
"""
34+
35+
36+
class Solution:
37+
def alienOrder(self, words: List[str]) -> str:
38+
indegrees: Dict[str, int] = {}
39+
graph: Dict[int, List[int]] = {}
40+
queue = []
41+
path = []
42+
for word in words:
43+
for s in word:
44+
45+
return ""
46+
47+
48+
cases = [
49+
{
50+
"input": [["wrt", "wrf", "er", "ett", "rftt"]],
51+
"output": "werft",
52+
},
53+
{
54+
"input": [["z", "x"]],
55+
"output": "zx",
56+
},
57+
{
58+
"input": [["z", "x", "z"]],
59+
"output": "",
60+
},
61+
]
62+
63+
64+
class SolutionTestCase(unittest.TestCase):
65+
def test(self):
66+
for t in cases:
67+
print(f"input: {t['input']}\noutput: {t['output']}\n")
68+
ret = Solution().alienOrder(*t["input"])
69+
self.assertEqual(ret, t["output"])
70+
71+
72+
if __name__ == "__main__":
73+
unittest.main()

alien-dictionary/main_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"testing"
9+
)
10+
11+
func Test_findKthLargest(t *testing.T) {
12+
type args struct {
13+
arr []int
14+
k int
15+
}
16+
tests := []struct {
17+
args args
18+
want int
19+
}{
20+
{args{[]int{3, 2, 1, 5, 6, 4}, 2}, 5},
21+
{args{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6}, 4}, 4},
22+
}
23+
for i, tt := range tests {
24+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
25+
if got := findKthLargest(tt.args.arr, tt.args.k); !assert.Equal(t, tt.want, got) {
26+
t.Errorf("singleNonDuplicate() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}

combination-sum/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "math/rand"
4+
5+
func findKthLargest(arr []int, k int) int {
6+
pivot := arr[rand.Int()%(len(arr)-1)]
7+
var larger []int
8+
var equal []int
9+
var less []int
10+
for _, x := range arr {
11+
if x > arr[pivot] {
12+
larger = append(larger, x)
13+
} else if x == arr[pivot] {
14+
equal = append(equal, x)
15+
} else {
16+
less = append(less, x)
17+
}
18+
}
19+
if len(larger) >= k {
20+
return findKthLargest(larger, k)
21+
} else if len(larger)+len(equal) >= k {
22+
return arr[pivot]
23+
} else {
24+
return findKthLargest(append(equal, less...), k-len(larger))
25+
}
26+
}

combination-sum/main.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
import unittest
3+
from typing import List
4+
5+
6+
"""
7+
https://leetcode.com/problems/combination-sum/
8+
给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 
9+
的唯一组合。
10+
11+
candidates 中的数字可以无限制重复被选取。如果至少一个所选数字数量不同,则两种组合是唯一的。 
12+
13+
对于给定的输入,保证和为 target 的唯一组合数少于 150 个。
14+
15+
示例 1:
16+
17+
输入: candidates = [2,3,6,7], target = 7
18+
输出: [[7],[2,2,3]]
19+
示例 2:
20+
21+
输入: candidates = [2,3,5], target = 8
22+
输出: [[2,2,2,2],[2,3,3],[3,5]]
23+
示例 3:
24+
25+
输入: candidates = [2], target = 1
26+
输出: []
27+
示例 4:
28+
29+
输入: candidates = [1], target = 1
30+
输出: [[1]]
31+
示例 5:
32+
33+
输入: candidates = [1], target = 2
34+
输出: [[1,1]]
35+
"""
36+
37+
38+
class Solution:
39+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
40+
return [[7], [2, 2, 3]]
41+
42+
43+
cases = [
44+
{
45+
"input": [[2, 3, 6, 7], 7],
46+
"output": [[7], [2, 2, 3]],
47+
},
48+
]
49+
50+
51+
class SolutionTestCase(unittest.TestCase):
52+
def test(self):
53+
for t in cases:
54+
print(f"input: {t['input']}\noutput: {t['output']}\n")
55+
ret = Solution().combinationSum(*t["input"])
56+
self.assertListEqual(ret, t["output"])
57+
58+
59+
if __name__ == "__main__":
60+
unittest.main()

0 commit comments

Comments
 (0)