From df3ab01c1537e2b880df6a8978e75ade07094341 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 8 Dec 2024 16:17:09 +0800 Subject: [PATCH] update 853 java --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- .../java/LeetCodeJava/Stack/CarFleet.java | 152 +++++++++++++++++- .../src/main/java/dev/workspace5.java | 66 ++++++++ 5 files changed, 225 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c88247a9..7730caf8 100644 --- a/README.md +++ b/README.md @@ -455,7 +455,7 @@ 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) 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) 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) -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) +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) 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) 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* 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) diff --git a/data/progress.txt b/data/progress.txt index ea970fbd..dd5a7eea 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241208: 304 +20241208: 304,853 20241202: 370(todo),1109(todo) 20241130: 34,767 20241126: 722,380 diff --git a/data/to_review.txt b/data/to_review.txt index 86a0dbf2..b6cc16da 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2025-02-01 -> ['304'] +2025-02-01 -> ['304,853'] 2025-01-26 -> ['370(todo),1109(todo)'] 2025-01-24 -> ['34,767'] 2025-01-20 -> ['722,380'] @@ -7,32 +7,32 @@ 2025-01-16 -> ['776,31'] 2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)'] 2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)'] -2025-01-11 -> ['304', '394'] +2025-01-11 -> ['304,853', '394'] 2025-01-10 -> ['833,950'] 2025-01-05 -> ['370(todo),1109(todo)'] 2025-01-04 -> ['53,210,207'] 2025-01-03 -> ['34,767', '444'] 2025-01-02 -> ['1188,130,855(again)'] 2024-12-30 -> ['722,380'] -2024-12-29 -> ['304', '33,81'] +2024-12-29 -> ['304,853', '33,81'] 2024-12-28 -> ['900'] 2024-12-27 -> ['253', '26,27', '802,1197,26'] 2024-12-26 -> ['776,31'] 2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)'] 2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)'] 2024-12-23 -> ['370(todo),1109(todo)'] -2024-12-21 -> ['304', '34,767', '394', '855,846'] +2024-12-21 -> ['304,853', '34,767', '394', '855,846'] 2024-12-20 -> ['833,950', '932'] 2024-12-18 -> ['951,792'] 2024-12-17 -> ['722,380'] -2024-12-16 -> ['304', '33,81'] +2024-12-16 -> ['304,853', '33,81'] 2024-12-15 -> ['370(todo),1109(todo)'] 2024-12-14 -> ['253', '53,210,207', '163,1048'] -2024-12-13 -> ['304', '34,767', '776,31', '444', '298,729'] +2024-12-13 -> ['304,853', '34,767', '776,31', '444', '298,729'] 2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146'] -2024-12-11 -> ['304', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)'] -2024-12-10 -> ['304', '370(todo),1109(todo)'] -2024-12-09 -> ['304', '722,380'] +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)'] +2024-12-10 -> ['304,853', '370(todo),1109(todo)'] +2024-12-09 -> ['304,853', '722,380'] 2024-12-08 -> ['34,767', '33,81', '394', '737'] 2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737'] 2024-12-06 -> ['253', '26,27', '802,1197,26', '353'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java b/leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java index 76c0b227..763600f7 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Stack/CarFleet.java @@ -1,14 +1,159 @@ package LeetCodeJava.Stack; -// https://leetcode.com/problems/car-fleet/ - import java.util.*; +// https://leetcode.com/problems/car-fleet/ +/** + * 853. Car Fleet + * Solved + * Medium + * Topics + * Companies + * There are n cars at given miles away from the starting mile 0, traveling to reach the mile target. + * + * 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. + * + * A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car. + * + * 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. + * + * If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet. + * + * Return the number of car fleets that will arrive at the destination. + * + * + * + * Example 1: + * + * Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] + * + * Output: 3 + * + * Explanation: + * + * The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target. + * The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself. + * 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. + * Example 2: + * + * Input: target = 10, position = [3], speed = [3] + * + * Output: 1 + * + * Explanation: + * + * There is only one car, hence there is only one fleet. + * Example 3: + * + * Input: target = 100, position = [0,2,4], speed = [4,2,1] + * + * Output: 1 + * + * Explanation: + * + * 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. + * 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. + * + * + * Constraints: + * + * n == position.length == speed.length + * 1 <= n <= 105 + * 0 < target <= 106 + * 0 <= position[i] < target + * All the values of position are unique. + * 0 < speed[i] <= 106 + * + * + * + */ + public class CarFleet { // V0 - // IDEA : STACK + HASH MAP + // IDEA: pair position and speed, sorting (gpt) + /** + * IDEA : + * + * The approach involves sorting the cars by their starting positions + * (from farthest to nearest to the target) + * and computing their time to reach the target. + * We then iterate through these times to count the number of distinct fleets. + * + * + * + * Steps in the Code: + * 1. Pair Cars with Their Speeds: + * • Combine position and speed into a 2D array cars for easier sorting and access. + * 2. Sort Cars by Position Descending: + * • Use Arrays.sort with a custom comparator to sort cars from farthest to nearest relative to the target. + * 3. Calculate Arrival Times: + * • Compute the time each car takes to reach the target using the formula: + * + * time = (target - position) / speed + * + * 4. Count Fleets: + * • Iterate through the times array: + * • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet. + * • Update lastTime to the current car’s time. + * 5. Return Fleet Count: + * • The number of distinct times that exceed lastTime corresponds to the number of fleets. + * + */ public int carFleet(int target, int[] position, int[] speed) { + int n = position.length; + // Pair positions with speeds and `sort by position in descending order` + // cars : [position][speed] + int[][] cars = new int[n][2]; + for (int i = 0; i < n; i++) { + cars[i][0] = position[i]; + cars[i][1] = speed[i]; + } + + /** + * NOTE !!! + * + * Sort by position descending (simulate the "car arriving" process + */ + Arrays.sort(cars, (a, b) -> b[0] - a[0]); // Sort by position descending + + // Calculate arrival times + double[] times = new double[n]; + for (int i = 0; i < n; i++) { + times[i] = (double) (target - cars[i][0]) / cars[i][1]; + } + + // Count fleets + int fleets = 0; + double lastTime = 0; + for (double time : times) { + /** + * 4. Count Fleets: + * • Iterate through the times array: + * • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet. + * • Update lastTime to the current car’s time. + */ + // If current car's time is greater than the last fleet's time, it forms a new fleet + if (time > lastTime) { + fleets++; + lastTime = time; + } + } + + return fleets; + } + + // V0-1 + // IDEA : HASH MAP + Array sorting + /** + * Step 1) calculate all arriving time for all cars and save as map + * Step 2) sort distance array + * - Sorting ensures that cars are processed in the order they reach the target, making it easier to determine fleet formation. + * Step 3) compare previous arrive time and cur arrive time, if can't form a "fleet" then append to fleet + * Step 4) return fleet size + * + */ + public int carFleet_0_1(int target, int[] position, int[] speed) { int[] dist = new int[position.length]; double[] arrived_time = new double[position.length]; @@ -33,7 +178,6 @@ public int carFleet(int target, int[] position, int[] speed) { return fleet.size(); } - // V1 // https://leetcode.com/problems/car-fleet/solutions/2013259/java-simple-solution-100-faster/ public int carFleet_2(int target, int[] position, int[] speed) { diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 79884992..2c471196 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -4272,6 +4272,72 @@ public int sumRegion(int row1, int col1, int row2, int col2) { } } + // LC 853 + // 3.22 pm - 3.40 pm + /** + * 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. + * + * + * A car cannot pass another car, but it can catch up and + * then travel next to it at the speed of the slower car. + * + * 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. + */ + /** + * + * Example 1) target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3], fleet = 0 + * + * find if there is any overlap "when time = t" + * + * -> t = 1, position = [12,12,1,6,6], so fleet = 2 + * -> t = 2, position = [x,x,2,x,x], so fleet = 3 + * + * + * Example 2) target = 100, position = [0,2,4], speed = [4,2,1], fleet = 0 + * + * -> t = 1, position = [4, 4, 5], fleet = 1 + * -> t = 2, position = [6,6,5], fleet = 1 + * + * + * Idea : stack (FILO) + */ + public int carFleet(int target, int[] position, int[] speed){ + + // edge case + if (position.length == 1){ + return 1; + } + + // map record car (position) and its speed + Map map = new HashMap<>(); + int fleet = 0; + Stack stack = new Stack<>(); + //stack.add() + for (int idx = 0; idx < position.length; idx++){ + map.putIfAbsent(position[idx], speed[idx]); + stack.add(position[idx]); + } + + while(!stack.isEmpty()){ + int curP = stack.pop(); + curP += map.get(curP); + if (!stack.contains(curP) && curP < target){ + stack.add(curP); + }else{ + + } + } + + + System.out.println(">>> stack = " + stack); + + return stack.size(); + } + }