-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1648.py
More file actions
26 lines (21 loc) · 681 Bytes
/
1648.py
File metadata and controls
26 lines (21 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from typing import List
class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
inventory.sort(reverse=True)
n = len(inventory)
res = 0
for i, v in enumerate(inventory):
d = v - inventory[i+1] if i + 1 < n else v
w = i + 1
h, orders = divmod(orders, w)
if not h:
res += orders * v
break
if d <= h:
res += w * (v+v-d+1) * d // 2
orders += (h-d) * w
else:
res += w * (v+v-h+1) * h // 2
res += orders * (v-h+1)
break
return res