Skip to content

Commit f9062de

Browse files
committed
add 849 java
1 parent 0f1d7ec commit f9062de

File tree

2 files changed

+169
-78
lines changed

2 files changed

+169
-78
lines changed

leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java

Lines changed: 129 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,69 +53,115 @@
5353
*/
5454
public class MaximizeDistanceToClosestPerson {
5555

56-
// V0
57-
// TODO : fix below
58-
// public int maxDistToClosest(int[] seats) {
59-
//
60-
// List<Integer> distances = new ArrayList<>();
61-
// int lastIdx = -1;
62-
// for(int i = seats.length - 1; i >= 0; i--){
63-
// if (seats[i] == 1){
64-
// if (lastIdx != -1){
65-
// int diff = Math.abs(i - lastIdx);
66-
// distances.add(diff);
67-
// }
68-
// lastIdx = i;
69-
// }
70-
// }
71-
//
72-
// System.out.println(">>> (before sort) distances = " + distances);
73-
// distances.sort(Integer::compareTo);
74-
// System.out.println(">>> (after sort) distances = " + distances);
75-
//
76-
// // edge case : if only one "1"
77-
// if (distances.isEmpty()){
78-
// return seats.length-1;
79-
// }
80-
// // return the max dist
81-
// return distances.get(distances.size()-1) / 2; // ??
82-
// }
56+
// V0
57+
// TODO : fix below
58+
// public int maxDistToClosest(int[] seats) {
59+
//
60+
// List<Integer> distances = new ArrayList<>();
61+
// int lastIdx = -1;
62+
// for(int i = seats.length - 1; i >= 0; i--){
63+
// if (seats[i] == 1){
64+
// if (lastIdx != -1){
65+
// int diff = Math.abs(i - lastIdx);
66+
// distances.add(diff);
67+
// }
68+
// lastIdx = i;
69+
// }
70+
// }
71+
//
72+
// System.out.println(">>> (before sort) distances = " + distances);
73+
// distances.sort(Integer::compareTo);
74+
// System.out.println(">>> (after sort) distances = " + distances);
75+
//
76+
// // edge case : if only one "1"
77+
// if (distances.isEmpty()){
78+
// return seats.length-1;
79+
// }
80+
// // return the max dist
81+
// return distances.get(distances.size()-1) / 2; // ??
82+
// }
8383

84-
// V1-1
85-
// IDEA : Next Array
86-
// https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/
87-
public int maxDistToClosest_1_1(int[] seats) {
88-
int N = seats.length;
89-
int[] left = new int[N], right = new int[N];
90-
Arrays.fill(left, N);
91-
Arrays.fill(right, N);
84+
// V0-1
85+
// IDEA (fixed by gpt)
86+
/**
87+
* IDEA :
88+
*
89+
* Explanation of the Code:
90+
* 1. Initial Setup:
91+
* • lastOccupied keeps track of the index of the last seat occupied by a person.
92+
* • maxDistance is initialized to 0 to store the maximum distance found.
93+
*
94+
* 2. Iterate Through the Array:
95+
* • When a seat is occupied (seats[i] == 1):
96+
* • If it’s the first occupied seat, calculate the distance from the start of the array to this seat (i).
97+
* • Otherwise, calculate the middle distance between the current and the last occupied seat using (i - lastOccupied) / 2.
98+
*
99+
* 3. Check the Last Segment:
100+
* • If the last seat is empty, calculate the distance from the last occupied seat to the end of the array (seats.length - 1 - lastOccupied).
101+
*
102+
* 4. Return the Maximum Distance:
103+
* • The value of maxDistance at the end of the loop is the answer.
104+
*
105+
*
106+
* Example :
107+
* input : seats = [1, 0, 0, 0, 1, 0, 1]
108+
*
109+
* Execution Steps:
110+
* 1. First occupied seat at index 0 → Distance to start = 0.
111+
* 2. Second occupied seat at index 4 → Middle distance = (4 - 0) / 2 = 2.
112+
* 3. Third occupied seat at index 6 → Middle distance = (6 - 4) / 2 = 1.
113+
* 4. No empty seats after the last occupied seat.
114+
* 5. maxDistance = 2.
115+
*
116+
* output: 2
117+
*
118+
*/
119+
public int maxDistToClosest_0_1(int[] seats) {
120+
int maxDistance = 0;
121+
int lastOccupied = -1;
92122

93-
for (int i = 0; i < N; ++i) {
94-
if (seats[i] == 1)
95-
left[i] = 0;
96-
else if (i > 0)
97-
left[i] = left[i - 1] + 1;
123+
// Traverse the array to calculate maximum distances
124+
for (int i = 0; i < seats.length; i++) {
125+
/** NOTE !!! handle the seat val == 1 cases */
126+
if (seats[i] == 1) {
127+
if (lastOccupied == -1) {
128+
// Handle the case where the `first` occupied seat is found
129+
maxDistance = i; // Distance from the start to the first occupied seat
130+
} else {
131+
// Calculate the distance to the closest person for the middle segment
132+
/** NOTE !!! need to divided by 2, since the person need to seat at `middle` seat */
133+
maxDistance = Math.max(maxDistance, (i - lastOccupied) / 2);
134+
}
135+
lastOccupied = i;
136+
}
98137
}
99138

100-
for (int i = N - 1; i >= 0; --i) {
101-
if (seats[i] == 1)
102-
right[i] = 0;
103-
else if (i < N - 1)
104-
right[i] = right[i + 1] + 1;
139+
// Handle the case where the last segment is empty
140+
if (seats[seats.length - 1] == 0) {
141+
maxDistance = Math.max(maxDistance, seats.length - 1 - lastOccupied);
105142
}
106143

107-
int ans = 0;
108-
for (int i = 0; i < N; ++i)
109-
if (seats[i] == 0)
110-
ans = Math.max(ans, Math.min(left[i], right[i]));
111-
return ans;
144+
return maxDistance;
112145
}
113146

114-
115-
// V1-2
147+
// V1-1
116148
// IDEA : 2 POINTERS
117149
// https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/
118-
public int maxDistToClosest_1_2(int[] seats) {
150+
/**
151+
* IDEA :
152+
*
153+
* In a group of K adjacent empty seats between two people, the answer is (K+1) / 2.
154+
*
155+
* Algorithm
156+
*
157+
* For each group of K empty seats between two people,
158+
* we can take into account the candidate answer (K+1) / 2.
159+
*
160+
* For groups of empty seats between the edge of the row and one other person,
161+
* the answer is K, and we should take into account those answers too.
162+
*
163+
*/
164+
public int maxDistToClosest_1_1(int[] seats) {
119165
int N = seats.length;
120166
int prev = -1, future = 0;
121167
int ans = 0;
@@ -136,5 +182,35 @@ public int maxDistToClosest_1_2(int[] seats) {
136182
return ans;
137183
}
138184

185+
// V1-2
186+
// IDEA : Next Array
187+
// https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/
188+
public int maxDistToClosest_1_2(int[] seats) {
189+
int N = seats.length;
190+
int[] left = new int[N], right = new int[N];
191+
Arrays.fill(left, N);
192+
Arrays.fill(right, N);
193+
194+
for (int i = 0; i < N; ++i) {
195+
if (seats[i] == 1)
196+
left[i] = 0;
197+
else if (i > 0)
198+
left[i] = left[i - 1] + 1;
199+
}
200+
201+
for (int i = N - 1; i >= 0; --i) {
202+
if (seats[i] == 1)
203+
right[i] = 0;
204+
else if (i < N - 1)
205+
right[i] = right[i + 1] + 1;
206+
}
207+
208+
int ans = 0;
209+
for (int i = 0; i < N; ++i)
210+
if (seats[i] == 0)
211+
ans = Math.max(ans, Math.min(left[i], right[i]));
212+
return ans;
213+
}
214+
139215
// V2
140216
}

leetcode_java/src/main/java/dev/workspace7.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,52 @@ public class workspace7 {
88
public static void main(String[] args) {
99

1010
// ------------------- TEST 1
11-
// int len = 5;
12-
// int[][] updates ={ {1,3,2}, {2,4,3}, {0,2,-2} };
13-
// int[] res = getModifiedArray(len, updates);
14-
// System.out.println(">>> res = ");
15-
// for (int x : res){
16-
// // [-2,0,3,5,3]
17-
// System.out.println(x);
18-
// }
11+
// int len = 5;
12+
// int[][] updates ={ {1,3,2}, {2,4,3}, {0,2,-2} };
13+
// int[] res = getModifiedArray(len, updates);
14+
// System.out.println(">>> res = ");
15+
// for (int x : res){
16+
// // [-2,0,3,5,3]
17+
// System.out.println(x);
18+
// }
1919

2020
// ------------------- TEST 2
21-
// int[][] mat1 = new int[][]{ {1,0,0}, {-1,0,3} };
22-
// int[][] mat2 = new int[][]{ {7,0,0}, {0,0,0}, {0,0,1} };
23-
// int[][] res = multiply(mat1, mat2);
24-
// System.out.println(">>> res = ");
25-
// for (int i = 0; i < res.length; i++){
26-
// for (int j = 0; j < res[0].length; j++){
27-
// System.out.println(res[i][j]);
28-
// }
29-
// }
21+
// int[][] mat1 = new int[][]{ {1,0,0}, {-1,0,3} };
22+
// int[][] mat2 = new int[][]{ {7,0,0}, {0,0,0}, {0,0,1} };
23+
// int[][] res = multiply(mat1, mat2);
24+
// System.out.println(">>> res = ");
25+
// for (int i = 0; i < res.length; i++){
26+
// for (int j = 0; j < res[0].length; j++){
27+
// System.out.println(res[i][j]);
28+
// }
29+
// }
3030

3131
// ------------------- TEST 3
32-
// String a = "a";
33-
// String b = "b";
34-
// a += b;
35-
// System.out.println(">> a = " + a);
32+
// String a = "a";
33+
// String b = "b";
34+
// a += b;
35+
// System.out.println(">> a = " + a);
3636

3737
// ------------------- TEST 4
38-
List<String> x = new ArrayList<>();
39-
x.add("a");
40-
x.add("b");
41-
System.out.println(x.toString());
38+
// List<String> x = new ArrayList<>();
39+
// x.add("a");
40+
// x.add("b");
41+
// System.out.println(x.toString());
42+
43+
// ------------------- TEST 5
44+
// ++i -> It increments i after the current iteration’s execution.
45+
/**
46+
* Key Points:
47+
* • Prefix (++i) vs. Postfix (i++):
48+
* • Prefix increments the value before use.
49+
* • Postfix increments the value after use.
50+
* • In a for loop, this distinction doesn’t matter since the increment happens at the end of each iteration.
51+
* • Variable Name:
52+
* • Ensure the loop variable is consistently referenced. Mixing i and I results in a compilation error due to case sensitivity in Java.
53+
*/
54+
for (int i = 0; i < 5; ++i) {
55+
System.out.println(i);
56+
}
4257
}
4358

4459

0 commit comments

Comments
 (0)