Skip to the code
+25 XP

#298 Distinct Playlist Orders

medium

A music app shuffles a playlist, but it only shows each song's genre ID, given in genres. Many songs share a genre, so different shuffles can look the same.

Write distinctGenreOrders(genres) that returns every distinct sequence of genre IDs that a shuffle can show. The sequences may be listed in any order, but each must appear exactly once.

Example 1

Input: genres = [1,1,2]
Output: [[1,1,2],[1,2,1],[2,1,1]]

There are 6 shuffles of 3 songs, but swapping the two genre-1 songs looks the same, so only 3 sequences are distinct.

Example 2

Input: genres = [3,3]
Output: [[3,3]]

Both songs show genre 3, so every shuffle looks the same.

Constraints

  • 1 <= genres.length <= 16
  • 0 <= genres[i] <= 9
  • There are at most 20,000 distinct sequences.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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