Leetcode 1109.航班预定统计 差分
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 1109.航班预定统计 差分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路:
這道題假如要用暴力的方法,是過不去的。要使用差分的思想,創建一個差數組。因為本道題初始的座位數是0,直接建立一個全0數組即可。
參考代碼:
class Solution { public:vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {vector<int> v;for(int i = 0;i <= n; i++) //此處需要多加個一個0,因為飛機編號是從1開始的v.push_back(0);int first, last, seats;for(int i = 0; i < bookings.size(); i++){first = bookings[i][0];last = bookings[i][1];seats = bookings[i][2];v[first-1] += seats;v[last] -= seats;}for(int i = 1; i < n; i++) //將差分數組轉成正常的座位數v[i] += v[i-1];v.pop_back();return v;} };總結
以上是生活随笔為你收集整理的Leetcode 1109.航班预定统计 差分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android开发笔记之Makefile
- 下一篇: Leetcode 1094.拼车 差分