1793. Rising Picks

Medium3.5 points estimatedBacktrackingHash TableArray

A game show shows contestants a row of scores, given as the array nums. A contestant may keep some of the scores, in their original left-to-right order, as a rising pick if each kept score is at least as large as the one before it. A pick must keep at least two scores.

Return every distinct rising pick as a list of its score values. Two picks are the same when their value lists are equal, even if they use different positions, and each distinct list must appear exactly once. Sort the returned lists in ascending lexicographic order, where a list that is a prefix of another comes first. If there is no rising pick, return an empty list. Backtrack over positions, and at each depth skip a value that has already been tried at that depth, so duplicates are never built. With n = nums.length at most 8 there are at most 2^n picks, so time is O(2^n * n) and extra space is O(n) apart from the output.

Example 1

Input:
nums = [2,5,2,5]
Output:
[[2,2],[2,2,5],[2,5],[2,5,5],[5,5]]
Explanation:

The distinct rising picks are [2,2], [2,5], [2,5,5], [2,2,5] and [5,5], listed in lexicographic order; the exact list is the output.

Example 2

Input:
nums = [3,2,1]
Output:
[]
Explanation:

The scores strictly fall, so no two kept scores can be in rising order and the result is empty.

Example 3

Input:
nums = [1,1]
Output:
[[1,1]]
Explanation:

The only pick is the pair [1,1], counted once.

Constraints

1 ≤ nums.length ≤ 8

-9 ≤ nums[i] ≤ 9

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(2^n * n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…