Leetcode Note: Go - Time Needed to Buy Tickets

Time Needed to Buy Tickets - LeetCode
https://leetcode.com/problems/time-needed-to-buy-tickets/

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

回答

Go - Simple Solution using Queue | 60% Faster | 70% less memory needed - Time Needed to Buy Tickets - LeetCode
https://leetcode.com/problems/time-needed-to-buy-tickets/solutions/2458459/go-simple-solution-using-queue-60-faster-70-less-memory-needed/

func timeRequiredToBuy(tickets []int, k int) int {
    out := 0
    endMainLoop := false

    for !endMainLoop {

        end := len(tickets)

        if tickets[k] == 1 {
            end = k + 1
            endMainLoop = true
        }

        for i := 0; i < end; i++ {

            elem := tickets[0]

            if elem > 0 {
                elem--
                out++
            }

            tickets = append(tickets[1:], elem)
        }
    }

    return out
}