Open
Conversation
nodchip
reviewed
Sep 21, 2025
| for (int[] p : pair) { | ||
| stack.push((double) (target - p[0]) / p[1]); | ||
| if (stack.size() >= 2 && | ||
| stack.peek() <= stack.get(stack.size() - 2)) |
There was a problem hiding this comment.
浮動小数を <= で比較しているのが気になりました。 / の計算誤差により、同じ計算結果になるはずの値が、微妙に異なる場合があります。摂動として小さい値を加えるとよいと思います。
stack.peek() < stack.get(stack.size() - 2) + 1e-8どれくらいの摂動を加えればよいか、解析的に求める方法を自分は知りません。ひとまず 1e-8 を使っていますが、これが正しく動く保証はありません。
| } | ||
| Arrays.sort(pair, (a, b) -> Integer.compare(b[0], a[0])); | ||
|
|
||
| int fleets = 1; |
There was a problem hiding this comment.
fleets という変数名からは、各 fleet の内容が格納されているニュアンスを感じました。 num_fleets のほうが良いと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/car-fleet/description/
There are n cars at given miles away from the starting mile 0, traveling to reach the mile target.
You are given two integer arrays 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 single car or a group of 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.
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.