diff --git a/2231. Largest Number After Digit Swaps by Parity/2231. Largest Number After Digit Swaps by Parity.py b/2231. Largest Number After Digit Swaps by Parity/2231. Largest Number After Digit Swaps by Parity.py new file mode 100644 index 0000000..eb27a7c --- /dev/null +++ b/2231. Largest Number After Digit Swaps by Parity/2231. Largest Number After Digit Swaps by Parity.py @@ -0,0 +1,49 @@ +""" +You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits). + +Return the largest possible value of num after any number of swaps. + + + +Example 1: + +Input: num = 1234 +Output: 3412 +Explanation: Swap the digit 3 with the digit 1, this results in the number 3214. +Swap the digit 2 with the digit 4, this results in the number 3412. +Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number. +Also note that we may not swap the digit 4 with the digit 1 since they are of different parities. +Example 2: + +Input: num = 65875 +Output: 87655 +Explanation: Swap the digit 8 with the digit 6, this results in the number 85675. +Swap the first digit 5 with the digit 7, this results in the number 87655. +Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number. + + +Constraints: + +1 <= num <= 109 +""" +class Solution: + def largestInteger(self, num: int) -> int: + odd,even,odd_nums,even_nums = set(),set(),[],[] + for i,digit in enumerate(str(num)): + if int(digit) % 2 == 0: + even.add(i) + even_nums.append(int(digit)) + else: + odd.add(i) + odd_nums.append(int(digit)) + odd_nums.sort(reverse=True) + even_nums.sort(reverse=True) + result,o,e = [],0,0 + for i in range(len(str(num))): + if i in odd: + result.append(str(odd_nums[o])) + o += 1 + elif i in even: + result.append(str(even_nums[e])) + e += 1 + return int(''.join(result)) \ No newline at end of file diff --git a/2233. Maximum Product After K Increments/2233. Maximum Product After K Increments.py b/2233. Maximum Product After K Increments/2233. Maximum Product After K Increments.py new file mode 100644 index 0000000..c725635 --- /dev/null +++ b/2233. Maximum Product After K Increments/2233. Maximum Product After K Increments.py @@ -0,0 +1,53 @@ +""" +You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1. + +Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. + + + +Example 1: + +Input: nums = [0,4], k = 5 +Output: 20 +Explanation: Increment the first number 5 times. +Now nums = [5, 4], with a product of 5 * 4 = 20. +It can be shown that 20 is maximum product possible, so we return 20. +Note that there may be other ways to increment nums to have the maximum product. +Example 2: + +Input: nums = [6,3,3,2], k = 2 +Output: 216 +Explanation: Increment the second number 1 time and increment the fourth number 1 time. +Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216. +It can be shown that 216 is maximum product possible, so we return 216. +Note that there may be other ways to increment nums to have the maximum product. + + +Constraints: + +1 <= nums.length, k <= 105 +0 <= nums[i] <= 106 +""" +class Solution: + def maximumProduct(self, nums: List[int], k: int) -> int: + if len(nums) == 1: return nums[0] + k + min_heap = nums + heapq.heapify(min_heap) + while k > 0: + current = heapq.heappop(min_heap) + cur_diff = min_heap[0] - current + if cur_diff == 0: + current += 1 + k -= 1 + elif cur_diff > k: + current += k + k = 0 + else: + current += cur_diff + k -= cur_diff + heapq.heappush(min_heap,current) + result = 1 + while len(min_heap) > 0: + cur = heapq.heappop(min_heap) + result = (result * cur) % (10**9+7) + return result \ No newline at end of file diff --git a/347. Top K Frequent Elements/347. Top K Frequent Elements_nlogk_solution.py b/347. Top K Frequent Elements/347. Top K Frequent Elements_nlogk_solution.py index 0336919..9a99056 100644 --- a/347. Top K Frequent Elements/347. Top K Frequent Elements_nlogk_solution.py +++ b/347. Top K Frequent Elements/347. Top K Frequent Elements_nlogk_solution.py @@ -1,17 +1,9 @@ class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: - hmap = {} - for item in nums: - if hmap.get(item): - hmap[item] += 1 - else: - hmap[item] = 1 - result = [] - heapq.heapify(result) - for key,val in hmap.items(): - if len(result) < k: - heapq.heappush(result,(val,key)) - else: - if val > result[0][0]: - heapq.heapreplace(result,(val,key)) - return [arr[1] for arr in result] \ No newline at end of file + #Calculate the frequency of all the elements in an array. + hmap = collections.Counter(nums) + # Create a max_heap of size n. (We can create a max_heap using min_heap by multiplying frequency by -1.) + max_heap = [(-v,k) for k,v in hmap.items()] + heapq.heapify(max_heap) + #pop k elements from max_heap. + return [heapq.heappop(max_heap)[1] for _ in range(k)] \ No newline at end of file diff --git a/682. Baseball Game/682. Baseball Game.py b/682. Baseball Game/682. Baseball Game.py new file mode 100644 index 0000000..f7ffc46 --- /dev/null +++ b/682. Baseball Game/682. Baseball Game.py @@ -0,0 +1,64 @@ +""" +You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores. + +At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following: + +An integer x - Record a new score of x. +"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores. +"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score. +"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score. +Return the sum of all the scores on the record. + + + +Example 1: + +Input: ops = ["5","2","C","D","+"] +Output: 30 +Explanation: +"5" - Add 5 to the record, record is now [5]. +"2" - Add 2 to the record, record is now [5, 2]. +"C" - Invalidate and remove the previous score, record is now [5]. +"D" - Add 2 * 5 = 10 to the record, record is now [5, 10]. +"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15]. +The total sum is 5 + 10 + 15 = 30. +Example 2: + +Input: ops = ["5","-2","4","C","D","9","+","+"] +Output: 27 +Explanation: +"5" - Add 5 to the record, record is now [5]. +"-2" - Add -2 to the record, record is now [5, -2]. +"4" - Add 4 to the record, record is now [5, -2, 4]. +"C" - Invalidate and remove the previous score, record is now [5, -2]. +"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4]. +"9" - Add 9 to the record, record is now [5, -2, -4, 9]. +"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5]. +"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14]. +The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27. +Example 3: + +Input: ops = ["1"] +Output: 1 + + +Constraints: + +1 <= ops.length <= 1000 +ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104]. +For operation "+", there will always be at least two previous scores on the record. +For operations "C" and "D", there will always be at least one previous score on the record. +""" +class Solution: + def calPoints(self, ops: List[str]) -> int: + result = [] + for op in ops: + if op.lstrip("-").isdigit(): + result.append(int(op)) + elif op == "+": + result.append(result[-1]+result[-2]) + elif op == "D": + result.append(2*result[-1]) + elif op == "C": + result.pop() + return sum(result) \ No newline at end of file