Skip to content

Commit 8960c43

Browse files
committed
Two city scheduling
1 parent 5620c20 commit 8960c43

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

leetcode/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
| Sl No.| Level | Questions | Solution |
139139
| :---: | :---: | :--- | :--- |
140140
| 122. | <span style="color:green">Easy</span> | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/best-time-to-buy-and-sell-stock-ii.py) |
141+
| 1029. | <span style="color:green">Easy</span> | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/two-city-scheduling.py) |
141142
| 1046. | <span style="color:green">Easy</span> | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/last-stone-weight.py) |
142143
| 1196. | <span style="color:green">Easy</span> | [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/how-many-apples-can-you-put-into-the-basket.py) |
143144

@@ -304,5 +305,5 @@
304305
| :---: | :---: | :---: | :--- | :--- |
305306
| 1 | 226. | <span style="color:green">Easy</span> | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/tree/invert-binary-tree.py) |
306307
| 2 | 237. | <span style="color:green">Easy</span> | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/delete-node-in-a-linked-list.py) \| [C++](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/delete-node-in-a-linked-list.cpp) |
307-
308+
| 3 | 1029. | <span style="color:green">Easy</span> | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/two-city-scheduling.py) |
308309

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
## Questions
3+
4+
### 1029. [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)
5+
There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0],
6+
and the cost of flying the i-th person to city B is costs[i][1].
7+
8+
Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
9+
10+
Example 1:
11+
Input: [[10,20],[30,200],[400,50],[30,20]]
12+
Output: 110
13+
Explanation:
14+
The first person goes to city A for a cost of 10.
15+
The second person goes to city A for a cost of 30.
16+
The third person goes to city B for a cost of 50.
17+
The fourth person goes to city B for a cost of 20.
18+
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
19+
20+
21+
Note:
22+
1 <= costs.length <= 100
23+
It is guaranteed that costs.length is even.
24+
1 <= costs[i][0], costs[i][1] <= 1000
25+
"""
26+
27+
## Solutions
28+
29+
30+
class Solution:
31+
def lastStoneWeight(self, stones: List[int]) -> int:
32+
"""Slow using sort each time"""
33+
if len(stones) <= 1:
34+
return stones[0] if len(stones) == 1 else None
35+
temp = sorted(stones, reverse=True)
36+
while len(temp) > 2:
37+
new_weight = abs(temp[0] - temp[1])
38+
temp = sorted(temp[2:] + [new_weight], reverse=True)
39+
return abs(temp[0] - temp[1])
40+
41+
42+
# Runtime: 32 ms, faster than 49.02% of Python3 online submissions
43+
# Memory Usage: 13.9 MB, less than 100.00% of Python3 online submissions
44+
45+
46+
# Solution
47+
48+
49+
class Solution:
50+
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
51+
costA = [i for i, j in costs]
52+
diff = [j - i for i, j in costs]
53+
return sum(costA) + sum(sorted(diff)[: len(costs) // 2])
54+
55+
56+
# Runtime: 36 ms, faster than 90.00% of Python3 online submissions
57+
# Memory Usage: 13.8 MB, less than 7.69% of Python3 online submissions

0 commit comments

Comments
 (0)