Leetcode - Check If N and Its Double Exist
Leetcode - Divide Array in Sets of K Consecutive Numbers

Leetcode - Corporate Flight Bookings

violet posted @ Apr 02, 2020 06:58:08 AM in 算法 with tags Algorithm Golang array count sort , 237 阅读

https://leetcode.com/problems/corporate-flight-bookings/

There are n flights, and they are labeled from 1 to n.

We have a list of flight bookings.  The i-th booking bookings[i] = [i, j, k] means that we booked k seats from flights labeled i to j inclusive.

Return an array answer of length n, representing the number of seats booked on each flight in order of their label.

 

Example 1:

Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output: [10,55,45,25,25]

 

Typical count sort problem.

func corpFlightBookings(bookings [][]int, n int) []int {
    count := make([]int, n+1)
   
    for i := 0; i < len(bookings); i++ {
        for j := bookings[i][0]; j <= bookings[i][1]; j++ {
            count[j] += bookings[i][2]
        }
    }
    
    return count[1:]
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter