diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java index d9194ec6..9f3fcaa1 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java @@ -53,69 +53,115 @@ */ public class MaximizeDistanceToClosestPerson { - // V0 - // TODO : fix below -// public int maxDistToClosest(int[] seats) { -// -// List distances = new ArrayList<>(); -// int lastIdx = -1; -// for(int i = seats.length - 1; i >= 0; i--){ -// if (seats[i] == 1){ -// if (lastIdx != -1){ -// int diff = Math.abs(i - lastIdx); -// distances.add(diff); -// } -// lastIdx = i; -// } -// } -// -// System.out.println(">>> (before sort) distances = " + distances); -// distances.sort(Integer::compareTo); -// System.out.println(">>> (after sort) distances = " + distances); -// -// // edge case : if only one "1" -// if (distances.isEmpty()){ -// return seats.length-1; -// } -// // return the max dist -// return distances.get(distances.size()-1) / 2; // ?? -// } + // V0 + // TODO : fix below + // public int maxDistToClosest(int[] seats) { + // + // List distances = new ArrayList<>(); + // int lastIdx = -1; + // for(int i = seats.length - 1; i >= 0; i--){ + // if (seats[i] == 1){ + // if (lastIdx != -1){ + // int diff = Math.abs(i - lastIdx); + // distances.add(diff); + // } + // lastIdx = i; + // } + // } + // + // System.out.println(">>> (before sort) distances = " + distances); + // distances.sort(Integer::compareTo); + // System.out.println(">>> (after sort) distances = " + distances); + // + // // edge case : if only one "1" + // if (distances.isEmpty()){ + // return seats.length-1; + // } + // // return the max dist + // return distances.get(distances.size()-1) / 2; // ?? + // } - // V1-1 - // IDEA : Next Array - // https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/ - public int maxDistToClosest_1_1(int[] seats) { - int N = seats.length; - int[] left = new int[N], right = new int[N]; - Arrays.fill(left, N); - Arrays.fill(right, N); + // V0-1 + // IDEA (fixed by gpt) + /** + * IDEA : + * + * Explanation of the Code: + * 1. Initial Setup: + * • lastOccupied keeps track of the index of the last seat occupied by a person. + * • maxDistance is initialized to 0 to store the maximum distance found. + * + * 2. Iterate Through the Array: + * • When a seat is occupied (seats[i] == 1): + * • If it’s the first occupied seat, calculate the distance from the start of the array to this seat (i). + * • Otherwise, calculate the middle distance between the current and the last occupied seat using (i - lastOccupied) / 2. + * + * 3. Check the Last Segment: + * • If the last seat is empty, calculate the distance from the last occupied seat to the end of the array (seats.length - 1 - lastOccupied). + * + * 4. Return the Maximum Distance: + * • The value of maxDistance at the end of the loop is the answer. + * + * + * Example : + * input : seats = [1, 0, 0, 0, 1, 0, 1] + * + * Execution Steps: + * 1. First occupied seat at index 0 → Distance to start = 0. + * 2. Second occupied seat at index 4 → Middle distance = (4 - 0) / 2 = 2. + * 3. Third occupied seat at index 6 → Middle distance = (6 - 4) / 2 = 1. + * 4. No empty seats after the last occupied seat. + * 5. maxDistance = 2. + * + * output: 2 + * + */ + public int maxDistToClosest_0_1(int[] seats) { + int maxDistance = 0; + int lastOccupied = -1; - for (int i = 0; i < N; ++i) { - if (seats[i] == 1) - left[i] = 0; - else if (i > 0) - left[i] = left[i - 1] + 1; + // Traverse the array to calculate maximum distances + for (int i = 0; i < seats.length; i++) { + /** NOTE !!! handle the seat val == 1 cases */ + if (seats[i] == 1) { + if (lastOccupied == -1) { + // Handle the case where the `first` occupied seat is found + maxDistance = i; // Distance from the start to the first occupied seat + } else { + // Calculate the distance to the closest person for the middle segment + /** NOTE !!! need to divided by 2, since the person need to seat at `middle` seat */ + maxDistance = Math.max(maxDistance, (i - lastOccupied) / 2); + } + lastOccupied = i; + } } - for (int i = N - 1; i >= 0; --i) { - if (seats[i] == 1) - right[i] = 0; - else if (i < N - 1) - right[i] = right[i + 1] + 1; + // Handle the case where the last segment is empty + if (seats[seats.length - 1] == 0) { + maxDistance = Math.max(maxDistance, seats.length - 1 - lastOccupied); } - int ans = 0; - for (int i = 0; i < N; ++i) - if (seats[i] == 0) - ans = Math.max(ans, Math.min(left[i], right[i])); - return ans; + return maxDistance; } - - // V1-2 + // V1-1 // IDEA : 2 POINTERS // https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/ - public int maxDistToClosest_1_2(int[] seats) { + /** + * IDEA : + * + * In a group of K adjacent empty seats between two people, the answer is (K+1) / 2. + * + * Algorithm + * + * For each group of K empty seats between two people, + * we can take into account the candidate answer (K+1) / 2. + * + * For groups of empty seats between the edge of the row and one other person, + * the answer is K, and we should take into account those answers too. + * + */ + public int maxDistToClosest_1_1(int[] seats) { int N = seats.length; int prev = -1, future = 0; int ans = 0; @@ -136,5 +182,35 @@ public int maxDistToClosest_1_2(int[] seats) { return ans; } + // V1-2 + // IDEA : Next Array + // https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/ + public int maxDistToClosest_1_2(int[] seats) { + int N = seats.length; + int[] left = new int[N], right = new int[N]; + Arrays.fill(left, N); + Arrays.fill(right, N); + + for (int i = 0; i < N; ++i) { + if (seats[i] == 1) + left[i] = 0; + else if (i > 0) + left[i] = left[i - 1] + 1; + } + + for (int i = N - 1; i >= 0; --i) { + if (seats[i] == 1) + right[i] = 0; + else if (i < N - 1) + right[i] = right[i + 1] + 1; + } + + int ans = 0; + for (int i = 0; i < N; ++i) + if (seats[i] == 0) + ans = Math.max(ans, Math.min(left[i], right[i])); + return ans; + } + // V2 } diff --git a/leetcode_java/src/main/java/dev/workspace7.java b/leetcode_java/src/main/java/dev/workspace7.java index 38c1947c..771480d4 100644 --- a/leetcode_java/src/main/java/dev/workspace7.java +++ b/leetcode_java/src/main/java/dev/workspace7.java @@ -8,37 +8,52 @@ public class workspace7 { public static void main(String[] args) { // ------------------- TEST 1 -// int len = 5; -// int[][] updates ={ {1,3,2}, {2,4,3}, {0,2,-2} }; -// int[] res = getModifiedArray(len, updates); -// System.out.println(">>> res = "); -// for (int x : res){ -// // [-2,0,3,5,3] -// System.out.println(x); -// } + // int len = 5; + // int[][] updates ={ {1,3,2}, {2,4,3}, {0,2,-2} }; + // int[] res = getModifiedArray(len, updates); + // System.out.println(">>> res = "); + // for (int x : res){ + // // [-2,0,3,5,3] + // System.out.println(x); + // } // ------------------- TEST 2 -// int[][] mat1 = new int[][]{ {1,0,0}, {-1,0,3} }; -// int[][] mat2 = new int[][]{ {7,0,0}, {0,0,0}, {0,0,1} }; -// int[][] res = multiply(mat1, mat2); -// System.out.println(">>> res = "); -// for (int i = 0; i < res.length; i++){ -// for (int j = 0; j < res[0].length; j++){ -// System.out.println(res[i][j]); -// } -// } + // int[][] mat1 = new int[][]{ {1,0,0}, {-1,0,3} }; + // int[][] mat2 = new int[][]{ {7,0,0}, {0,0,0}, {0,0,1} }; + // int[][] res = multiply(mat1, mat2); + // System.out.println(">>> res = "); + // for (int i = 0; i < res.length; i++){ + // for (int j = 0; j < res[0].length; j++){ + // System.out.println(res[i][j]); + // } + // } // ------------------- TEST 3 -// String a = "a"; -// String b = "b"; -// a += b; -// System.out.println(">> a = " + a); + // String a = "a"; + // String b = "b"; + // a += b; + // System.out.println(">> a = " + a); // ------------------- TEST 4 - List x = new ArrayList<>(); - x.add("a"); - x.add("b"); - System.out.println(x.toString()); + // List x = new ArrayList<>(); + // x.add("a"); + // x.add("b"); + // System.out.println(x.toString()); + + // ------------------- TEST 5 + // ++i -> It increments i after the current iteration’s execution. + /** + * Key Points: + * • Prefix (++i) vs. Postfix (i++): + * • Prefix increments the value before use. + * • Postfix increments the value after use. + * • In a for loop, this distinction doesn’t matter since the increment happens at the end of each iteration. + * • Variable Name: + * • Ensure the loop variable is consistently referenced. Mixing i and I results in a compilation error due to case sensitivity in Java. + */ + for (int i = 0; i < 5; ++i) { + System.out.println(i); + } }