Day 23 of LeetCode

Day 23 of LeetCode

ยท

2 min read

Documenting LeetCode solving.

Q71

215. Kth Largest Element in an Array

Medium.

type MinHeap []int

func (h MinHeap) Len() int {return len(h)}
func (h MinHeap) Less(i, j int) bool {return h[i] < h[j]}
func (h MinHeap) Swap(i, j int) {h[i], h[j] = h[j], h[i]}
func (h MinHeap) Peek() int {return h[0]}
func (h *MinHeap) Push(x interface{}) {*h = append(*h, x.(int))}
func (h *MinHeap) Pop() interface{} {
    x := (*h)[len(*h) - 1]
    *h = (*h)[:len(*h) - 1]
    return x
}


func findKthLargest(nums []int, k int) int {
    minh := &MinHeap{}
    heap.Init(minh)
    for _, num := range nums {
        heap.Push(minh, num)
    }

    for minh.Len() > k {
        heap.Pop(minh)
    }

    return minh.Peek()
}

Q72

2192. All Ancestors of a Node in a Directed Acyclic Graph

Medium. DAG

BFS

func getAncestors(n int, edges [][]int) [][]int {
    graph := make([][]int, n)
    res := make([][]int, n)

    for _, e := range edges {
        graph[e[0]] = append(graph[e[0]], e[1])
    }

    for i := 0; i < n; i++ {
        bfs(i, graph, res)
    }

    return res
}

func bfs(a int, graph [][]int, res [][]int) {
    queue := make([]int, 0)
    seen := map[int]bool{}
    seen[a] = true

    queue = append(queue, a)

    for len(queue) > 0 {
        c := queue[0]
        queue = queue[1:]

        for _, v := range graph[c] {
            if !seen[v] {
                seen[v] = true
                queue = append(queue, v)
                res[v] = append(res[v], a)
            }
        }

    }
}

Q73

1857. Largest Color Value in a Directed Graph

DFS

func maxCount(counts [26]int) int {
    maxVal := 0
    for _, count := range counts {
        maxVal = max(maxVal, count)
    }
    return maxVal
}

func largestPathValue(colors string, edges [][]int) int {
    // Different graph stucture (map) because the size is unknown
    graph := make(map[int][]int)
    for _, e := range edges {
        graph[e[0]] = append(graph[e[0]], e[1])
    }

    n, res := len(colors), 0
    visit := make(map[int]bool)
    // Track cycle
    path := make(map[int]bool)
    count := make([][26]int, n)

    var dfs func(node int) int
    dfs = func(node int) int {
        if path[node] == true {
            return math.MaxInt32
        }
        if visit[node] == true {
            return 0
        }
        visit[node] = true
        path[node] = true

        colorIndex := int(colors[node]) - int('a')
        count[node][colorIndex] = 1

        for _, nei := range graph[node] {
            if dfs(nei) == math.MaxInt32 {
                return math.MaxInt32
            }
            for c := 0; c < 26; c++ {
                if c == colorIndex {
                    count[node][c] = max(count[node][c], 1 + count[nei][c])
                } else {
                    count[node][c] = max(count[node][c], count[nei][c])
                }
            }
        }

        path[node] = false
        return maxCount(count[node])
    }

    for i := 0; i < n; i++ {
        res = max(res, dfs(i))
    }

    if res == math.MaxInt32 {
        return -1
    } else {
        return res
    }

}

Did you find this article valuable?

Support ๐Ÿฐ Evelyn's Learning Journey by becoming a sponsor. Any amount is appreciated!

ย