Skip to content

Commit 619710a

Browse files
committed
update 849 java, cheatsheet
1 parent 9016a13 commit 619710a

File tree

5 files changed

+214
-20
lines changed

5 files changed

+214
-20
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), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MaximizeDistanceToClosestPerson.java) | _O(n)_ | _O(1)_ | Medium |`basic`, 2 pointers, LC 855, google| AGAIN***** (1)
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)_ | Medium |`basic`, 2 pointers, LC 855, google| AGAIN********* (2)
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
20241228: 379,173
2-
20241227: 079,212(todo),362,849,855(todo)
1+
20241228: 379,173,855(todo)
2+
20241227: 079,212(todo),362,849
33
20241222: 369,311
44
20241221: 370
55
20241220: 815,871,593,1109

doc/cheatsheet/array.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,105 @@ class Vector2D:
787787
def hasNext(self):
788788
# If the next position is a valid index of nums, return True.
789789
return self.position + 1 < len(self.nums)
790+
```
791+
792+
### 2-17) Maximize Distance to Closest Person
793+
794+
```java
795+
// java
796+
// LC 849. Maximize Distance to Closest Person
797+
798+
// V0-1
799+
// IDEA (fixed by gpt)
800+
/**
801+
* IDEA :
802+
*
803+
* Explanation of the Code:
804+
* 1. Initial Setup:
805+
* • lastOccupied keeps track of the index of the last seat occupied by a person.
806+
* • maxDistance is initialized to 0 to store the maximum distance found.
807+
*
808+
* 2. Iterate Through the Array:
809+
* • When a seat is occupied (seats[i] == 1):
810+
* • If it’s the first occupied seat, calculate the distance from the start of the array to this seat (i).
811+
* • Otherwise, calculate the middle distance between the current and the last occupied seat using (i - lastOccupied) / 2.
812+
*
813+
* 3. Check the Last Segment:
814+
* • If the last seat is empty, calculate the distance from the last occupied seat to the end of the array (seats.length - 1 - lastOccupied).
815+
*
816+
* 4. Return the Maximum Distance:
817+
* • The value of maxDistance at the end of the loop is the answer.
818+
*
819+
*
820+
* Example :
821+
* input : seats = [1, 0, 0, 0, 1, 0, 1]
822+
*
823+
* Execution Steps:
824+
* 1. First occupied seat at index 0 → Distance to start = 0.
825+
* 2. Second occupied seat at index 4 → Middle distance = (4 - 0) / 2 = 2.
826+
* 3. Third occupied seat at index 6 → Middle distance = (6 - 4) / 2 = 1.
827+
* 4. No empty seats after the last occupied seat.
828+
* 5. maxDistance = 2.
829+
*
830+
* output: 2
831+
*
832+
*/
833+
/**
834+
* Cases
835+
*
836+
* Case 1) 0001 ( all "0" till meat first "1")
837+
* Case 2) 1001001 (all "0" are enclosed by "1")
838+
* Case 3) 1001000 (there are "0" that NOT enclosed by "1" on the right hand side)
839+
*
840+
*/
841+
public int maxDistToClosest_0_1(int[] seats) {
842+
int maxDistance = 0;
843+
int lastOccupied = -1;
844+
845+
// Traverse the array to calculate maximum distances
846+
for (int i = 0; i < seats.length; i++) {
847+
/** NOTE !!! handle the seat val == 1 cases */
848+
if (seats[i] == 1) {
849+
if (lastOccupied == -1) {
850+
// Handle the case where the `first` occupied seat is found
851+
/**
852+
* NOTE !!!
853+
*
854+
* for handling below case:
855+
*
856+
* e.g. : 0001
857+
*
858+
* (so, elements are all "0" till first visit "1")
859+
* in this case, we still can get put a person to seat, and get distance
860+
*
861+
*/
862+
maxDistance = i; // Distance from the start to the first occupied seat
863+
} else {
864+
// Calculate the distance to the closest person for the middle segment
865+
/** NOTE !!! need to divided by 2, since the person need to seat at `middle` seat */
866+
maxDistance = Math.max(maxDistance, (i - lastOccupied) / 2);
867+
}
868+
lastOccupied = i;
869+
}
870+
}
871+
872+
// Handle the case where the last segment is empty
873+
/**
874+
* NOTE !!!
875+
*
876+
* the condition is actually quite straightforward,
877+
* just need to check if the last element in array is "0"
878+
* if is "0", means the array is NOT enclosed by "1"
879+
* then we need to handle such case
880+
* (example as below)
881+
*
882+
* e.g. 100010000
883+
*
884+
*/
885+
if (seats[seats.length - 1] == 0) {
886+
maxDistance = Math.max(maxDistance, seats.length - 1 - lastOccupied);
887+
}
888+
889+
return maxDistance;
890+
}
790891
```

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public class MaximizeDistanceToClosestPerson {
116116
* output: 2
117117
*
118118
*/
119+
/**
120+
* Cases
121+
*
122+
* Case 1) 0001 ( all "0" till meat first "1")
123+
* Case 2) 1001001 (all "0" are enclosed by "1")
124+
* Case 3) 1001000 (there are "0" that NOT enclosed by "1" on the right hand side)
125+
*
126+
*/
119127
public int maxDistToClosest_0_1(int[] seats) {
120128
int maxDistance = 0;
121129
int lastOccupied = -1;
@@ -126,6 +134,17 @@ public int maxDistToClosest_0_1(int[] seats) {
126134
if (seats[i] == 1) {
127135
if (lastOccupied == -1) {
128136
// Handle the case where the `first` occupied seat is found
137+
/**
138+
* NOTE !!!
139+
*
140+
* for handling below case:
141+
*
142+
* e.g. : 0001
143+
*
144+
* (so, elements are all "0" till first visit "1")
145+
* in this case, we still can get put a person to seat, and get distance
146+
*
147+
*/
129148
maxDistance = i; // Distance from the start to the first occupied seat
130149
} else {
131150
// Calculate the distance to the closest person for the middle segment
@@ -137,6 +156,18 @@ public int maxDistToClosest_0_1(int[] seats) {
137156
}
138157

139158
// Handle the case where the last segment is empty
159+
/**
160+
* NOTE !!!
161+
*
162+
* the condition is actually quite straightforward,
163+
* just need to check if the last element in array is "0"
164+
* if is "0", means the array is NOT enclosed by "1"
165+
* then we need to handle such case
166+
* (example as below)
167+
*
168+
* e.g. 100010000
169+
*
170+
*/
140171
if (seats[seats.length - 1] == 0) {
141172
maxDistance = Math.max(maxDistance, seats.length - 1 - lastOccupied);
142173
}

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

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -730,31 +730,93 @@ public int getHits(int timestamp) {
730730
*
731731
*
732732
*/
733+
// 5.45 pm - 6.00 pm
734+
/**
735+
* IDEA : 2 pointers
736+
*
737+
* 2 cases
738+
* -> seat between "1"
739+
* -> dist = dist(1,1) / 2
740+
* -> set is NOT between "1"
741+
* -> dist = dist(1, array_len)
742+
*
743+
* and maintain the max dist within the looping
744+
*
745+
*
746+
* Exp :
747+
*
748+
* 1001001
749+
*
750+
* 100100100
751+
*
752+
* 1000
753+
*
754+
* 1000010
755+
*
756+
* 10100000
757+
*
758+
*/
733759
public int maxDistToClosest(int[] seats) {
734760

735-
List<Integer> distances = new ArrayList<>();
761+
if (seats.length == 2){
762+
return 1; // ?? check
763+
}
764+
765+
int maxDist = 0;
766+
// int seatedCnt = 0;
736767
int lastIdx = -1;
737-
for(int i = seats.length - 1; i >= 0; i--){
768+
//int closed = -1; // ???
769+
770+
for(int i = 0; i < seats.length; i++){
771+
// if (seats[i] == 1){
772+
// //seatedCnt += 1;
773+
// //closed = closed * closed;
774+
// }
738775
if (seats[i] == 1){
739-
if (lastIdx != -1){
740-
int diff = Math.abs(i - lastIdx);
741-
distances.add(diff);
742-
}
743-
lastIdx = i;
776+
if (lastIdx == -1){
777+
lastIdx = i;
778+
}else{
779+
int dist = (i - lastIdx) / 2;
780+
maxDist = Math.max(maxDist, (dist));
781+
}
744782
}
745783
}
746784

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;
785+
// NOTE !!! ONLY need to check if the "last 0s" is NOT encclosed by "1"
786+
// -> then we NEED to treat it as 2) case
787+
// -> e.g. calculate dist from `last seen 1` to `last 0`
788+
if (seats[seats.length-1] == 0){
789+
//return seats.length - lastIdx;
790+
maxDist = Math.max(maxDist, (seats.length - lastIdx));
754791
}
755-
// return the max dist
756-
return distances.get(distances.size()-1) / 2; // ??
757-
}
792+
793+
return maxDist;
794+
}
795+
// public int maxDistToClosest(int[] seats) {
796+
//
797+
// List<Integer> distances = new ArrayList<>();
798+
// int lastIdx = -1;
799+
// for(int i = seats.length - 1; i >= 0; i--){
800+
// if (seats[i] == 1){
801+
// if (lastIdx != -1){
802+
// int diff = Math.abs(i - lastIdx);
803+
// distances.add(diff);
804+
// }
805+
// lastIdx = i;
806+
// }
807+
// }
808+
//
809+
// System.out.println(">>> (before sort) distances = " + distances);
810+
// distances.sort(Integer::compareTo);
811+
// System.out.println(">>> (after sort) distances = " + distances);
812+
//
813+
// // edge case : if only one "1"
814+
// if (distances.isEmpty()){
815+
// return seats.length-1;
816+
// }
817+
// // return the max dist
818+
// return distances.get(distances.size()-1) / 2; // ??
819+
// }
758820

759821
// LC 855
760822
// https://leetcode.com/problems/exam-room/description/

0 commit comments

Comments
 (0)