Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 135 · Local Choice / Two-pass

Candy

LeetCode 135 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Two passes, take the max.

ಕಠಿಣArrayGreedy
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Candy (LeetCode 135) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Local Choice / Two-pass ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Two constraints, one per direction. Left to right, a child rated higher than the left neighbour needs one more candy; right to left, likewise for the right neighbour. Taking the max of both passes satisfies both. A one-pass version counts lengths of up and down slopes.

Approach

  1. 1

    Start everyone with 1.

  2. 2

    Left pass: higher than left → left + 1.

  3. 3

    Right pass: higher than right → max(current, right + 1).

  4. 4

    Sum.

  5. 5

    Slope version: add up and down run lengths, fixing the peak with max(up, down).

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Two passesO(n) time · O(n) space

Left and right constraints.

2
Slope countingAcceptedO(n) time · O(1) space

Up and down runs.

Two passesPythonO(n) time · O(n) space
class Solution:
    def candy(self, ratings: List[int]) -> int:
        n = len(ratings)
        arr = [1]*n
        for i in range(1,n):
            if ratings[i]>ratings[i-1]:
                arr[i]=arr[i-1]+1

        for i in range(n-2,-1,-1):
            if ratings[i]>ratings[i+1]:
                arr[i]=max(arr[i],arr[i+1]+1)
        return sum(arr)
Slope countingPythonO(n) time · O(1) space
class Solution:
    def candy(self, ratings: List[int]) -> int:
        cur_sum=1
        i=1
        n = len(ratings)
        while i<n:
            if i<n and ratings[i]==ratings[i-1]:
                cur_sum+=1
                i+=1
            up=1
            while i<n and ratings[i]>ratings[i-1]:
                up+=1
                cur_sum+=up
                i+=1
            down=1
            while i<n and ratings[i]<ratings[i-1]:
                cur_sum+=down
                down+=1
                i+=1
            if down>up:
                cur_sum+=down-up
        return cur_sum
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Candy — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Candy ಹಿಂದಿನ intuition ಏನು?

Two constraints, one per direction. Left to right, a child rated higher than the left neighbour needs one more candy; right to left, likewise for the right neighbour. Taking the max of both passes satisfies both. A one-pass version counts lengths of up and down slopes.

Candy (LeetCode 135) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Start everyone with 1. 2. Left pass: higher than left → left + 1. 3. Right pass: higher than right → max(current, right + 1). 4. Sum. 5. Slope version: add up and down run lengths, fixing the peak with max(up, down).

Candy ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n). Space: O(n) two-pass · O(1) slope. (Accepted approach: Slope counting.)

Candy ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Equal ratings don't need more candy.

Candy ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Two passes (O(n) time · O(n) space): Left and right constraints. Slope counting (O(n) time · O(1) space): Up and down runs.