Tensorum
YouTubeশেখা শুরু করুন
LeetCode 1752 · Linear Scan

Check if Array Is Sorted and Rotated

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

Count descents including the wrap-around; ≤ 1 means yes.

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

আমি Check if Array Is Sorted and Rotated (LeetCode 1752) এভাবে সমাধান করেছি — Linear Scan প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

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

Intuition

A sorted array rotated any amount has at most one 'drop' where a value is bigger than the next one, counting the wrap from the last element back to the first. More than one drop means it can't be a rotation of a sorted array.

Approach

  1. 1

    Count positions where nums[i−1] > nums[i].

  2. 2

    Also count the wrap if nums[n−1] > nums[0].

  3. 3

    Return count ≤ 1.

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

Count descentsPythonO(n) time · O(1) space
class Solution:
    def check(self, nums):
        count = 0
        n = len(nums)
        for i in range(1, n):
            if nums[i - 1] > nums[i]:
                count += 1
        if nums[n - 1] > nums[0]:
            count += 1
        if count <= 1:
            return True
        return False
স্টেপ-থ্রু লোড হচ্ছে…

Check if Array Is Sorted and Rotated — সাধারণ প্রশ্ন

Check if Array Is Sorted and Rotated-এর পেছনে intuition কী?

A sorted array rotated any amount has at most one 'drop' where a value is bigger than the next one, counting the wrap from the last element back to the first. More than one drop means it can't be a rotation of a sorted array.

Check if Array Is Sorted and Rotated (LeetCode 1752) কীভাবে সমাধান করবেন?

1. Count positions where nums[i−1] > nums[i]. 2. Also count the wrap if nums[n−1] > nums[0]. 3. Return count ≤ 1.

Check if Array Is Sorted and Rotated-এর time আর space complexity কত?

Time: O(n)। Space: O(1)। (Accepted approach: Count descents।)

Check if Array Is Sorted and Rotated-এ সাধারণ ভুলগুলো কী?

Equal neighbours are not drops.