-
Notifications
You must be signed in to change notification settings - Fork 0
853. Car Fleet #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yamashita-ki
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/853
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
853. Car Fleet #11
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| ## step1 とりあえず解く | ||
| - 計算量 | ||
| - 時間:O(NlogN) | ||
| - 空間:O(N) | ||
| - 反省点 | ||
| - やりたいことに対して必要以上に複雑になった気がする | ||
| - fleetの数さえあればいいから、Mapではなくintがあればよかった | ||
| - 直前のFleetのarrivalTimeだけあればいいからArrayDequeを使う必要はなかった | ||
| - 全体的に変数名がわかりにくい気がする | ||
| - 特に `entrySet` の命名がわかりにくい | ||
|
|
||
|
|
||
| ```java | ||
| class Solution { | ||
|
|
||
| private final record CarFleet(double arrivalTime, int count) {}; | ||
|
|
||
| public int carFleet(int target, int[] position, int[] speed) { | ||
| Deque<CarFleet> fleets = new ArrayDeque<>(); | ||
| Map<Integer, Integer> carsPositionAndSpeed = createPositionDescendingOrderMap(position, speed); | ||
|
|
||
| for (Map.Entry<Integer, Integer> entrySet : carsPositionAndSpeed.entrySet()) { | ||
| int distance = target - entrySet.getKey(); | ||
| double arrivalTime = (double) distance / entrySet.getValue(); | ||
| if (!fleets.isEmpty() && fleets.peekLast().arrivalTime() >= arrivalTime) { | ||
| CarFleet lastFleet = fleets.pollLast(); | ||
| fleets.addLast(new CarFleet(lastFleet.arrivalTime(), lastFleet.count() + 1)); | ||
| } else { | ||
| fleets.addLast(new CarFleet(arrivalTime, 1)); | ||
| } | ||
| } | ||
| return fleets.size(); | ||
| } | ||
|
|
||
| private Map<Integer, Integer> createPositionDescendingOrderMap(int[] position, int[] speed) { | ||
| Map<Integer, Integer> carsPositionAndSpeed = new TreeMap<>(Comparator.reverseOrder()); | ||
| for (int i = 0; i < position.length; i++) { | ||
| carsPositionAndSpeed.put(position[i], speed[i]); | ||
| } | ||
| return carsPositionAndSpeed; | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## step2 他の人の回答を見る | ||
| - 計算量 | ||
| - 時間:O(NlogN) | ||
| - 空間:O(N) | ||
| - やっていることは私が書いたStep1と同じ | ||
| - positonの降順で、positonとspeedを持つ配列を作成 | ||
| - stackに積んで、後ろの車が前の車に追いつくかどうかを確認 | ||
| - StackではなくArrayDequeを使うべきだと感じた | ||
|
|
||
| ```java | ||
| public class Solution { | ||
| public int carFleet(int target, int[] position, int[] speed) { | ||
| int[][] pair = new int[position.length][2]; | ||
| for (int i = 0; i < position.length; i++) { | ||
| pair[i][0] = position[i]; | ||
| pair[i][1] = speed[i]; | ||
| } | ||
| Arrays.sort(pair, (a, b) -> Integer.compare(b[0], a[0])); | ||
| Stack<Double> stack = new Stack<>(); | ||
| for (int[] p : pair) { | ||
| stack.push((double) (target - p[0]) / p[1]); | ||
| if (stack.size() >= 2 && | ||
| stack.peek() <= stack.get(stack.size() - 2)) | ||
| { | ||
| stack.pop(); | ||
| } | ||
| } | ||
| return stack.size(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - 計算量 | ||
| - 時間:O(NlogN) | ||
| - 空間:O(N) | ||
| - Stackを使わない方法 | ||
| - こちらの方が登場する変数が少なくすみ、余計なデータ構造がないのでより良い回答に思える | ||
| - ただ、変数名はもう少しわかりやすい方が好ましいように感じる | ||
| - nやpairなど | ||
| - なんのpairなのかわかりにくいのと中身が何かわかりにくい | ||
| - 最終的にソートできればいいので、各要素をinsertした時点で順序が担保されるTreeMapを使わず配列を最後にソート | ||
|
|
||
| ```java | ||
| public class Solution { | ||
| public int carFleet(int target, int[] position, int[] speed) { | ||
| int n = position.length; | ||
| int[][] pair = new int[n][2]; | ||
| for (int i = 0; i < n; i++) { | ||
| pair[i][0] = position[i]; | ||
| pair[i][1] = speed[i]; | ||
| } | ||
| Arrays.sort(pair, (a, b) -> Integer.compare(b[0], a[0])); | ||
|
|
||
| int fleets = 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fleets という変数名からは、各 fleet の内容が格納されているニュアンスを感じました。 num_fleets のほうが良いと思います。 |
||
| double prevTime = (double)(target - pair[0][0]) / pair[0][1]; | ||
| for (int i = 1; i < n; i++) { | ||
| double currTime = (double)(target - pair[i][0]) / pair[i][1]; | ||
| if (currTime > prevTime) { | ||
| fleets++; | ||
| prevTime = currTime; | ||
| } | ||
| } | ||
| return fleets; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## step3 3回とく | ||
| - 計算量 | ||
| - 時間:O(NlogN) | ||
| - 空間:O(N) | ||
| - step2で見た他の人の回答を一部修正 | ||
|
|
||
| ```java | ||
| class Solution { | ||
|
|
||
| private final record Car(int position, int speed) {}; | ||
|
|
||
| public int carFleet(int target, int[] position, int[] speed) { | ||
| int carCount = position.length; | ||
| Car[] cars = new Car[carCount]; | ||
| for (int i = 0; i < carCount; i++) { | ||
| cars[i] = new Car(position[i], speed[i]); | ||
| } | ||
| Arrays.sort(cars, (a,b) -> Integer.compare(b.position(), a.position())); | ||
| int fleets = 1; | ||
| double previousArrivalTime = (double) (target - cars[0].position()) / cars[0].speed(); | ||
| for (int i = 1; i < carCount; i++) { | ||
| double arrivalTime = (double) (target - cars[i].position()) / cars[i].speed(); | ||
| if (previousArrivalTime < arrivalTime) { | ||
| fleets++; | ||
| previousArrivalTime = arrivalTime; | ||
| } | ||
| } | ||
| return fleets; | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
浮動小数を <= で比較しているのが気になりました。 / の計算誤差により、同じ計算結果になるはずの値が、微妙に異なる場合があります。摂動として小さい値を加えるとよいと思います。
どれくらいの摂動を加えればよいか、解析的に求める方法を自分は知りません。ひとまず 1e-8 を使っていますが、これが正しく動く保証はありません。