forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create: 1029-two-city-scheduling.go / .py / .java
- Loading branch information
1 parent
2ae4b28
commit df542a5
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import "sort" | ||
|
||
func main() { | ||
|
||
} | ||
|
||
func twoCitySchedCost(costs [][]int) int { | ||
sort.Slice(costs, func(a, b int) bool { return costs[a][1] - costs[a][0] < costs[b][1] - costs[b][0]}); | ||
|
||
n ,totalCost := len(costs) / 2, 0 | ||
for i := 0; i < n ; i++ { | ||
totalCost += costs[i][1] + costs[i +n][0] | ||
} | ||
return totalCost | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public int twoCitySchedCost(int[][] costs) { | ||
Arrays.sort(costs, | ||
(a, b) -> (a[1] - a[0]) - (b[1] - b[0]) | ||
); | ||
|
||
int n = costs.length / 2; | ||
int total = 0; | ||
for (int i = 0; i < n; i++) { | ||
total += costs[i][1] + costs[i + n][0]; | ||
} | ||
|
||
return total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def twoCitySchedCost(self, costs: List[List[int]]) -> int: | ||
diffs = [] | ||
for c1, c2 in costs: | ||
diffs.append([c2 - c1, c1, c2]) | ||
diffs.sort() | ||
res = 0 | ||
for i in range(len(diffs)): | ||
if i < len(diffs) / 2: | ||
res += diffs[i][2] | ||
else: | ||
res += diffs[i][1] | ||
return res |