Skip to content

Commit d2e62aa

Browse files
committed
Reducing Dishes
1 parent aefd7c5 commit d2e62aa

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

reducing-dishes_v1.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
2+
#
3+
# Like-time coefficient of a dish is defined as the time taken to cook that dish
4+
# including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
5+
#
6+
# Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
7+
#
8+
# Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
9+
10+
"""
11+
>>> Solution().maxSatisfaction([-1,-8,0,5,-9])
12+
14
13+
>>> Solution().maxSatisfaction([4,3,2])
14+
20
15+
>>> Solution().maxSatisfaction([-1,-4,-5])
16+
0
17+
>>> Solution().maxSatisfaction([-2,5,-1,0,3,-3])
18+
35
19+
"""
20+
from typing import List
21+
22+
23+
class Solution:
24+
# O(n^2) time | O(1) space
25+
def maxSatisfaction(self, satisfaction: List[int]) -> int:
26+
satisfaction.sort()
27+
factor = 1
28+
maxSumLikeTime = 0
29+
30+
while factor <= len(satisfaction):
31+
time = factor
32+
partialSumLikeTime = 0
33+
for i in reversed(range(len(satisfaction) - factor, len(satisfaction))):
34+
partialSumLikeTime += satisfaction[i] * time
35+
time -= 1
36+
if partialSumLikeTime < maxSumLikeTime:
37+
break
38+
maxSumLikeTime = partialSumLikeTime
39+
factor += 1
40+
41+
return maxSumLikeTime

reducing-dishes_v2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
2+
#
3+
# Like-time coefficient of a dish is defined as the time taken to cook that dish
4+
# including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
5+
#
6+
# Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
7+
#
8+
# Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
9+
10+
"""
11+
>>> Solution().maxSatisfaction([-1,-8,0,5,-9])
12+
14
13+
>>> Solution().maxSatisfaction([4,3,2])
14+
20
15+
>>> Solution().maxSatisfaction([-1,-4,-5])
16+
0
17+
>>> Solution().maxSatisfaction([-2,5,-1,0,3,-3])
18+
35
19+
"""
20+
from typing import List
21+
22+
23+
class Solution:
24+
# O(n log n) time | O(1) space
25+
def maxSatisfaction(self, satisfaction: List[int]) -> int:
26+
satisfaction.sort(reverse=True)
27+
partialSumLikeTime = maxSumLikeTime = 0
28+
for s in satisfaction:
29+
if partialSumLikeTime + s < 0:
30+
break
31+
partialSumLikeTime += s
32+
maxSumLikeTime += partialSumLikeTime
33+
34+
return maxSumLikeTime

0 commit comments

Comments
 (0)