Skip to content

Commit ae737c3

Browse files
author
Partho Biswas
committed
Largest_Range
1 parent 20dcd18 commit ae737c3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ A place where I upload my daily practice on Data Structure and Algorithm problem
134134
|38| [Min_Number_Of_Jumps](algoexpert.io/questions/Min_Number_Of_Jumps.md) | [Python](algoexpert.io/python/Min_Number_Of_Jumps.py) | [Youtube Video 01](https://www.youtube.com/watch?v=cETfFsSTGJI)|
135135
|39| [Four_Number_Sum](algoexpert.io/questions/Four_Number_Sum.md) | [Python](algoexpert.io/python/Four_Number_Sum.py) |
136136
|40| [Subarray_Sort](algoexpert.io/questions/Subarray_Sort.md) | [Python](algoexpert.io/python/Subarray_Sort.py) |
137+
|41| [Largest_Range](algoexpert.io/questions/Largest_Range.md) | [Python](algoexpert.io/python/Largest_Range.py) |
137138

138139

139140

algoexpert.io/python/Largest_Range.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
# O(n) time | O(n) space
4+
def largestRange(array):
5+
bestRange = []
6+
longestLength = 0
7+
nums = {}
8+
for num in array:
9+
nums[num] = True
10+
for num in array:
11+
if not nums[num]:
12+
continue
13+
nums[num] = False
14+
currentLenght = 1
15+
left = num - 1
16+
right = num + 1
17+
while left in nums:
18+
nums[left] = False
19+
currentLenght += 1
20+
left -= 1
21+
while right in nums:
22+
nums[right] = False
23+
currentLenght += 1
24+
right += 1
25+
if currentLenght > longestLength:
26+
longestLength = currentLenght
27+
bestRange = [left + 1, right - 1]
28+
return bestRange

0 commit comments

Comments
 (0)