File tree 5 files changed +98
-19
lines changed
solution/1000-1099/1029.Two City Scheduling
5 files changed +98
-19
lines changed Original file line number Diff line number Diff line change 55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ ** 方法一:贪心**
59
+
60
+ 选出 ` aCost - bCost ` 最小的 N 个人,让他们飞往 A 市,其余人飞往 B 市。
61
+
58
62
<!-- tabs:start -->
59
63
60
64
### ** Python3**
61
65
62
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
67
64
68
``` python
65
-
69
+ class Solution :
70
+ def twoCitySchedCost (self , costs : List[List[int ]]) -> int :
71
+ costs.sort(key = lambda x : x[0 ] - x[1 ])
72
+ n = len (costs) >> 1
73
+ return sum (costs[i][0 ] + costs[i + n][1 ] for i in range (n))
66
74
```
67
75
68
76
### ** Java**
69
77
70
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
79
72
80
``` java
81
+ class Solution {
82
+ public int twoCitySchedCost (int [][] costs ) {
83
+ Arrays . sort(costs, (a, b) - > {
84
+ return a[0 ] - a[1 ] - (b[0 ] - b[1 ]);
85
+ });
86
+ int ans = 0 ;
87
+ int n = costs. length >> 1 ;
88
+ for (int i = 0 ; i < n; ++ i) {
89
+ ans += costs[i][0 ] + costs[i + n][1 ];
90
+ }
91
+ return ans;
92
+ }
93
+ }
94
+ ```
73
95
96
+ ### ** Go**
97
+
98
+ ``` go
99
+ func twoCitySchedCost (costs [][]int ) int {
100
+ sort.Slice (costs, func (i, j int ) bool {
101
+ return costs[i][0 ]-costs[i][1 ] < costs[j][0 ]-costs[j][1 ]
102
+ })
103
+ ans , n := 0 , len (costs)>>1
104
+ for i := 0 ; i < n; i++ {
105
+ ans += costs[i][0 ] + costs[i+n][1 ]
106
+ }
107
+ return ans
108
+ }
74
109
```
75
110
76
111
### ** ...**
Original file line number Diff line number Diff line change @@ -49,18 +49,51 @@ The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interv
49
49
50
50
## Solutions
51
51
52
+ Greedy.
53
+
52
54
<!-- tabs:start -->
53
55
54
56
### ** Python3**
55
57
56
58
``` python
57
-
59
+ class Solution :
60
+ def twoCitySchedCost (self , costs : List[List[int ]]) -> int :
61
+ costs.sort(key = lambda x : x[0 ] - x[1 ])
62
+ n = len (costs) >> 1
63
+ return sum (costs[i][0 ] + costs[i + n][1 ] for i in range (n))
58
64
```
59
65
60
66
### ** Java**
61
67
62
68
``` java
69
+ class Solution {
70
+ public int twoCitySchedCost (int [][] costs ) {
71
+ Arrays . sort(costs, (a, b) - > {
72
+ return a[0 ] - a[1 ] - (b[0 ] - b[1 ]);
73
+ });
74
+ int ans = 0 ;
75
+ int n = costs. length >> 1 ;
76
+ for (int i = 0 ; i < n; ++ i) {
77
+ ans += costs[i][0 ] + costs[i + n][1 ];
78
+ }
79
+ return ans;
80
+ }
81
+ }
82
+ ```
63
83
84
+ ### ** Go**
85
+
86
+ ``` go
87
+ func twoCitySchedCost (costs [][]int ) int {
88
+ sort.Slice (costs, func (i, j int ) bool {
89
+ return costs[i][0 ]-costs[i][1 ] < costs[j][0 ]-costs[j][1 ]
90
+ })
91
+ ans , n := 0 , len (costs)>>1
92
+ for i := 0 ; i < n; i++ {
93
+ ans += costs[i][0 ] + costs[i+n][1 ]
94
+ }
95
+ return ans
96
+ }
64
97
```
65
98
66
99
### ** ...**
Original file line number Diff line number Diff line change
1
+ func twoCitySchedCost (costs [][]int ) int {
2
+ sort .Slice (costs , func (i , j int ) bool {
3
+ return costs [i ][0 ]- costs [i ][1 ] < costs [j ][0 ]- costs [j ][1 ]
4
+ })
5
+ ans , n := 0 , len (costs )>> 1
6
+ for i := 0 ; i < n ; i ++ {
7
+ ans += costs [i ][0 ] + costs [i + n ][1 ]
8
+ }
9
+ return ans
10
+ }
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public int twoCitySchedCost (int [][] costs ) {
3
- Arrays .sort (costs , (a , b ) -> {
4
- return a [0 ] - a [1 ] - (b [0 ] - b [1 ]);
5
- });
6
-
7
- int sum = 0 ;
8
- for (int i = 0 ; i < costs .length ; ++i ) {
9
- if (i < costs .length / 2 ) {
10
- sum += costs [i ][0 ];
11
- } else {
12
- sum += costs [i ][1 ];
13
- }
14
- }
15
- return sum ;
16
- }
17
- }
1
+ class Solution {
2
+ public int twoCitySchedCost (int [][] costs ) {
3
+ Arrays .sort (costs , (a , b ) -> {
4
+ return a [0 ] - a [1 ] - (b [0 ] - b [1 ]);
5
+ });
6
+ int ans = 0 ;
7
+ int n = costs .length >> 1 ;
8
+ for (int i = 0 ; i < n ; ++i ) {
9
+ ans += costs [i ][0 ] + costs [i + n ][1 ];
10
+ }
11
+ return ans ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def twoCitySchedCost (self , costs : List [List [int ]]) -> int :
3
+ costs .sort (key = lambda x : x [0 ] - x [1 ])
4
+ n = len (costs ) >> 1
5
+ return sum (costs [i ][0 ] + costs [i + n ][1 ] for i in range (n ))
You can’t perform that action at this time.
0 commit comments