add new solutions
This commit is contained in:
parent
9c2c959a9b
commit
9a10695e8c
29 changed files with 1074 additions and 2 deletions
37
solutions/2/q207/solution.go
Normal file
37
solutions/2/q207/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q207
|
||||
|
||||
func canFinish(numCourses int, prerequisites [][]int) bool {
|
||||
nDeps := make([]int, numCourses)
|
||||
revDeps := make([][]int, numCourses)
|
||||
|
||||
for i := range prerequisites {
|
||||
course, prereq := prerequisites[i][0], prerequisites[i][1]
|
||||
nDeps[course]++
|
||||
revDeps[prereq] = append(revDeps[prereq], course)
|
||||
}
|
||||
|
||||
queue := []int{}
|
||||
for i := range numCourses {
|
||||
if nDeps[i] == 0 && len(revDeps) > 0 {
|
||||
queue = append(queue, i)
|
||||
}
|
||||
}
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
course := queue[0]
|
||||
for _, r := range revDeps[course] {
|
||||
nDeps[r]--
|
||||
if nDeps[r] == 0 {
|
||||
queue = append(queue, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range nDeps {
|
||||
if n > 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = canFinish
|
||||
89
solutions/2/q212/solution.go
Normal file
89
solutions/2/q212/solution.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package q212
|
||||
|
||||
type TrieNode struct {
|
||||
children [26]*TrieNode
|
||||
word *string
|
||||
}
|
||||
|
||||
func trieIdx(char byte) int8 {
|
||||
if char < 'a' || char > 'z' {
|
||||
return -1
|
||||
}
|
||||
return int8(char - 'a')
|
||||
}
|
||||
|
||||
func allocator(bulkSize int) func() *TrieNode {
|
||||
var v []TrieNode
|
||||
p := 0
|
||||
|
||||
return func() *TrieNode {
|
||||
if v == nil || p == bulkSize {
|
||||
v = make([]TrieNode, bulkSize)
|
||||
p = 0
|
||||
}
|
||||
p++
|
||||
return &v[p-1]
|
||||
}
|
||||
}
|
||||
|
||||
func buildTrie(words []string) *TrieNode {
|
||||
alloc := allocator(128)
|
||||
root := alloc()
|
||||
for _, word := range words {
|
||||
p := root
|
||||
for i := range len(word) {
|
||||
idx := trieIdx(word[i])
|
||||
if idx < 0 {
|
||||
continue
|
||||
}
|
||||
if p.children[idx] == nil {
|
||||
p.children[idx] = alloc()
|
||||
}
|
||||
p = p.children[idx]
|
||||
}
|
||||
p.word = &word
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
type Coord [2]int8
|
||||
|
||||
func find(board [][]byte, coord Coord, trie *TrieNode, found *[]string) {
|
||||
if coord[0] < 0 || coord[1] < 0 ||
|
||||
int(coord[0]) >= len(board) || int(coord[1]) >= len(board[0]) {
|
||||
return
|
||||
}
|
||||
c := board[coord[0]][coord[1]]
|
||||
idx := trieIdx(c)
|
||||
if idx == -1 || trie.children[idx] == nil {
|
||||
return
|
||||
}
|
||||
// Mark visited
|
||||
board[coord[0]][coord[1]] = '.'
|
||||
|
||||
trie = trie.children[idx]
|
||||
if trie.word != nil {
|
||||
*found = append(*found, *trie.word)
|
||||
trie.word = nil // dedup
|
||||
}
|
||||
|
||||
find(board, Coord{coord[0] + 1, coord[1]}, trie, found)
|
||||
find(board, Coord{coord[0] - 1, coord[1]}, trie, found)
|
||||
find(board, Coord{coord[0], coord[1] + 1}, trie, found)
|
||||
find(board, Coord{coord[0], coord[1] - 1}, trie, found)
|
||||
board[coord[0]][coord[1]] = c
|
||||
}
|
||||
|
||||
func findWords(board [][]byte, words []string) []string {
|
||||
trie := buildTrie(words)
|
||||
found := []string{}
|
||||
|
||||
for row := range board {
|
||||
for col := range board[0] {
|
||||
find(board, Coord{int8(row), int8(col)}, trie, &found)
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
var _ = findWords
|
||||
26
solutions/2/q221/solution.go
Normal file
26
solutions/2/q221/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q221
|
||||
|
||||
func maximalSquare(matrix [][]byte) int {
|
||||
w, h := len(matrix[0]), len(matrix)
|
||||
|
||||
maxSq := make([]int16, w+h)
|
||||
maxV := make([]int16, w)
|
||||
ret := 0
|
||||
for y := range h {
|
||||
var maxH int16
|
||||
for x := range w {
|
||||
if matrix[y][x] == '0' {
|
||||
maxH, maxV[x], maxSq[x-y+h] = 0, 0, 0
|
||||
continue
|
||||
}
|
||||
|
||||
maxH++
|
||||
maxV[x]++
|
||||
maxSq[x-y+h] = min(maxH, maxV[x], maxSq[x-y+h]+1)
|
||||
ret = max(ret, int(maxSq[x-y+h]))
|
||||
}
|
||||
}
|
||||
return ret * ret
|
||||
}
|
||||
|
||||
var _ = maximalSquare
|
||||
35
solutions/2/q230/solution.go
Normal file
35
solutions/2/q230/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q230
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func findKth(node *TreeNode, k, current int) (int, *int) {
|
||||
if node == nil {
|
||||
return current, nil
|
||||
}
|
||||
|
||||
current, ret := findKth(node.Left, k, current)
|
||||
if ret != nil {
|
||||
return current, ret
|
||||
}
|
||||
|
||||
current += 1
|
||||
if current == k {
|
||||
return current, &node.Val
|
||||
}
|
||||
|
||||
return findKth(node.Right, k, current)
|
||||
}
|
||||
|
||||
func kthSmallest(root *TreeNode, k int) int {
|
||||
_, kth := findKth(root, k, 0)
|
||||
if kth == nil {
|
||||
return 0
|
||||
}
|
||||
return *kth
|
||||
}
|
||||
|
||||
var _ = kthSmallest
|
||||
Loading…
Add table
Add a link
Reference in a new issue