← Home

1109. Corporate Flight Bookings

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
110

The trick here is to name the state correctly, then let the implementation follow. For 1109. Corporate Flight Bookings, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • line sweep
  • time: O(N+bookings), space: O(N)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are corpFlightBookings, counter.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N+bookings), space: O(N)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//line sweep
02//Runtime: 556 ms, faster than 28.20% of C++ online submissions for Corporate Flight Bookings.
03//Memory Usage: 67.8 MB, less than 83.30% of C++ online submissions for Corporate Flight Bookings.
04//time: O(N+bookings), space: O(N)
05class Solution {
06public:
07    vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
08        vector<int> counter(n);
09        
10        for(vector<int>& book : bookings){
11            int i = book[0]-1, j = book[1]-1;
12            counter[i] += book[2];
13            if(j+1 < n) counter[j+1] -= book[2];
14        }
15        
16        for(int i = 1; i < n; ++i){
17            counter[i] += counter[i-1];
18        }
19        
20        return counter;
21    }
22};

Cost

Complexity

Time
O(N+bookings), space: O(N)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.