diff --git a/Week_03/id_29/Leetcode_104_29.go b/Week_03/id_29/Leetcode_104_29.go new file mode 100644 index 00000000..dfa20501 --- /dev/null +++ b/Week_03/id_29/Leetcode_104_29.go @@ -0,0 +1,14 @@ +package main + +func maxDepth(root *TreeNode) int { + if root == nil { + return 0 + } + leftDepth := maxDepth(root.Left) + rightDepth := maxDepth(root.Right) + if leftDepth > rightDepth { + return leftDepth + 1 + } else { + return rightDepth + 1 + } +} diff --git a/Week_03/id_29/Leetcode_111_29.go b/Week_03/id_29/Leetcode_111_29.go new file mode 100644 index 00000000..dc623734 --- /dev/null +++ b/Week_03/id_29/Leetcode_111_29.go @@ -0,0 +1,29 @@ +package main + +func minDepth(root *TreeNode) int { + if root == nil { + return 0 + } + left := minDepth(root.Left) + right := minDepth(root.Right) + if left == 0 || right == 0 { + return 1 + max(left, right) + } + return 1 + min(left, right) +} + +func min(x, y int) int { + if x >= y { + return y + } else { + return x + } +} + +func max(x, y int) int { + if x <= y { + return y + } else { + return x + } +} diff --git a/Week_03/id_29/Leetcode_200_29.go b/Week_03/id_29/Leetcode_200_29.go new file mode 100644 index 00000000..2505bea3 --- /dev/null +++ b/Week_03/id_29/Leetcode_200_29.go @@ -0,0 +1,46 @@ +package main + +func numIslands(grid [][]byte) int { + color := 1 + colors := make([][]int, len(grid)) + for x := range colors { + colors[x] = make([]int, len(grid[x])) + } + + for x := range grid { + for y := range grid[x] { + marked := markColor(x, y, grid, colors, color) + if marked { + color++ + } + } + } + + return color - 1 +} + +func markColor(x, y int, grid [][]byte, colors [][]int, color int) bool { + if x < 0 || x >= len(grid) { + return false + } + + if y < 0 || y >= len(grid[x]) { + return false + } + + if colors[x][y] != 0 { + return false + } + + if grid[x][y] == '0' { + colors[x][y] = -1 + return false + } + + colors[x][y] = color + markColor(x+1, y, grid, colors, color) + markColor(x-1, y, grid, colors, color) + markColor(x, y-1, grid, colors, color) + markColor(x, y+1, grid, colors, color) + return true +} diff --git a/Week_03/id_29/Leetcode_210_29.go b/Week_03/id_29/Leetcode_210_29.go new file mode 100644 index 00000000..cfd33c36 --- /dev/null +++ b/Week_03/id_29/Leetcode_210_29.go @@ -0,0 +1,37 @@ +package main + +func findOrder(numCourses int, prerequisites [][]int) []int { + + queue := make([]int, 0, numCourses) + inDegree := make([]int, numCourses, numCourses) + graph := make(map[int][]int) + + for _, edge := range prerequisites { + from, to := edge[1], edge[0] + inDegree[to]++ + graph[from] = append(graph[from], to) + } + + for i, n := range inDegree { + if n == 0 { + queue = append(queue, i) + } + } + + for i := 0; i < numCourses; i++ { + if i >= len(queue) { + return []int{} + } + course := queue[i] + nextCourses := graph[course] + + for _, next := range nextCourses { + inDegree[next]-- + if inDegree[next] == 0 { + queue = append(queue, next) + } + } + } + + return queue +} diff --git a/Week_03/id_29/Leetcode_703_29.go b/Week_03/id_29/Leetcode_703_29.go new file mode 100644 index 00000000..f19bab33 --- /dev/null +++ b/Week_03/id_29/Leetcode_703_29.go @@ -0,0 +1,57 @@ +package main + +import "container/heap" + +type IntHeap []int + +type KthLargest struct { + k int + heap intHeap +} + +func Constructor(k int, nums []int) KthLargest { + h := intHeap(nums) + heap.Init(&h) + + for len(h) > k { + heap.Pop(&h) + } + + return KthLargest{ + k: k, + heap: h, + } +} + +func (kl *KthLargest) Add(val int) int { + heap.Push(&kl.heap, val) + + if len(kl.heap) > kl.k { + heap.Pop(&kl.heap) + } + + return kl.heap[0] +} + +type intHeap []int + +func (h intHeap) Len() int { + return len(h) +} + +func (h intHeap) Less(i, j int) bool { + return h[i] < h[j] +} + +func (h intHeap) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} +func (h *intHeap) Push(x interface{}) { + *h = append(*h, x.(int)) +} + +func (h *intHeap) Pop() interface{} { + res := (*h)[len(*h)-1] + *h = (*h)[:len(*h)-1] + return res +} diff --git a/Week_03/id_29/main.go b/Week_03/id_29/main.go new file mode 100644 index 00000000..216deaa0 --- /dev/null +++ b/Week_03/id_29/main.go @@ -0,0 +1,11 @@ +package main + +func main() { + +} + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +}