Amazon aims to optimally package parcels to improve efficiency. There are n parcels…
- User
Amazon aims to optimally package parcels to improve efficiency. There are n parcels (0-based Indexing), and the price of the /th parcel is denoted by prices[i]. Amazon uses five different stamps (Indexed from 1 to 5) to label the parcels.
Packaging is done sequentially from left to right, and each parcel must be labeled with one of the five stamps. A packaging is considered perfect if it adheres to the following rules (for i > 0):
• If prices[i] > prices[i-1], then stamp[i] > stamp[i-1] (where stamp(i) is the Index of the stamp used to label the parcel).
• If prices[i] < prices[i-1], then stamp[i] < stamp[i-1].
• If prices[i] = prices[i-1], then stamp[i] = stamp[i-1].
Given an array prices of n Integers, find the number of perfect packaging possibilities using the five different stamps. Since the number of perfect packaging can be large, return the result modulo (109 + 7).
Note: If no valid packaging can be done based on the given rules, return 0.
Example
n=3
prices = [1, 2, 3]
There are 10 perfect packaging combinations that can be done by labeling the parcels with the following Indexed
stamps:
[1, 2, 3]
[1,2,4]
[1,2,5]
[1,3,4]
[1.3.5]
[1,4,5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
For example, [1, 2, 3] means that the first parcel is labeled with the first-Indexed stamp, the second parcel with the second-Indexed stamp, and the third parcel with the third-indexed stamp.
Hence, the answer is 10.
Function Description
Complete the function getNumPerfectPackaging In the editor below.
getNumPerfectPackaging has the following parameter: int prices[n]: the price of the parcels
Returns
int: the number of perfect packaging that can be done modulo (109+7)
- Assistant