Skip to content

Commit 48eceb8

Browse files
committed
add 849 java
1 parent 064e452 commit 48eceb8

File tree

5 files changed

+207
-11
lines changed

5 files changed

+207
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287
840 | [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | [Python](./leetcode_python/Array/magic-squares-in-grid.py) | _O(m * n)_ | _O(1)_ | Easy |`complex`| AGAIN (not start)
288288
842 | [Split Array into Fibonacci Sequence](https://leetcode.com/problems/split-array-into-fibonacci-sequence/) | [Python](./leetcode_python/Array/split-array-into-fibonacci-sequence.py) | _O(n^3)_ | _O(n)_ | Medium |check `# 306 Addictive Number`, `basic`, `dfs`, `fibonacci`| AGAIN* (not start)
289289
845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [Python](./leetcode_python/Array/longest-mountain-in-array.py) | _O(n)_ | _O(1)_ | Medium |`basic`, `trick`| AGAIN* (not start)
290-
849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [Python](./leetcode_python/Array/maximize-distance-to-closest-person.py) | _O(n)_ | _O(1)_ | Easy |`basic`, `trick`| AGAIN (not start*)
290+
849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [Python](./leetcode_python/Array/maximize-distance-to-closest-person.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java) | _O(n)_ | _O(1)_ | Easy |`basic`, google, 2 pointers| AGAIN (1)
291291
860 | [Lemonade Change](https://leetcode.com/problems/lemonade-change/) | [Python](./leetcode_python/Array/lemonade-change.py) | _O(n)_ | _O(1)_ | Easy |amazon| OK* (2)
292292
0868 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Python](./leetcode_python/Array/transpose-matrix.py) | _O(r * c)_ | _O(1)_ | Easy |`basic`| OK*
293293
885 | [Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/) | [Python](./leetcode_python/Array/spiral-matrix-iii.py) | _O(max(m, n)^2)_ | _O(1)_ | Medium |`basic`| AGAIN* (not start)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241227: 079,212(todo),362
1+
20241227: 079,212(todo),362,849
22
20241222: 369,311
33
20241221: 370
44
20241220: 815,871,593,1109

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
2025-02-20 -> ['079,212(todo),362']
1+
2025-02-20 -> ['079,212(todo),362,849']
22
2025-02-15 -> ['369,311']
33
2025-02-14 -> ['370']
44
2025-02-13 -> ['815,871,593,1109']
55
2025-02-07 -> ['560,523']
66
2025-02-01 -> ['304,853,325']
7-
2025-01-30 -> ['079,212(todo),362']
7+
2025-01-30 -> ['079,212(todo),362,849']
88
2025-01-26 -> ['370(todo)']
99
2025-01-25 -> ['369,311']
1010
2025-01-24 -> ['370', '34,767']
1111
2025-01-23 -> ['815,871,593,1109']
1212
2025-01-20 -> ['722,380']
1313
2025-01-19 -> ['33,81']
14-
2025-01-17 -> ['079,212(todo),362', '560,523', '253']
14+
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
1515
2025-01-16 -> ['776,31']
1616
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1717
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1818
2025-01-12 -> ['369,311']
1919
2025-01-11 -> ['370', '304,853,325', '394']
2020
2025-01-10 -> ['815,871,593,1109', '833,950']
21-
2025-01-09 -> ['079,212(todo),362']
21+
2025-01-09 -> ['079,212(todo),362,849']
2222
2025-01-05 -> ['370(todo)']
23-
2025-01-04 -> ['079,212(todo),362', '369,311', '560,523', '53,210,207']
23+
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
2424
2025-01-03 -> ['370', '34,767', '444']
2525
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
26-
2025-01-01 -> ['079,212(todo),362']
27-
2024-12-30 -> ['079,212(todo),362', '369,311', '722,380']
28-
2024-12-29 -> ['079,212(todo),362', '370', '304,853,325', '33,81']
29-
2024-12-28 -> ['079,212(todo),362', '815,871,593,1109', '900']
26+
2025-01-01 -> ['079,212(todo),362,849']
27+
2024-12-30 -> ['079,212(todo),362,849', '369,311', '722,380']
28+
2024-12-29 -> ['079,212(todo),362,849', '370', '304,853,325', '33,81']
29+
2024-12-28 -> ['079,212(todo),362,849', '815,871,593,1109', '900']
3030
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
3131
2024-12-26 -> ['370', '776,31']
3232
2024-12-25 -> ['369,311', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/maximize-distance-to-closest-person/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* 849. Maximize Distance to Closest Person
11+
* Medium
12+
* Topics
13+
* Companies
14+
* You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed).
15+
*
16+
* There is at least one empty seat, and at least one person sitting.
17+
*
18+
* Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
19+
*
20+
* Return that maximum distance to the closest person.
21+
*
22+
*
23+
*
24+
* Example 1:
25+
*
26+
*
27+
* Input: seats = [1,0,0,0,1,0,1]
28+
* Output: 2
29+
* Explanation:
30+
* If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
31+
* If Alex sits in any other open seat, the closest person has distance 1.
32+
* Thus, the maximum distance to the closest person is 2.
33+
* Example 2:
34+
*
35+
* Input: seats = [1,0,0,0]
36+
* Output: 3
37+
* Explanation:
38+
* If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
39+
* This is the maximum distance possible, so the answer is 3.
40+
* Example 3:
41+
*
42+
* Input: seats = [0,1]
43+
* Output: 1
44+
*
45+
*
46+
* Constraints:
47+
*
48+
* 2 <= seats.length <= 2 * 104
49+
* seats[i] is 0 or 1.
50+
* At least one seat is empty.
51+
* At least one seat is occupied.
52+
*
53+
*/
54+
public class MaximizeDistanceToClosestPerson {
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+
// }
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);
92+
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;
98+
}
99+
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;
105+
}
106+
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;
112+
}
113+
114+
115+
// V1-2
116+
// IDEA : 2 POINTERS
117+
// https://leetcode.com/problems/maximize-distance-to-closest-person/editorial/
118+
public int maxDistToClosest_1_2(int[] seats) {
119+
int N = seats.length;
120+
int prev = -1, future = 0;
121+
int ans = 0;
122+
123+
for (int i = 0; i < N; ++i) {
124+
if (seats[i] == 1) {
125+
prev = i;
126+
} else {
127+
while (future < N && seats[future] == 0 || future < i)
128+
future++;
129+
130+
int left = prev == -1 ? N : i - prev;
131+
int right = future == N ? N : future - i;
132+
ans = Math.max(ans, Math.min(left, right));
133+
}
134+
}
135+
136+
return ans;
137+
}
138+
139+
// V2
140+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,60 @@ public int getHits(int timestamp) {
700700
}
701701
}
702702

703+
// LC 849
704+
// https://leetcode.com/problems/maximize-distance-to-closest-person/
705+
// 4.10 pm - 4.20 pm
706+
/**
707+
*
708+
* Alex wants to sit in the seat such that the
709+
* distance between him and the closest person
710+
* to him is maximized.
711+
*
712+
* Return that maximum distance to the closest person.
713+
*
714+
*/
715+
/**
716+
*
717+
* Idea :
718+
*
719+
* array collect cur distances ? (dist between "1", and "1") ???
720+
* sorting the distance
721+
* select the "max" distance
722+
*
723+
*
724+
* Exp 1:
725+
*
726+
* Input: seats = [1,0,0,0,1,0,1]
727+
* Output: 2
728+
*
729+
* ->
730+
*
731+
*
732+
*/
733+
public int maxDistToClosest(int[] seats) {
734+
735+
List<Integer> distances = new ArrayList<>();
736+
int lastIdx = -1;
737+
for(int i = seats.length - 1; i >= 0; i--){
738+
if (seats[i] == 1){
739+
if (lastIdx != -1){
740+
int diff = Math.abs(i - lastIdx);
741+
distances.add(diff);
742+
}
743+
lastIdx = i;
744+
}
745+
}
746+
747+
System.out.println(">>> (before sort) distances = " + distances);
748+
distances.sort(Integer::compareTo);
749+
System.out.println(">>> (after sort) distances = " + distances);
750+
751+
// edge case : if only one "1"
752+
if (distances.isEmpty()){
753+
return seats.length-1;
754+
}
755+
// return the max dist
756+
return distances.get(distances.size()-1) / 2; // ??
757+
}
758+
703759
}

0 commit comments

Comments
 (0)