Skip to content

Commit df3ab01

Browse files
committed
update 853 java
1 parent a53f471 commit df3ab01

File tree

5 files changed

+225
-15
lines changed

5 files changed

+225
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@
455455
735| [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [Python](./leetcode_python/Stack/asteroid-collision.py) | _O(n)_ | _O(n)_ | Medium| good basic, stack, m$, GS, fb, `amazon`| AGAIN************** (5)
456456
739| [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | [Python](./leetcode_python/Stack/daily-temperatures.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/DailyTemperatures.java) | _O(n)_ | _O(n)_ | Medium |LC 739, LC 503, LC 406, LC 496, LC 42, Monotonic stack, good trick,`amazon`| AGAIN******************* (12) (MUST)
457457
772| [Basic Calculator III](https://leetcode.com/problems/basic-calculator-iii/) |[Python](./leetcode_python/Stack/basic-calculator-iii.py)| _O(n)_| _O(n)_| Medium |check LC 224, 227| AGAIN (not start)
458-
853| [Car Fleet](https://leetcode.com/problems/car-fleet/) | [Python](./leetcode_python/Stack/car-fleet.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java) | _O(nlogn)_ | _O(n)_| Medium|array, stack, good trick, google, amz| AGAIN***** (2)
458+
853| [Car Fleet](https://leetcode.com/problems/car-fleet/) | [Python](./leetcode_python/Stack/car-fleet.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java) | _O(nlogn)_ | _O(n)_| Medium|array sorting, good trick, google, amz| AGAIN******* (4)
459459
856| [Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/) | [Python](./leetcode_python/Stack/score-of-parentheses.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
460460
872| [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/) | [Python](./leetcode_python/Stack/leaf-similar-trees.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/LeafSimilarTrees.java) | _O(n)_ | _O(h)_| Easy|| AGAIN*
461461
895| [Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/) | [Python](./leetcode_python/Stack/maximum-frequency-stack.py) | _O(n)_ | _O(h)_| Hard| good basic, heap, design, stack, amazon, apple, linkedin, m$, twitter| AGAIN********** (1) (not start)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241208: 304
1+
20241208: 304,853
22
20241202: 370(todo),1109(todo)
33
20241130: 34,767
44
20241126: 722,380

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-02-01 -> ['304']
1+
2025-02-01 -> ['304,853']
22
2025-01-26 -> ['370(todo),1109(todo)']
33
2025-01-24 -> ['34,767']
44
2025-01-20 -> ['722,380']
@@ -7,32 +7,32 @@
77
2025-01-16 -> ['776,31']
88
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
99
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
10-
2025-01-11 -> ['304', '394']
10+
2025-01-11 -> ['304,853', '394']
1111
2025-01-10 -> ['833,950']
1212
2025-01-05 -> ['370(todo),1109(todo)']
1313
2025-01-04 -> ['53,210,207']
1414
2025-01-03 -> ['34,767', '444']
1515
2025-01-02 -> ['1188,130,855(again)']
1616
2024-12-30 -> ['722,380']
17-
2024-12-29 -> ['304', '33,81']
17+
2024-12-29 -> ['304,853', '33,81']
1818
2024-12-28 -> ['900']
1919
2024-12-27 -> ['253', '26,27', '802,1197,26']
2020
2024-12-26 -> ['776,31']
2121
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2222
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2323
2024-12-23 -> ['370(todo),1109(todo)']
24-
2024-12-21 -> ['304', '34,767', '394', '855,846']
24+
2024-12-21 -> ['304,853', '34,767', '394', '855,846']
2525
2024-12-20 -> ['833,950', '932']
2626
2024-12-18 -> ['951,792']
2727
2024-12-17 -> ['722,380']
28-
2024-12-16 -> ['304', '33,81']
28+
2024-12-16 -> ['304,853', '33,81']
2929
2024-12-15 -> ['370(todo),1109(todo)']
3030
2024-12-14 -> ['253', '53,210,207', '163,1048']
31-
2024-12-13 -> ['304', '34,767', '776,31', '444', '298,729']
31+
2024-12-13 -> ['304,853', '34,767', '776,31', '444', '298,729']
3232
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
33-
2024-12-11 -> ['304', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34-
2024-12-10 -> ['304', '370(todo),1109(todo)']
35-
2024-12-09 -> ['304', '722,380']
33+
2024-12-11 -> ['304,853', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34+
2024-12-10 -> ['304,853', '370(todo),1109(todo)']
35+
2024-12-09 -> ['304,853', '722,380']
3636
2024-12-08 -> ['34,767', '33,81', '394', '737']
3737
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
3838
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']

leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,159 @@
11
package LeetCodeJava.Stack;
22

3-
// https://leetcode.com/problems/car-fleet/
4-
53
import java.util.*;
64

5+
// https://leetcode.com/problems/car-fleet/
6+
/**
7+
* 853. Car Fleet
8+
* Solved
9+
* Medium
10+
* Topics
11+
* Companies
12+
* There are n cars at given miles away from the starting mile 0, traveling to reach the mile target.
13+
*
14+
* You are given two integer array position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour.
15+
*
16+
* A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.
17+
*
18+
* A car fleet is a car or cars driving next to each other. The speed of the car fleet is the minimum speed of any car in the fleet.
19+
*
20+
* If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet.
21+
*
22+
* Return the number of car fleets that will arrive at the destination.
23+
*
24+
*
25+
*
26+
* Example 1:
27+
*
28+
* Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
29+
*
30+
* Output: 3
31+
*
32+
* Explanation:
33+
*
34+
* The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target.
35+
* The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.
36+
* The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
37+
* Example 2:
38+
*
39+
* Input: target = 10, position = [3], speed = [3]
40+
*
41+
* Output: 1
42+
*
43+
* Explanation:
44+
*
45+
* There is only one car, hence there is only one fleet.
46+
* Example 3:
47+
*
48+
* Input: target = 100, position = [0,2,4], speed = [4,2,1]
49+
*
50+
* Output: 1
51+
*
52+
* Explanation:
53+
*
54+
* The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.
55+
* Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
56+
*
57+
*
58+
* Constraints:
59+
*
60+
* n == position.length == speed.length
61+
* 1 <= n <= 105
62+
* 0 < target <= 106
63+
* 0 <= position[i] < target
64+
* All the values of position are unique.
65+
* 0 < speed[i] <= 106
66+
*
67+
*
68+
*
69+
*/
70+
771
public class CarFleet {
872

973
// V0
10-
// IDEA : STACK + HASH MAP
74+
// IDEA: pair position and speed, sorting (gpt)
75+
/**
76+
* IDEA :
77+
*
78+
* The approach involves sorting the cars by their starting positions
79+
* (from farthest to nearest to the target)
80+
* and computing their time to reach the target.
81+
* We then iterate through these times to count the number of distinct fleets.
82+
*
83+
*
84+
*
85+
* Steps in the Code:
86+
* 1. Pair Cars with Their Speeds:
87+
* • Combine position and speed into a 2D array cars for easier sorting and access.
88+
* 2. Sort Cars by Position Descending:
89+
* • Use Arrays.sort with a custom comparator to sort cars from farthest to nearest relative to the target.
90+
* 3. Calculate Arrival Times:
91+
* • Compute the time each car takes to reach the target using the formula:
92+
*
93+
* time = (target - position) / speed
94+
*
95+
* 4. Count Fleets:
96+
* • Iterate through the times array:
97+
* • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet.
98+
* • Update lastTime to the current car’s time.
99+
* 5. Return Fleet Count:
100+
* • The number of distinct times that exceed lastTime corresponds to the number of fleets.
101+
*
102+
*/
11103
public int carFleet(int target, int[] position, int[] speed) {
104+
int n = position.length;
105+
// Pair positions with speeds and `sort by position in descending order`
106+
// cars : [position][speed]
107+
int[][] cars = new int[n][2];
108+
for (int i = 0; i < n; i++) {
109+
cars[i][0] = position[i];
110+
cars[i][1] = speed[i];
111+
}
112+
113+
/**
114+
* NOTE !!!
115+
*
116+
* Sort by position descending (simulate the "car arriving" process
117+
*/
118+
Arrays.sort(cars, (a, b) -> b[0] - a[0]); // Sort by position descending
119+
120+
// Calculate arrival times
121+
double[] times = new double[n];
122+
for (int i = 0; i < n; i++) {
123+
times[i] = (double) (target - cars[i][0]) / cars[i][1];
124+
}
125+
126+
// Count fleets
127+
int fleets = 0;
128+
double lastTime = 0;
129+
for (double time : times) {
130+
/**
131+
* 4. Count Fleets:
132+
* • Iterate through the times array:
133+
* • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet.
134+
* • Update lastTime to the current car’s time.
135+
*/
136+
// If current car's time is greater than the last fleet's time, it forms a new fleet
137+
if (time > lastTime) {
138+
fleets++;
139+
lastTime = time;
140+
}
141+
}
142+
143+
return fleets;
144+
}
145+
146+
// V0-1
147+
// IDEA : HASH MAP + Array sorting
148+
/**
149+
* Step 1) calculate all arriving time for all cars and save as map
150+
* Step 2) sort distance array
151+
* - Sorting ensures that cars are processed in the order they reach the target, making it easier to determine fleet formation.
152+
* Step 3) compare previous arrive time and cur arrive time, if can't form a "fleet" then append to fleet
153+
* Step 4) return fleet size
154+
*
155+
*/
156+
public int carFleet_0_1(int target, int[] position, int[] speed) {
12157

13158
int[] dist = new int[position.length];
14159
double[] arrived_time = new double[position.length];
@@ -33,7 +178,6 @@ public int carFleet(int target, int[] position, int[] speed) {
33178
return fleet.size();
34179
}
35180

36-
37181
// V1
38182
// https://leetcode.com/problems/car-fleet/solutions/2013259/java-simple-solution-100-faster/
39183
public int carFleet_2(int target, int[] position, int[] speed) {

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,6 +4272,72 @@ public int sumRegion(int row1, int col1, int row2, int col2) {
42724272
}
42734273
}
42744274

4275+
// LC 853
4276+
// 3.22 pm - 3.40 pm
4277+
/**
4278+
* You are given two integer array position and speed,
4279+
* both of length n, where position[i] is the starting mile
4280+
* of the ith car
4281+
* and speed[i] is the speed of the ith car in miles per hour.
4282+
*
4283+
*
4284+
* A car cannot pass another car, but it can catch up and
4285+
* then travel next to it at the speed of the slower car.
4286+
*
4287+
* A car fleet is a car or cars driving next to each other.
4288+
* The speed of the car fleet is the `minimum` speed of any car in the fleet.
4289+
*/
4290+
/**
4291+
*
4292+
* Example 1) target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3], fleet = 0
4293+
*
4294+
* find if there is any overlap "when time = t"
4295+
*
4296+
* -> t = 1, position = [12,12,1,6,6], so fleet = 2
4297+
* -> t = 2, position = [x,x,2,x,x], so fleet = 3
4298+
*
4299+
*
4300+
* Example 2) target = 100, position = [0,2,4], speed = [4,2,1], fleet = 0
4301+
*
4302+
* -> t = 1, position = [4, 4, 5], fleet = 1
4303+
* -> t = 2, position = [6,6,5], fleet = 1
4304+
*
4305+
*
4306+
* Idea : stack (FILO)
4307+
*/
4308+
public int carFleet(int target, int[] position, int[] speed){
4309+
4310+
// edge case
4311+
if (position.length == 1){
4312+
return 1;
4313+
}
4314+
4315+
// map record car (position) and its speed
4316+
Map<Integer, Integer> map = new HashMap<>();
4317+
int fleet = 0;
4318+
Stack<Integer> stack = new Stack<>();
4319+
//stack.add()
4320+
for (int idx = 0; idx < position.length; idx++){
4321+
map.putIfAbsent(position[idx], speed[idx]);
4322+
stack.add(position[idx]);
4323+
}
4324+
4325+
while(!stack.isEmpty()){
4326+
int curP = stack.pop();
4327+
curP += map.get(curP);
4328+
if (!stack.contains(curP) && curP < target){
4329+
stack.add(curP);
4330+
}else{
4331+
4332+
}
4333+
}
4334+
4335+
4336+
System.out.println(">>> stack = " + stack);
4337+
4338+
return stack.size();
4339+
}
4340+
42754341

42764342
}
42774343

0 commit comments

Comments
 (0)