File tree 1 file changed +60
-0
lines changed
June-LeetCoding-Challenge/03-Two-City-Scheduling
1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ class TwoCitySchedulingKotlin1029 {
2
+
3
+ fun twoCitySchedCost (costs : Array <IntArray >): Int {
4
+ costs.sortWith(Comparator { ints1, ints2 -> compareValues(ints1[0 ] - ints1[1 ], ints2[0 ] - ints2[1 ]) })
5
+ val half = costs.size / 2
6
+ var sum = 0
7
+ costs.forEachIndexed { index, ints ->
8
+ sum + = when {
9
+ index < half -> ints[0 ]
10
+ else -> ints[1 ]
11
+ }
12
+ }
13
+ return sum
14
+ }
15
+ /*
16
+ fun twoCitySchedCost(costs: Array<IntArray>): Int {
17
+ val half = costs.size.shr(1)
18
+ val dp = Array(half + 1) { IntArray(half + 1) }
19
+ for (index in 1 until dp.size) {
20
+ dp[0][index] = dp[0][index - 1] + costs[index - 1][0]
21
+ }
22
+ for (i in 1 until dp.size) {
23
+ dp[i][0] = dp[i - 1][0] + costs[i - 1][1]
24
+ for (j in 1 until dp.size) {
25
+ dp[i][j] = minOf(
26
+ dp[i - 1][j] + costs[i + j - 1][1],
27
+ dp[i][j - 1] + costs[i + j - 1][0]
28
+ )
29
+ }
30
+ }
31
+ return dp[half][half]
32
+ }
33
+ */
34
+ }
35
+
36
+ fun main () {
37
+ val solution = TwoCitySchedulingKotlin1029 ()
38
+ // 110
39
+ println (
40
+ solution.twoCitySchedCost(
41
+ arrayOf(
42
+ intArrayOf(10 , 20 ),
43
+ intArrayOf(30 , 200 ),
44
+ intArrayOf(400 , 50 ),
45
+ intArrayOf(30 , 20 )
46
+ )
47
+ )
48
+ )
49
+ // 130
50
+ println (
51
+ solution.twoCitySchedCost(
52
+ arrayOf(
53
+ intArrayOf(10 , 20 ),
54
+ intArrayOf(30 , 200 ),
55
+ intArrayOf(400 , 50 ),
56
+ intArrayOf(30 , 200 )
57
+ )
58
+ )
59
+ )
60
+ }
You can’t perform that action at this time.
0 commit comments