Leetcode Note: Go - Calculate Amount Paid in Taxes

Calculate Amount Paid in Taxes - LeetCode
https://leetcode.com/problems/calculate-amount-paid-in-taxes/

  • Go 言語で取り組んだメモ

回答

go solution - Calculate Amount Paid in Taxes - LeetCode
https://leetcode.com/problems/calculate-amount-paid-in-taxes/solutions/2141995/go-solution/

func calculateTax(brackets [][]int, income int) float64 {
    result := float64(0)
    lastTax := 0
    remain := income
    for _, bracket := range brackets {
        if bracket[0] < income {
            result += float64(bracket[0] - lastTax) * float64(bracket[1]) / float64(100)
            lastTax = bracket[0]
            remain = income - bracket[0]
            continue
        }
        if income > lastTax {
            result += float64(remain) * float64(bracket[1]) / float64(100)
            lastTax = bracket[0]
        } else {
            break
        }
    }
    return result
}