mediumArraysPattern: pattern sorting by custom criteria

Rearrange Seats by Rating Solution

Problem Statement

You are given an array of integers `seats` representing seat popularity ratings. Rearrange the seats such that the highest ratings are at the center, and the lower ratings are towards the sides. Specifically, seats at odd indices should have higher ratings than seats at even indices. If there is an odd number of seats, the middle seat should have the highest rating. Return the rearranged array of seat ratings.

Constraints

  • The input array is never empty
  • The total number of seats is within the range 2 <= n <= 2*10^5
  • All seats in the input array are unique, and the ratings are guaranteed to be non-negative
  • The ratings of seats can be the same
  • If there are an odd number of seats, the middle seat should have a higher rating.
Time: O(n log n) Space: O(1)
The optimized approach involves sorting the seats based on their popularity and then using a two-pointer approach to rearrange the seats in O(n log n) time complexity.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.