53
53
*/
54
54
public class MaximizeDistanceToClosestPerson {
55
55
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
+ // }
83
83
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 ;
92
122
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
+ }
98
137
}
99
138
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 );
105
142
}
106
143
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 ;
112
145
}
113
146
114
-
115
- // V1-2
147
+ // V1-1
116
148
// IDEA : 2 POINTERS
117
149
// 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 ) {
119
165
int N = seats .length ;
120
166
int prev = -1 , future = 0 ;
121
167
int ans = 0 ;
@@ -136,5 +182,35 @@ public int maxDistToClosest_1_2(int[] seats) {
136
182
return ans ;
137
183
}
138
184
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
+
139
215
// V2
140
216
}
0 commit comments