Skip to content

Commit 639b6ae

Browse files
authored
Merge pull request 149ps#3 from 149ps/main
Main
2 parents 5b009ad + 71378f5 commit 639b6ae

File tree

4 files changed

+173
-15
lines changed

4 files changed

+173
-15
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
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).
3+
4+
Return the largest possible value of num after any number of swaps.
5+
6+
7+
8+
Example 1:
9+
10+
Input: num = 1234
11+
Output: 3412
12+
Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.
13+
Swap the digit 2 with the digit 4, this results in the number 3412.
14+
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
15+
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
16+
Example 2:
17+
18+
Input: num = 65875
19+
Output: 87655
20+
Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.
21+
Swap the first digit 5 with the digit 7, this results in the number 87655.
22+
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
23+
24+
25+
Constraints:
26+
27+
1 <= num <= 109
28+
"""
29+
class Solution:
30+
def largestInteger(self, num: int) -> int:
31+
odd,even,odd_nums,even_nums = set(),set(),[],[]
32+
for i,digit in enumerate(str(num)):
33+
if int(digit) % 2 == 0:
34+
even.add(i)
35+
even_nums.append(int(digit))
36+
else:
37+
odd.add(i)
38+
odd_nums.append(int(digit))
39+
odd_nums.sort(reverse=True)
40+
even_nums.sort(reverse=True)
41+
result,o,e = [],0,0
42+
for i in range(len(str(num))):
43+
if i in odd:
44+
result.append(str(odd_nums[o]))
45+
o += 1
46+
elif i in even:
47+
result.append(str(even_nums[e]))
48+
e += 1
49+
return int(''.join(result))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
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.
3+
4+
Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [0,4], k = 5
11+
Output: 20
12+
Explanation: Increment the first number 5 times.
13+
Now nums = [5, 4], with a product of 5 * 4 = 20.
14+
It can be shown that 20 is maximum product possible, so we return 20.
15+
Note that there may be other ways to increment nums to have the maximum product.
16+
Example 2:
17+
18+
Input: nums = [6,3,3,2], k = 2
19+
Output: 216
20+
Explanation: Increment the second number 1 time and increment the fourth number 1 time.
21+
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
22+
It can be shown that 216 is maximum product possible, so we return 216.
23+
Note that there may be other ways to increment nums to have the maximum product.
24+
25+
26+
Constraints:
27+
28+
1 <= nums.length, k <= 105
29+
0 <= nums[i] <= 106
30+
"""
31+
class Solution:
32+
def maximumProduct(self, nums: List[int], k: int) -> int:
33+
if len(nums) == 1: return nums[0] + k
34+
min_heap = nums
35+
heapq.heapify(min_heap)
36+
while k > 0:
37+
current = heapq.heappop(min_heap)
38+
cur_diff = min_heap[0] - current
39+
if cur_diff == 0:
40+
current += 1
41+
k -= 1
42+
elif cur_diff > k:
43+
current += k
44+
k = 0
45+
else:
46+
current += cur_diff
47+
k -= cur_diff
48+
heapq.heappush(min_heap,current)
49+
result = 1
50+
while len(min_heap) > 0:
51+
cur = heapq.heappop(min_heap)
52+
result = (result * cur) % (10**9+7)
53+
return result
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
class Solution:
22
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3-
hmap = {}
4-
for item in nums:
5-
if hmap.get(item):
6-
hmap[item] += 1
7-
else:
8-
hmap[item] = 1
9-
result = []
10-
heapq.heapify(result)
11-
for key,val in hmap.items():
12-
if len(result) < k:
13-
heapq.heappush(result,(val,key))
14-
else:
15-
if val > result[0][0]:
16-
heapq.heapreplace(result,(val,key))
17-
return [arr[1] for arr in result]
3+
#Calculate the frequency of all the elements in an array.
4+
hmap = collections.Counter(nums)
5+
# Create a max_heap of size n. (We can create a max_heap using min_heap by multiplying frequency by -1.)
6+
max_heap = [(-v,k) for k,v in hmap.items()]
7+
heapq.heapify(max_heap)
8+
#pop k elements from max_heap.
9+
return [heapq.heappop(max_heap)[1] for _ in range(k)]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
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.
3+
4+
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:
5+
6+
An integer x - Record a new score of x.
7+
"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
8+
"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
9+
"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
10+
Return the sum of all the scores on the record.
11+
12+
13+
14+
Example 1:
15+
16+
Input: ops = ["5","2","C","D","+"]
17+
Output: 30
18+
Explanation:
19+
"5" - Add 5 to the record, record is now [5].
20+
"2" - Add 2 to the record, record is now [5, 2].
21+
"C" - Invalidate and remove the previous score, record is now [5].
22+
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
23+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
24+
The total sum is 5 + 10 + 15 = 30.
25+
Example 2:
26+
27+
Input: ops = ["5","-2","4","C","D","9","+","+"]
28+
Output: 27
29+
Explanation:
30+
"5" - Add 5 to the record, record is now [5].
31+
"-2" - Add -2 to the record, record is now [5, -2].
32+
"4" - Add 4 to the record, record is now [5, -2, 4].
33+
"C" - Invalidate and remove the previous score, record is now [5, -2].
34+
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
35+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
36+
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
37+
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
38+
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
39+
Example 3:
40+
41+
Input: ops = ["1"]
42+
Output: 1
43+
44+
45+
Constraints:
46+
47+
1 <= ops.length <= 1000
48+
ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
49+
For operation "+", there will always be at least two previous scores on the record.
50+
For operations "C" and "D", there will always be at least one previous score on the record.
51+
"""
52+
class Solution:
53+
def calPoints(self, ops: List[str]) -> int:
54+
result = []
55+
for op in ops:
56+
if op.lstrip("-").isdigit():
57+
result.append(int(op))
58+
elif op == "+":
59+
result.append(result[-1]+result[-2])
60+
elif op == "D":
61+
result.append(2*result[-1])
62+
elif op == "C":
63+
result.pop()
64+
return sum(result)

0 commit comments

Comments
 (0)