Class Solution
-
- All Implemented Interfaces:
public final class Solution
2931 - Maximum Spending After Buying Items\.
Hard
You are given a 0-indexed
m * n
integer matrixvalues
, representing the values ofm * n
different items inm
different shops. Each shop hasn
items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value ofvalues[i][j]
. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is,values[i][j] >= values[i][j + 1]
for all0 <= j < n - 1
.On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:
Pick any shop
i
.Buy the rightmost available item
j
for the price ofvalues[i][j] * d
. That is, find the greatest indexj
such that itemj
was never bought before, and buy it for the price ofvalues[i][j] * d
.
Note that all items are pairwise different. For example, if you have bought item
0
from shop1
, you can still buy item0
from any other shop.Return the maximum amount of money that can be spent on buying all
m * n
products.Example 1:
Input: values = \[\[8,5,2],6,4,1,9,7,3]
Output: 285
Explanation: On the first day, we buy product 2 from shop 1 for a price of values2 \* 1 = 1.
On the second day, we buy product 2 from shop 0 for a price of values2 \* 2 = 4.
On the third day, we buy product 2 from shop 2 for a price of values2 \* 3 = 9.
On the fourth day, we buy product 1 from shop 1 for a price of values1 \* 4 = 16.
On the fifth day, we buy product 1 from shop 0 for a price of values1 \* 5 = 25.
On the sixth day, we buy product 0 from shop 1 for a price of values0 \* 6 = 36.
On the seventh day, we buy product 1 from shop 2 for a price of values1 \* 7 = 49.
On the eighth day, we buy product 0 from shop 0 for a price of values0 \* 8 = 64.
On the ninth day, we buy product 0 from shop 2 for a price of values0 \* 9 = 81.
Hence, our total spending is equal to 285.
It can be shown that 285 is the maximum amount of money that can be spent buying all m \* n products.
Example 2:
Input: values = \[\[10,8,6,4,2],9,7,5,3,2]
Output: 386
Explanation: On the first day, we buy product 4 from shop 0 for a price of values4 \* 1 = 2.
On the second day, we buy product 4 from shop 1 for a price of values4 \* 2 = 4.
On the third day, we buy product 3 from shop 1 for a price of values3 \* 3 = 9.
On the fourth day, we buy product 3 from shop 0 for a price of values3 \* 4 = 16.
On the fifth day, we buy product 2 from shop 1 for a price of values2 \* 5 = 25.
On the sixth day, we buy product 2 from shop 0 for a price of values2 \* 6 = 36.
On the seventh day, we buy product 1 from shop 1 for a price of values1 \* 7 = 49.
On the eighth day, we buy product 1 from shop 0 for a price of values1 \* 8 = 64
On the ninth day, we buy product 0 from shop 1 for a price of values0 \* 9 = 81.
On the tenth day, we buy product 0 from shop 0 for a price of values0 \* 10 = 100.
Hence, our total spending is equal to 386.
It can be shown that 386 is the maximum amount of money that can be spent buying all m \* n products.
Constraints:
1 <= m == values.length <= 10
<code>1 <= n == valuesi.length <= 10<sup>4</sup></code>
<code>1 <= valuesj<= 10<sup>6</sup></code>
values[i]
are sorted in non-increasing order.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Long
maxSpending(Array<IntArray> values)
-
-
Method Detail
-
maxSpending
final Long maxSpending(Array<IntArray> values)
-
-
-
-