Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #3

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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]
#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)]
64 changes: 64 additions & 0 deletions 682. Baseball Game/682. Baseball Game.py
Original file line number Diff line number Diff line change
@@ -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)