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 [].