-
Notifications
You must be signed in to change notification settings - Fork 0
128. Longest Consecutive Sequence #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| ## step1 何も見ずに解く | ||
| - 計算量 | ||
| - 時間計算量:O(nlogn) | ||
| - 参考:https://docs.oracle.com/javase/jp/21/docs/api/java.base/java/util/Arrays.html#sort(int%5B%5D) | ||
| - 空間計算量:O(n) | ||
| - 「You must write an algorithm that runs in O(n) time.」と記載があったがSortする方法しか思い浮かばず | ||
| - 考慮漏れしエラーになったケース | ||
| - 与えられたnumsが空の場合を考慮できずエラー | ||
|
|
||
| ```java | ||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| if(nums.length == 0) return 0; | ||
|
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. step1とstep2で、if文のスペースがあったりなかったりしていますね。 |
||
|
|
||
| Arrays.sort(nums); | ||
| int maxStreak = 0; | ||
| int currentStreak = 1; | ||
| int previousNum = nums[0]; | ||
|
|
||
| for(int i = 1; i < nums.length; i++){ | ||
| if(previousNum == nums[i]){ | ||
| continue; | ||
| } | ||
| if(previousNum + 1 == nums[i]){ | ||
| currentStreak ++; | ||
| previousNum = nums[i]; | ||
| continue; | ||
| } | ||
| maxStreak = Math.max(maxStreak, currentStreak); | ||
| currentStreak = 1; | ||
| previousNum = nums[i]; | ||
| } | ||
| return Math.max(maxStreak, currentStreak); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## step2 他の回答を見る | ||
| - 感想: | ||
| - 入力値を数直線上に置いたときに、連続値の始まりかどうかの判別は「左隣の数値が存在するかどうか」という観点を持てていなかった | ||
| - 重複する数値はノイズでしかないのでHash Setでもつという観点もなかった | ||
|
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. 問題のバリエーションとして、重複を省かずにカウントする場合も考えてみると面白いかもです。 |
||
| - longestの更新箇所が1箇所であること、登場する変数の少なさから可読性が高いので個人的には良さを感じている | ||
| - 計算量 | ||
| - 時間計算量:O(n) | ||
| - 空間計算量:O(n) | ||
|
|
||
| ```java | ||
| public class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| Set<Integer> numSet = new HashSet<>(); | ||
|
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. numsから重複を取り除いたものへの命名としては、distinctNums, uniqueNums とかもいいかもです。 命名関係はdiscordだと、https://discord.com/channels/1084280443945353267/1230079550923341835/1230201155619913728 に色々とまとめてくださっています。 |
||
| for (int num : nums) { | ||
| numSet.add(num); | ||
| } | ||
| int longest = 0; | ||
|
|
||
| for (int num : numSet) { | ||
| if (!numSet.contains(num - 1)) { | ||
| int length = 1; | ||
| while (numSet.contains(num + length)) { | ||
| length++; | ||
| } | ||
| longest = Math.max(longest, length); | ||
| } | ||
| } | ||
| return longest; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## step3 3回ミスなく書く | ||
| - step2の回答が好みなのでそちらで実装 | ||
|
|
||
| ```java | ||
| public class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| Set<Integer> numSet = new HashSet<>(); | ||
| for (int num : nums) { | ||
| numSet.add(num); | ||
| } | ||
| int longest = 0; | ||
|
|
||
| for (int num : numSet) { | ||
| if (!numSet.contains(num - 1)) { | ||
| int length = 1; | ||
| while (numSet.contains(num + length)) { | ||
| length++; | ||
| } | ||
| longest = Math.max(longest, length); | ||
| } | ||
| } | ||
| return longest; | ||
| } | ||
| } | ||
| ``` | ||
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.
submit後に修正して現在は修正済みです