Summary Ranges - Leetcode 228 - Arrays & Strings (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Don't often comment on here but feel I have to say a huge thank you for making all these videos. Your explanations are great and although i find many of the problems difficult, I usually understand after watching two or three times. You're probably saving me from early onset dementia - I've watched quite a few other presenters on you tube explaining this stuff but you are the best by quite a margin in my opinion.

davidbuckingham
Автор

hey, just wanna let you know your free dsa leetcode roadmap is helpful af. I'm a 2nd year cs student and my dsa professor last semester couldn't teach for the sake of his life, and I'm learning more from here than anywhere else. Big Thanks!

zynthrix
Автор

awsome explination, definetly not seen this aproach in college as a second year
thanks so much Greg
!!

sebastianibarraherrera
Автор

My solution checks the difference between the next number and current number

class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
ans = []
n = len(nums)
i = 0

while i < n:
start = nums[i]
while i + 1 < n and nums[i + 1] - nums[i] == 1:
i += 1
if start != nums[i]:

else:
ans.append(str(start))
i += 1

return ans

ZaneFalcao
Автор

I really liked this solution, thanks a lot for sharing this valuable content.

christianjt
Автор

The trick is to start at a value, then loop again to see how many times a number is the increment of the start.

kidyoshi
Автор

def summaryRanges(self, nums: list) -> list:
# empty list
if not nums:
return []

# initialisation
a = nums[0]
b = nums[0]
ans = []

for i in range(1, len(nums)):
# tracks the end of interval
if nums[i] == b + 1:
b = nums[i]
continue

# empty interval or not
if a == b:
ans.append(str(a))
else:
ans.append(str(a) + "->" + str(b))

# next interval
a = nums[i]
b = nums[i]

# last interval
if a == b:
ans.append(str(a))
else:
ans.append(str(a) + "->" + str(b))

return ans

# time -> O(n)
# space -> O(n)

teamtitus
Автор

Thank you for providing this solution.😇😇

mohamedthaiebu
Автор

My appraoch -

class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
lst = []
l = 0
length = 0
for r in range(len(nums)):
if nums[r] != nums[l] + length:
if l == r-1:
lst.append(str(nums[l]))
else:
lst.append(str(nums[l]) + "->" + str(nums[r-1]))
length = 1
l = r
else:
length+=1

if l<len(nums)-1:
lst.append(str(nums[l]) + "->" + str(nums[r]))
elif l==len(nums)-1:
lst.append(str(nums[l]))

return lst

Rutik
Автор

omg so easy solution, i thought about using nested for loops

Ivan-Shyriaiev
Автор

What would you rate my solution? I used two pointers method

def summaryRanges(self, nums: List[int]) -> List[str]:
results = []
nums.append(-999)
lp = nums[0]

for i in range(1, len(nums)):
if nums[i-1] != nums[i]-1:
if(lp == nums[i-1]):
string = str(lp)
else:
string = str(lp)+"->"+str(nums[i-1])
results.append(string)
lp = nums[i]

return results

The nums.append(-999) is a bit of a hack but it passed all the test cases so i guess its fine

spongsquad
Автор

GREG HOGG + NEETCODE = 200 LEETCODE PROBLEMS SOLVED

atTien-kopg
welcome to shbcf.ru