

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Corporate Flight Bookings in Python
<p>Suppose we have n flights, and they are labeled from 1 to n. We have a list of flight bookings. The i-th booking indicates using bookings[i] = [i, j, k] this means that we booked k seats from flights labeled i to j inclusive. Find an array answer of length n, showing the number of seats booked on each flight in order of their label. So if the input is like [[1,2,10],[2,3,20],[2,5,25]] and n = 5, then the output will be [10, 55, 45, 25, 25].</p><p>To solve this, we will follow these steps −</p><ul class="list"><li>res := make one array of size n, and fill this with 0</li><li>for each entry i in bookings<ul class="list"><li>res[i[0] - 1] := res[i[0] - 1] + i[2]</li><li>if i[1] < n, then res[i[1]] := res[i[1]] – i[2]</li></ul></li><li>for i in range 1 to n – 1<ul class="list"><li>res[i] := res[i] + res[i - 1]</li></ul></li><li>return res</li></ul><p>Let us see the following implementation to get better understanding −</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/00PZynwJ " rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">class Solution(object): def corpFlightBookings(self, bookings, n): res = [0 for i in range(n)] for i in bookings: res[i[0]-1]+=i[2] if(i[1]<n): res[i[1]]-=i[2] for i in range(1,n): res[i]+=res[i-1] return res ob = Solution() print(ob.corpFlightBookings([[1,2,10],[2,3,20],[2,5,25]],5))</pre><h2>Input</h2><pre class="result notranslate">[[1,2,10],[2,3,20],[2,5,25]] 5</pre><h2>Output</h2><pre class="result notranslate">[10, 55, 45, 25, 25]</pre>
- Related Questions & Answers
- Do Airlines Refuel for Every Flight?
- What is corporate restructuring?
- What is Levered Beta in Corporate Finance?
- What are Issue Costs in Corporate Finance?
- What is Free Cash Flow in Corporate Finance?
- What is the principle involved in the flight of an aircraft?
- What do you mean by corporate culture?
- What are the characteristics of corporate finance?
- Find if k bookings possible with given arrival and departure times in C++
- To what extent are body tattoos acceptable in corporate offices?
- Mention the difference between corporate strategy and business strategy.
- Is it true that IRCTC will not take transaction charges on online ticket bookings?
- What is the biggest lesson you have learned being in the corporate world?
- Why can’t corporate CSR be used more effectively to increase the number of cattle shelters which are known to be largely economically sustainable?
- ChainMap in Python
Advertisements