Problem ยท Dynamic Programming
Find Niceness ๐บ
Learn this problemProblem statement
Ramu and Sonu each have N chalks with distinct positive lengths. Array a gives Ramu's arrangement, while array b contains Sonu's chalk lengths.
Permute b so that every adjacent comparison has the same direction as the corresponding comparison in a. If a[i] < a[i + 1], Sonu's values at those positions must increase; if a[i] > a[i + 1], they must decrease.
The niceness of an arrangement is the sum of the absolute differences between adjacent lengths. Return the maximum niceness among all arrangements that match Ramu's comparison pattern.
Function
findNiceness(N: int, a: int[], b: int[]) โ intExamples
Example 1
N = 4a = [5, 7, 4, 9]b = [1, 2, 3, 4]return = 7Ramu's comparison pattern is increase, decrease, increase. The arrangement [2,4,1,3] follows that pattern and has niceness |2-4| + |4-1| + |1-3| = 7. No valid permutation has a larger value, so the answer is 7.
Constraints
N == a.length == b.length- Both arrays contain
Ndistinct positive integers.