Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Week_03/id_29/Leetcode_104_29.go
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 29 additions & 0 deletions Week_03/id_29/Leetcode_111_29.go
Original file line number Diff line number Diff line change
@@ -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
}
}
46 changes: 46 additions & 0 deletions Week_03/id_29/Leetcode_200_29.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions Week_03/id_29/Leetcode_210_29.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions Week_03/id_29/Leetcode_703_29.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions Week_03/id_29/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func main() {

}

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}