Skip to content

Commit 1af5cfb

Browse files
Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
authored and
Zanger67/leetcode
committed
Updated markdown files
1 parent 57b3b64 commit 1af5cfb

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

markdowns/Questions_By_Code_Length.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Calculations are based on the code files's byte sizes.
117117
| 40 | [Combination Sum II](<https://leetcode.com/problems/combination-sum-ii>) | Medium | Daily, N150 | [solution](<_40. Combination Sum II.md>) | py | Aug 13, 2024 |
118118
| 611 | [Valid Triangle Number](<https://leetcode.com/problems/valid-triangle-number>) | Medium | | [solution](<_611. Valid Triangle Number.md>) | py | May 22, 2024 |
119119
| 3229 | Weekly Contest 407 - q4 - [Minimum Operations to Make Array Equal to Target](<https://leetcode.com/problems/minimum-operations-to-make-array-equal-to-target>) | Hard | Contest | [solution](<_3229. Minimum Operations to Make Array Equal to Target.md>) | py | Jul 21, 2024 |
120+
| 128 | [Longest Consecutive Sequence](<https://leetcode.com/problems/longest-consecutive-sequence>) | Medium | B75, N150 | [solution](<_128. Longest Consecutive Sequence.md>) | java, py | Jun 13, 2024 |
120121
| 381 | [Insert Delete GetRandom O(1) - Duplicates allowed](<https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed>) | Hard | | [solution](<_381. Insert Delete GetRandom O(1) - Duplicates allowed.md>) | java | Jul 06, 2024 |
121122
| 419 | [Battleships in a Board](<https://leetcode.com/problems/battleships-in-a-board>) | Medium | | [solution](<_419. Battleships in a Board.md>) | c, py | Jun 24, 2024 |
122123
| 353 | [Design Snake Game](<https://leetcode.com/problems/design-snake-game>) | Medium | | [solution](<_353. Design Snake Game.md>) | py | Jun 28, 2024 |
@@ -214,7 +215,6 @@ Calculations are based on the code files's byte sizes.
214215
| 1184 | [Distance Between Bus Stops](<https://leetcode.com/problems/distance-between-bus-stops>) | Easy | | [solution](<_1184. Distance Between Bus Stops.md>) | py | May 31, 2024 |
215216
| 362 | [Design Hit Counter](<https://leetcode.com/problems/design-hit-counter>) | Medium | | [solution](<_362. Design Hit Counter.md>) | py | Oct 24, 2024 |
216217
| 90 | [Subsets II](<https://leetcode.com/problems/subsets-ii>) | Medium | N150 | [solution](<_90. Subsets II.md>) | py | Jul 03, 2024 |
217-
| 128 | [Longest Consecutive Sequence](<https://leetcode.com/problems/longest-consecutive-sequence>) | Medium | B75, N150 | [solution](<_128. Longest Consecutive Sequence.md>) | java, py | Jun 13, 2024 |
218218
| 1730 | [Shortest Path to Get Food](<https://leetcode.com/problems/shortest-path-to-get-food>) | Medium | Weekly Premium | [solution](<_1730. Shortest Path to Get Food.md>) | py | Jun 26, 2024 |
219219
| 1530 | [Number of Good Leaf Nodes Pairs](<https://leetcode.com/problems/number-of-good-leaf-nodes-pairs>) | Medium | Daily | [solution](<_1530. Number of Good Leaf Nodes Pairs.md>) | py | Jul 18, 2024 |
220220
| 1823 | [Find the Winner of the Circular Game](<https://leetcode.com/problems/find-the-winner-of-the-circular-game>) | Medium | Daily | [solution](<_1823. Find the Winner of the Circular Game.md>) | c, cpp, java, js, py | Jul 08, 2024 |

markdowns/_128. Longest Consecutive Sequence.md

+68-30
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,77 @@
1818
1919
------
2020

21+
> ## m128 v1
22+
>
23+
> My initial attempt that was successful but inefficient. Achieved around a 350ms runtime.
24+
>
25+
> ## m128 java
26+
>
27+
> Second attempt that was much more successful and achieved a reasonable runtim
28+
>
29+
> ## m128 v2
30+
>
31+
> 2nd python attempt that implemented a set removal optimization to save redundancy that brought the python version to the same runtime level as java.
32+
>
33+
34+
------
35+
2136
## Solutions
2237

38+
- [m128 v1.py](<../my-submissions/m128 v1.py>)
39+
- [m128 v2.py](<../my-submissions/m128 v2.py>)
2340
- [m128.java](<../my-submissions/m128.java>)
24-
- [m128.py](<../my-submissions/m128.py>)
41+
### Python
42+
#### [m128 v1.py](<../my-submissions/m128 v1.py>)
43+
```Python
44+
# O(n) solution babyyyy
45+
46+
class Solution:
47+
def longestConsecutive(self, nums: List[int]) -> int:
48+
if len(nums) == 0 :
49+
return 0
50+
51+
vals = set(nums) # O(n) creation, O(1) lookups
52+
53+
maxx = 0
54+
for val in vals: # O(n) looping
55+
if val - 1 in vals: # not start of sequence
56+
continue
57+
58+
cnter = 1
59+
while val + cnter in vals :
60+
cnter += 1
61+
62+
maxx = max(maxx, cnter)
63+
64+
return maxx
65+
66+
```
67+
68+
#### [m128 v2.py](<../my-submissions/m128 v2.py>)
69+
```Python
70+
class Solution:
71+
def longestConsecutive(self, nums: List[int]) -> int:
72+
if len(nums) == 0 :
73+
return 0
74+
75+
vals = set(nums) # O(n) creation, O(1) lookups
76+
77+
maxx = 0
78+
for val in vals: # O(n) looping
79+
if val - 1 in vals: # not start of sequence
80+
continue
81+
82+
cnter = 1
83+
while val + cnter in vals :
84+
cnter += 1
85+
86+
maxx = max(maxx, cnter)
87+
88+
return maxx
89+
90+
```
91+
2592
### Java
2693
#### [m128.java](<../my-submissions/m128.java>)
2794
```Java
@@ -54,32 +121,3 @@ class Solution {
54121
}
55122
```
56123

57-
### Python
58-
#### [m128.py](<../my-submissions/m128.py>)
59-
```Python
60-
class Solution:
61-
def longestConsecutive(self, nums: List[int]) -> int:
62-
if len(nums) == 0 :
63-
return 0
64-
65-
vals = set(nums) # O(n) creation, O(1) lookups
66-
67-
maxx = 0
68-
while vals :
69-
left = right = vals.pop()
70-
curr = 1
71-
while left - 1 in vals :
72-
left -= 1
73-
curr += 1
74-
vals.remove(left)
75-
while right + 1 in vals :
76-
right += 1
77-
curr += 1
78-
vals.remove(right)
79-
80-
maxx = max(maxx, curr)
81-
82-
return maxx
83-
84-
```
85-

0 commit comments

Comments
 (0)