Skip to the code
cf.
+25 XP

#265 Next Larger in a Looping Carousel

medium

A looping product carousel shows cards in a circle: after the last card comes the first again. ratings[i] is card i's rating.

Write nextLargerAround(ratings) that returns, for each card, the first strictly larger rating you meet when moving forward around the circle from it, or -1 if no card is larger.

Example 1

Input: ratings = [1,2,1]
Output: [2,-1,2]

The last 1 wraps around to the first card and keeps going until it meets 2.

Example 2

Input: ratings = [1,2,3,4,3]
Output: [2,3,4,-1,4]

The last 3 wraps around and finds 4.

Constraints

  • 1 <= ratings.length <= 10^4
  • -10^9 <= ratings[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).