diff --git a/Arrays/1029_Two_City_Scheduling.java b/Arrays/1029_Two_City_Scheduling.java new file mode 100644 index 00000000..c69dd7fd --- /dev/null +++ b/Arrays/1029_Two_City_Scheduling.java @@ -0,0 +1,19 @@ +//Link to question: https://leetcode.com/problems/two-city-scheduling/ + +import java.lang.Math; +class Solution { + public int twoCitySchedCost(int[][] costs) { + int N = costs.length/2; + int[] refund = new int[N * 2]; + int minCost = 0, index = 0; + for(int[] cost : costs){ + refund[index++] = cost[1] - cost[0]; + minCost += cost[0]; + } + Arrays.sort(refund); + for(int i = 0; i < N; i++){ + minCost += refund[i]; + } + return minCost; + } +}