Tensorum
YouTubeশেখা শুরু করুন
LeetCode 228 · Intervals

Summary Ranges

LeetCode 228 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Extend consecutive runs.

সহজArray
LeetCode-এ খুলুন ↗

আমি Summary Ranges (LeetCode 228) এভাবে সমাধান করেছি — Intervals প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Walk the sorted list and extend a run while each next number is exactly one more. When the run breaks, write it as 'a->b' (or just 'a' for a single number).

Approach

  1. 1

    start = nums[i].

  2. 2

    Advance while nums[i+1] == nums[i] + 1.

  3. 3

    Record start->end or start.

আমি কীভাবে সমাধান করেছি

Run extensionPythonO(n) time
class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        i=0
        res=[]
        n = len(nums)
        while i<n:
            start = nums[i]
            while i+1<n and nums[i+1]==nums[i]+1:
                i+=1
            end = nums[i]
            if start!=end:
                res.append(f"{start}->{end}")
            else:
                res.append(f"{start}")
            i+=1
        return res
স্টেপ-থ্রু লোড হচ্ছে…

Summary Ranges — সাধারণ প্রশ্ন

Summary Ranges-এর পেছনে intuition কী?

Walk the sorted list and extend a run while each next number is exactly one more. When the run breaks, write it as 'a->b' (or just 'a' for a single number).

Summary Ranges (LeetCode 228) কীভাবে সমাধান করবেন?

1. start = nums[i]. 2. Advance while nums[i+1] == nums[i] + 1. 3. Record start->end or start.

Summary Ranges-এর time আর space complexity কত?

Time: O(n)। Space: O(1) extra। (Accepted approach: Run extension।)

Summary Ranges-এ সাধারণ ভুলগুলো কী?

Empty input returns [].