You are given a string s and two integers x
and y. You can perform two types of operations any number
of times.
Remove substring "ab" and gain x points.
For example, when removing "ab" from
"cabxbae" it becomes "cxbae".
Remove substring "ba" and gain y points.
For example, when removing "ba" from
"cabxbae" it becomes "cabxe".
Return the maximum points you can gain after applying the above
operations on s.
1 2 3 4 5 6 7 8 9 10 11 12 13
Example 1: Input: s = "cdbcbbaaabab", x = 4, y = 5 Output: 19 Explanation: - Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score. - Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score. - Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score. - Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score. Total score = 5 + 4 + 5 + 5 = 19.
Example 2: Input: s = "aabbaaxybbaabb", x = 5, y = 4 Output: 20
classSolution: defmaximumGain(self, s: str, x: int, y: int) -> int: defhelper(high_match, low_match, high_score, low_score): stack = [] ans = 0 for i in s: stack.append(i) if i notin ['a', 'b']: new_stack = [] for i in stack: new_stack.append(i) if new_stack[-2:] == low_match: ans += low_score new_stack.pop() new_stack.pop() stack = [] if stack[-2:] == high_match: ans += high_score stack.pop() stack.pop() if stack: new_stack = [] for i in stack: new_stack.append(i) if new_stack[-2:] == low_match: ans += low_score new_stack.pop() new_stack.pop() return ans if x > y: return helper(['a', 'b'], ['b', 'a'], x, y) else: return helper(['b', 'a'], ['a', 'b'], y, x)
classSolution: defmaximumGain(self, s: str, x: int, y: int) -> int: high_prefix = 'a' # make x > y if y > x: high_prefix = 'b' x, y = y, x count_high = 0 count_low = 0 count_high_low = 0 ans = 0 for i in s: if i notin'ab': ans += count_high_low * x + max(min(count_high-count_high_low, count_low - count_high_low), 0) * y count_high = 0 count_low = 0 count_high_low = 0 else: if i == high_prefix: count_high += 1 else: count_low += 1 if count_high > count_high_low: count_high_low += 1 ans += count_high_low * x + max(min(count_high-count_high_low, count_low - count_high_low), 0) * y return ans
1190.
Reverse Substrings Between Each Pair of Parentheses
You are given a string s that consists of lower case English letters
and brackets.
Reverse the strings in each pair of matching parentheses, starting
from the innermost one.
Your result should not contain any brackets.
1 2 3 4 5 6 7 8 9 10 11 12 13
Example 1: Input: s = "(abcd)" Output: "dcba"
Example 2: Input: s = "(u(love)i)" Output: "iloveu" Explanation: The substring "love" is reversed first, then the whole string is reversed.
Example 3: Input: s = "(ed(et(oc))el)" Output: "leetcode" Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
stack(bruteforce)
最简单的思路, stack记录index,
检测到括号就翻转stack[-1]到i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defreverseParentheses(self, s: str) -> str: lst = list(s) stack = [] n = len(s) for i inrange(n): if s[i] == '(': stack.append(i) elif s[i] == ')': start = stack.pop() lst[start:i] = lst[start:i][::-1] ans = '' for i in lst: ans += i if i notin [')','('] else'' return ans
classSolution: defreverseParentheses(self, s: str) -> str: n = len(s) idx_lst = [0for i inrange(n)] stack = [] for i inrange(n): if s[i] == '(': stack.append(i) elif s[i] == ')': target = stack.pop() idx_lst[target] = i idx_lst[i] = target direction = 1 curr_idx = 0 ans = '' while curr_idx < n: if s[curr_idx] == '('or s[curr_idx] == ')': direction = -direction curr_idx = idx_lst[curr_idx] else: ans += s[curr_idx] curr_idx += direction return ans
1823. Find the Winner of
the Circular Game
There are n friends that are playing a game. The friends are sitting
in a circle and are numbered from 1 to n in clockwise order. More
formally, moving clockwise from the ith friend brings you to the (i+1)th
friend for 1 <= i < n, and moving clockwise from the nth friend
brings you to the 1st friend.
The rules of the game are as follows:
Start at the 1st friend.
Count the next k friends in the clockwise direction including the
friend you started at. The counting wraps around the circle and may
count some friends more than once.
The last friend you counted leaves the circle and loses the
game.
If there is still more than one friend in the circle, go back to
step 2 starting from the friend immediately clockwise of the friend who
just lost and repeat.
Else, the last friend in the circle wins the game.
Given the number of friends, n, and an integer k, return the winner
of the game.
这题还有个说法是处刑犯人, 每隔k个人处死一个, 最后一个豁免,
问谁能活下去.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Example 1: Input: n = 5, k = 2 Output: 3 Explanation: Here are the steps of the game: (1) Start at friend 1. (2) Count 2 friends clockwise, which are friends 1 and 2. (3) Friend 2 leaves the circle. Next start is friend 3. (4) Count 2 friends clockwise, which are friends 3 and 4. (5) Friend 4 leaves the circle. Next start is friend 5. (6) Count 2 friends clockwise, which are friends 5 and 1. (7) Friend 1 leaves the circle. Next start is friend 3. (8) Count 2 friends clockwise, which are friends 3 and 5. (9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
Example 2: Input: n = 6, k = 5 Output: 1 Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
classSolution: deffindTheWinner(self, n: int, k: int) -> int: winner = 0 for i inrange(2, n + 1): winner = (winner + k) % i return winner + 1
552. Student Attendance Record
II
An attendance record for a student can be represented as a string
where each character signifies whether the student was absent, late, or
present on that day. The record only contains the following three
characters:
'A': Absent.
'L': Late.
'P': Present.
Any student is eligible for an attendance award if they meet both of
the following criteria:
The student was absent ('A') for strictly fewer than 2 days
total.
The student was never late ('L') for 3 or more consecutive
days.
Given an integer n, return the number of possible attendance records
of length n that make a student eligible for an attendance award. The
answer may be very large, so return it modulo 10^9 + 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Example 1: Input: n = 2 Output: 8 Explanation: There are 8 records with length 2 that are eligible for an award: "PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL" Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).