1
// costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
2
// The difference between them would be like this [511, -394, -259, -45, -722, -108] this will
3
// give us the differnce c[1] - c[0]
4
// Now after sorting them from highest to smallest would be [511, -45, -108, -259, -394,-722] from
5
// high to low c2[1] - c2[0], c1[1] - c1[0] if we want low to high then it would be like this c1[1]
6
// - c1[0], c2[1] - c2[0]
7
//
8

9
class Solution {
10
public int twoCitySchedCost(int[][] costs) {
11
Arrays.sort(
12
costs, (c1, c2) -> Integer.compare(c2[1] - c2[0], c1[1] - c1[0])); // biggest to smallest
13
int minCost = 0;
14
int n = costs.length;
15
for (int i = 0; i < n; i++) {
16
minCost +=
17
i < n / 2
18
? costs[i][0]
19
: costs[i][1]; // First half -> A; Last half -> B 259 + 184 + 577 + 54 + 667 + 118
20
}
21
return minCost;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0