Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions 128. Longest Consecutive Sequence.md
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が空の場合を考慮できずエラー
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

submit後に修正して現在は修正済みです


```java
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0) return 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step1とstep2で、if文のスペースがあったりなかったりしていますね。
体裁面ではPythonであればPEP8や、C++ではgoogleのコーディング規約がよく引用されていると思います。
Javaだとhttps://google.github.io/styleguide/javaguide.htmlなどがあるようです。


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でもつという観点もなかった
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}
}
```