Skip to content

Commit e3aefb6

Browse files
committed
Add solution #1029
1 parent 9d98cb8 commit e3aefb6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,100 LeetCode solutions in JavaScript
1+
# 1,101 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -836,6 +836,7 @@
836836
1026|[Maximum Difference Between Node and Ancestor](./solutions/1026-maximum-difference-between-node-and-ancestor.js)|Medium|
837837
1027|[Longest Arithmetic Subsequence](./solutions/1027-longest-arithmetic-subsequence.js)|Medium|
838838
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|
839+
1029|[Two City Scheduling](./solutions/1029-two-city-scheduling.js)|Medium|
839840
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
840841
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
841842
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|

solutions/1029-two-city-scheduling.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 1029. Two City Scheduling
3+
* https://leetcode.com/problems/two-city-scheduling/
4+
* Difficulty: Medium
5+
*
6+
* A company is planning to interview 2n people. Given the array costs where
7+
* costs[i] = [aCosti, bCosti], the cost of flying the ith person to city a is aCosti,
8+
* and the cost of flying the ith person to city b is bCosti.
9+
*
10+
* Return the minimum cost to fly every person to a city such that exactly n people
11+
* arrive in each city.
12+
*/
13+
14+
/**
15+
* @param {number[][]} costs
16+
* @return {number}
17+
*/
18+
/**
19+
* @param {number[][]} costs
20+
* @return {number}
21+
*/
22+
var twoCitySchedCost = function(costs) {
23+
return costs
24+
.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1]))
25+
.reduce((sum, [a, b], index) => sum + (index < costs.length / 2 ? a : b), 0);
26+
};

0 commit comments

Comments
 (0)