LeetCode Entry
1752. Check if Array Is Sorted and Rotated
Is array shifted and sorted?
1752. Check if Array Is Sorted and Rotated easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1368
Problem TLDR
Is array shifted and sorted?
Intuition
- brute-force is accepted for 100 elements
- optimal way: count unordered elements
Approach
- the shortest Rust is n^2
- Rust itetools has circular_tuple_windows
Complexity
-
Time complexity: \(O(n|n^2)\)
-
Space complexity: \(O(1|n)\)
Code
fun check(n: IntArray) =
n.indices.count { n[it]>n[(it+1)%n.size]} < 2
pub fn check(n: Vec<i32>) -> bool {
n.repeat(2).windows(n.len()).any(|w|w.is_sorted())
}