Skip to content

Commit bd81ef4

Browse files
committed
Solution of latest contest
1 parent f8af46c commit bd81ef4

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

1100-1200q/1189.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
3+
You can use each character in text at most once. Return the maximum number of instances that can be formed.
4+
5+
6+
Example 1:
7+
Input: text = "nlaebolko"
8+
Output: 1
9+
10+
Example 2:
11+
Input: text = "loonbalxballpoon"
12+
Output: 2
13+
14+
Example 3:
15+
Input: text = "leetcode"
16+
Output: 0
17+
18+
19+
Constraints:
20+
21+
1 <= text.length <= 10^4
22+
text consists of lower case English letters only.
23+
'''
24+
25+
class Solution(object):
26+
def maxNumberOfBalloons(self, text):
27+
"""
28+
:type text: str
29+
:rtype: int
30+
"""
31+
if not text:
32+
return 0
33+
34+
import collections
35+
cnt = collections.Counter(text)
36+
cnt_ballon = collections.Counter('balloon')
37+
38+
return min([cnt[c]//cnt_ballon[c] for c in cnt_ballon])

1100-1200q/1190.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Given a string s that consists of lower case English letters and brackets.
3+
4+
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
5+
6+
Your result should not contain any bracket.
7+
8+
9+
Example 1:
10+
Input: s = "(abcd)"
11+
Output: "dcba"
12+
13+
Example 2:
14+
Input: s = "(u(love)i)"
15+
Output: "iloveu"
16+
17+
Example 3:
18+
Input: s = "(ed(et(oc))el)"
19+
Output: "leetcode"
20+
21+
Example 4:
22+
Input: s = "a(bcdefghijkl(mno)p)q"
23+
Output: "apmnolkjihgfedcbq"
24+
25+
26+
Constraints:
27+
28+
0 <= s.length <= 2000
29+
s only contains lower case English characters and parentheses.
30+
It's guaranteed that all parentheses are balanced.
31+
'''
32+
33+
class Solution(object):
34+
def reverseParentheses(self, s):
35+
"""
36+
:type s: str
37+
:rtype: str
38+
"""
39+
if not s:
40+
return ''
41+
42+
stack = []
43+
for char in s:
44+
if char == ')':
45+
combine_str = ''
46+
while stack and stack[-1] != '(':
47+
elem = stack.pop()[::-1]
48+
combine_str += elem
49+
stack.pop()
50+
stack.append(combine_str)
51+
else:
52+
stack.append(char)
53+
return "".join(stack)

1100-1200q/1191.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Given an integer array arr and an integer k, modify the array by repeating it k times.
3+
4+
For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].
5+
6+
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.
7+
8+
As the answer can be very large, return the answer modulo 10^9 + 7.
9+
10+
11+
12+
Example 1:
13+
14+
Input: arr = [1,2], k = 3
15+
Output: 9
16+
Example 2:
17+
18+
Input: arr = [1,-2,1], k = 5
19+
Output: 2
20+
Example 3:
21+
22+
Input: arr = [-1,-2], k = 7
23+
Output: 0
24+
25+
26+
Constraints:
27+
28+
1 <= arr.length <= 10^5
29+
1 <= k <= 10^5
30+
-10^4 <= arr[i] <= 10^4
31+
'''
32+
class Solution(object):
33+
def kConcatenationMaxSum(self, arr, k):
34+
"""
35+
:type arr: List[int]
36+
:type k: int
37+
:rtype: int
38+
"""
39+
def kadane(arr):
40+
curr_sum, max_sum = arr[0], arr[0]
41+
for index in range(1, len(arr)):
42+
curr_sum = max(arr[index], curr_sum + arr[index])
43+
max_sum = max(max_sum, curr_sum)
44+
return max_sum
45+
46+
def prefix(arr):
47+
curr_sum, max_val = 0, float('-inf')
48+
for index, val in enumerate(arr):
49+
curr_sum += val
50+
max_val = max(max_val, curr_sum)
51+
return max_val
52+
53+
def suffix(arr):
54+
curr_sum, max_val = 0, float('-inf')
55+
for index in range(len(arr)-1, -1, -1):
56+
curr_sum += arr[index]
57+
max_val = max(max_val, curr_sum)
58+
return max_val
59+
60+
if not arr:
61+
return 0
62+
if k == 1:
63+
return max(0, kadane(arr)) % (10 ** 9 + 7)
64+
else:
65+
return max(0, max((prefix(arr) + suffix(arr) + (k-2)*max(sum(arr), 0), kadane(arr)))) % (10 ** 9 + 7)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1515
##### [Problems 1100-1200](./1100-1200q/)
1616
| # | Title | Solution | Difficulty |
1717
|---| ----- | -------- | ---------- |
18+
|1191|[K-Concatenation Maximum Sum](https://leetcode.com/problems/k-concatenation-maximum-sum/)|[Python](./1100-1200q/1191.py)|Medium|
19+
|1190|[Reverse Substrings Between Each Pair of Parentheses](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/)|[Python](./1100-1200q/1190.py)|Medium|
20+
|1189|[ Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)|[Python](./1100-1200q/1189.py)|Easy|
1821
|1186|[Maximum Subarray Sum with One Deletion](https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/)|[Python](./1100-1200q/1186.py)|Medium|
1922
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Python](./1100-1200q/1185.py)|Easy|
2023
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Python](./1100-1200q/1184.py)|Easy|

0 commit comments

Comments
 (0)