From 6bb83b60e57f4c1e8f44f4b57cf1c78e738fffd8 Mon Sep 17 00:00:00 2001 From: sumanth-botlagunta Date: Wed, 29 Mar 2023 23:27:24 +0530 Subject: [PATCH] 1402: reducing dishes --- python/reducing_dishes.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 python/reducing_dishes.py diff --git a/python/reducing_dishes.py b/python/reducing_dishes.py new file mode 100644 index 0000000..bafc396 --- /dev/null +++ b/python/reducing_dishes.py @@ -0,0 +1,17 @@ +# https://leetcode.com/problems/reducing-dishes/ +# T: O(nlogn) where n is the length of satisfaction +# S: O(1) + +class Solution: + def maxSatisfaction(self, satisfaction: List[int]) -> int: + satisfaction.sort(reverse = True) + n = len(satisfaction) + presum , res = 0, 0 + + for i in range(n): + presum += satisfaction[i] + if presum < 0: + break + res = res + presum + + return res