Skip to content

Commit ad317cb

Browse files
committed
Updated solution for longest consecutive sequence
1 parent 7bd57f6 commit ad317cb

File tree

2 files changed

+27
-27
lines changed
  • content/posts/computer-science/interview-preparation/longest-consecutive

2 files changed

+27
-27
lines changed

config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ params:
9090
9191
- I document my learnings on computer science and mathematics through articles.
9292
93-
- Explore my <a target=_blank href="/posts/projects">personal projects</a> or check my <a target=_blank href="/resume/latest/Resume_Avnish_Pal.pdf">resume</a> for professional experience.
93+
- Explore my <a href="/posts/personal-projects">personal projects</a> or check my <a target=_blank href="/resume/latest/Resume_Avnish_Pal.pdf">resume</a> for professional experience.
9494
9595
- Connect with me on <a target=_blank href="https://github.com/bovem">GitHub</a> or <a target=_blank href="https://www.linkedin.com/in/avnish-pal/">LinkedIn</a> to discuss ideas or collaborate.
9696

content/posts/computer-science/interview-preparation/longest-consecutive/index.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ Once we have found the first element of consecutive sequence we can keep looking
153153

154154
<p align="center"><img src="longest-consecutive-optimized3.png" alt="Optimized solution for the longestConsecutive"></p>
155155

156+
During the second iteration we can utilize the hashmap to avoid duplicate values in the input array.
157+
156158
## Psuedo code for the Optimized Solution
157159
```text
158160
hashmap = HashMap()
@@ -162,10 +164,9 @@ loop value in inputArray
162164
163165
longest_sequence = 0
164166
sequence = 0
165-
loop index in inputArray
166-
value = inputArray[index]
167-
if not hashmap[value-1]
168-
while hashmap[value+1]
167+
loop key in hashmap.keys()
168+
if not hashmap[key-1]
169+
while hashmap[key+1]
169170
sequence += 1
170171
value+=1
171172
else
@@ -191,7 +192,6 @@ package main
191192

192193
import (
193194
"fmt"
194-
"math"
195195
)
196196

197197
func longestConsecutive(nums []int)(int){
@@ -210,39 +210,39 @@ func longestConsecutive(nums []int)(int){
210210
}
211211
}
212212

213-
longestSequence := 1
214-
sequence := 1
215-
216-
for index:=0;index<len(nums);index++{
217-
value := nums[index]
218-
_, key_exists := hashmap[value-1]
219-
213+
maxSeqLen := 1
214+
for value, _ := range hashmap {
215+
seqLen := 1
216+
220217
// If value-1 does not exist in the hashmap
221218
// it is the start of a sequence
222-
if !key_exists{
223-
224-
// Increment the value and sequence length
225-
// until we can't find value+1 in the hashmap
226-
for ;true;{
227-
value+=1
228-
_, key_exists = hashmap[value]
229-
if key_exists{
230-
sequence+=1
219+
_, found := hashmap[value-1]
220+
221+
if !found {
222+
for {
223+
224+
// Increment the value and sequence length
225+
// until we can't find value+1 in the hashmap
226+
value++
227+
_, found = hashmap[value]
228+
if found {
229+
seqLen++
231230
} else {
232231
break
233232
}
233+
234234
}
235235
}
236-
236+
237237
// Reset the value of longestSequence to the maximum
238238
// of current sequence and the current value of
239239
// the longestSequence
240-
longestSequence = int(math.Max(float64(sequence),
241-
float64(longestSequence)))
242-
sequence = 1
240+
if seqLen > maxSeqLen {
241+
maxSeqLen = seqLen
242+
}
243243
}
244244

245-
return longestSequence
245+
return maxSeqLen
246246
}
247247

248248
func main(){

0 commit comments

Comments
 (0)