[Go Version] Algorithm Clearance Village Level 17 Bronze - It turns out that greed is so simple

What is greedy algorithm

Greedy algorithm (greedy algorithm) means: when solving a problem, the best or optimal (i.e., the most advantageous) choice is taken in every step of the choice, so as to hope that the result will be the best or optimal algorithm.
The results obtained by the greedy algorithm may not necessarily be the optimal result (sometimes it will be the optimal solution), but they are all relatively approximate (close to) the optimal solution.

Topic: Distributing cookies

Question link: LeetCode-455. Distributing cookies
Insert image description here

Idea analysis: Sort appetite and biscuit size, traverse the appetite value in reverse order, if the largest biscuit is satisfied, it will be +1, if not, the appetite value will be eliminated.

Go code

func findContentChildren(g []int, s []int) int {
    
    
    sort.Ints(g)
    sort.Ints(s)
    num := 0
    leng := len(g)
    lens := len(s)
    if lens == 0 || leng == 0 {
    
    
        return 0
    }
    for i:=leng-1;i>=0;i-- {
    
    
        if g[i] <= s[lens-1] {
    
    
            lens--
            num++
        }
        if lens == 0 {
    
    
            break
        }
    }
    return num
}

Topic: Change for Lemonade

Question link: LeetCode-860. Lemonade change
Insert image description here

Idea analysis: Count 5/10 dollars, determine that the current value is 5/10/20, increase or decrease the count, and determine that the count of 5 < 0 will fail, otherwise the change will be successful

Go code

func lemonadeChange(bills []int) bool {
    
    
    bill5 := 0
    bill10 := 0
    for _, v := range bills {
    
    
        if v == 5 {
    
    
            bill5++
        }
        if v == 10 {
    
    
            bill5--
            bill10++
        }
        if v == 20 {
    
    
            if bill10 > 0 {
    
    
                bill10--
                bill5--
            } else {
    
    
                bill5 -= 3
            }
        }
        if bill5 < 0 {
    
    
            return false
        }
    }
    return true
}

Topic: Distributing candy

Question link: LeetCode-135. Distribute candy
Insert image description here

Idea analysis: traverse the score in forward order, the first one = 1, if the last one is greater than the previous one, +1, otherwise = 1, then traverse in reverse order, if the first one is greater than the last one, +1 and keep the maximum value of the old and new values.

Go code

func candy(ratings []int) int {
    
    
    length := len(ratings)
    if length == 0 {
    
    
        return 0
    }
    arr := make([]int, length)
    arr[0] = 1
    // 正序遍历分发一次
    for i:=1; i<=length-1; i++ {
    
    
        if ratings[i] > ratings[i-1] {
    
    
            arr[i] = arr[i-1]+1
        } else {
    
    
            arr[i] = 1
        }
    }
    sum := arr[length-1]
    // 逆序遍历分发一次
    for i:=length-2; i>=0; i-- {
    
    
        if ratings[i] > ratings[i+1] {
    
    
            arr[i] = GetMax(arr[i], arr[i+1]+1)
        }
        sum += arr[i]
    }
    return sum
}

func GetMax(a, b int) int {
    
    
    if a>b {
    
    
        return a
    }
    return b
}

Guess you like

Origin blog.csdn.net/trinityleo5/article/details/133936717