aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2023-12-20 01:01:59 +0000
committerterminaldweller <devi@terminaldweller.com>2023-12-20 01:01:59 +0000
commit7d31376408a514c760bb5694138b21184c726246 (patch)
tree48dccebfd53856c8257692c55b6c5081f0bdad41
parent661 (diff)
downloadleetcode-7d31376408a514c760bb5694138b21184c726246.tar.gz
leetcode-7d31376408a514c760bb5694138b21184c726246.zip
2706
-rwxr-xr-x2706/main.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/2706/main.py b/2706/main.py
new file mode 100755
index 0000000..697d312
--- /dev/null
+++ b/2706/main.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+import typing
+
+
+class Solution:
+ def buyChoco(self, prices: typing.List[int], money: int) -> int:
+ price_dict: typing.Dict = {}
+ for i in range(len(prices)):
+ price_dict[prices[i]] = (
+ price_dict[prices[i]] + 1 if prices[i] in price_dict else 1
+ )
+
+ mini = min(prices)
+ if price_dict[mini] >= 2:
+ if mini * 2 <= money:
+ return money - mini * 2
+ else:
+ return money
+
+ slider = mini + 1
+ maxi = max(prices)
+ while slider <= maxi:
+ if slider in price_dict:
+ if mini + slider <= money:
+ return money - mini - slider
+ slider += 1
+
+ return money
+
+
+def main():
+ solution = Solution()
+ print(solution.buyChoco([1, 2, 1, 3, 2], 4))
+ print(solution.buyChoco([10, 10, 10, 10], 20))
+ print(solution.buyChoco([1, 2, 2], 3))
+ print(solution.buyChoco([3, 2, 3], 3))
+
+
+if __name__ == "__main__":
+ main()