Check if Array Is Sorted and Rotated
Real-world version: Turntable Weight OrderThe statement here is our own: the same inputs, outputs and technique.
A pottery studio loads clay trays onto a circular turntable, one tray per station around the rim. House procedure says the loader picks any station to begin at and then works clockwise around the full circle, never putting on a tray lighter than the one just placed. Two trays of equal weight next to each other are perfectly fine.
An inspector arrives later and reads the weights off clockwise into the array weights, but the inspector begins at whichever station happens to face the doorway, which is not usually the station the loader began at. So the reading is the loader's ordering turned around the rim by some unknown number of stations.
Decide whether the reading is consistent with house procedure: return true when some station exists such that reading weights clockwise from it, all the way around, never steps down in weight, and false when no station does that. The inspector's own starting station counts, so a reading that never steps down as written is consistent.
Examples
Example 1
- Input
- weights = [7, 9, 4, 5, 6]
- Output
- true
Beginning at the station holding 4 and going clockwise gives 4, 5, 6, 7, 9, which never steps down, so the loader could have started there.
Example 2
- Input
- weights = [5, 3, 8, 6]
- Output
- false
Reading clockwise from each station in turn gives 5, 3, 8, 6 then 3, 8, 6, 5 then 8, 6, 5, 3 then 6, 5, 3, 8. Every one of those steps down somewhere, so no station is consistent with procedure.
Example 3
- Input
- weights = [4, 4, 4]
- Output
- true
Equal weights are allowed side by side, so the reading never steps down at all and any station works.
Constraints
- 1 <= weights.length <= 100
- 1 <= weights[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def turntable_weight_order(weights: list[int]) -> bool:public boolean turntableWeightOrder(int[] weights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 19 test cases: 19 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's