在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ There are We have a list of flight bookings. The Return an array Example 1: Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 Output: [10,55,45,25,25] Constraints:
这里有 我们这儿有一份航班预订表,表中第 请你返回一个长度为 示例: 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 输出:[10,55,45,25,25] 提示:
1224ms
1 class Solution { 2 func corpFlightBookings(_ bookings: [[Int]], _ n: Int) -> [Int] { 3 var solution = Array(repeating: 0, count: n) 4 5 for booking in bookings { 6 solution[booking[0]-1] += booking[2] 7 if booking[1] < n { solution[booking[1]] -= booking[2] } 8 } 9 10 for i in 1..<n { 11 solution[i] += solution[i-1] 12 } 13 14 return solution 15 } 16 } 1240ms 1 class Solution { 2 func corpFlightBookings(_ bookings: [[Int]], _ n: Int) -> [Int] { 3 var diff = [Int](repeating: 0, count: n+1) 4 for book in bookings { 5 let start = book[0] 6 let end = book[1] 7 let count = book[2] 8 diff[start-1] += count 9 diff[end] -= count 10 } 11 12 var result = [Int](repeating: 0, count: n) 13 for i in 0..<n { 14 result[i] = (i - 1 >= 0 ? result[i-1] : 0) + diff[i] 15 } 16 return result 17 } 18 } 1312ms 1 class Solution { 2 func corpFlightBookings(_ bookings: [[Int]], _ n: Int) -> [Int] { 3 var line = [Int](repeating: 0, count: n + 2) 4 for b in bookings { 5 line[b[0]] += b[2] 6 line[b[1] + 1] -= b[2] 7 } 8 var res = [Int]() 9 var r = 0 10 for i in 1...n { 11 r += line[i] 12 res.append(r) 13 } 14 return res 15 } 16 } Runtime: 1336 ms Memory Usage: 24.2 MB
1 class Solution { 2 func corpFlightBookings(_ bookings: [[Int]], _ n: Int) -> [Int] { 3 var res:[Int] = [Int](repeating:0,count:n) 4 for b in bookings 5 { 6 res[b[0] - 1] += b[2] 7 if b[1] < n 8 { 9 res[b[1]] -= b[2] 10 } 11 } 12 13 for i in 1..<n 14 { 15 res[i] += res[i - 1] 16 } 17 return res 18 } 19 }
|
请发表评论