Skip to content

Commit cbeff55

Browse files
committed
做了道题
1 parent a2218b9 commit cbeff55

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

main.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,92 @@ def dfs(i: int, mask: int, changed: bool) -> int:
10981098

10991099
return dfs(0, 0, False)
11001100

1101+
def finalValueAfterOperations(self, operations: List[str]) -> int:
1102+
num = 0
1103+
for operation in operations:
1104+
if operation[0] == "-" or operation[2] == "-":
1105+
num -= 1
1106+
else:
1107+
num += 1
1108+
return num
1109+
1110+
def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:
1111+
nums.sort()
1112+
1113+
1114+
def findMaximumScore(self, nums: List[int]) -> int:
1115+
stack = []
1116+
ans = 0
1117+
for idx, num in enumerate(nums):
1118+
if not stack:
1119+
stack.append([num, idx])
1120+
else:
1121+
if num > stack[0][0]:
1122+
ans += (idx - stack[-1][1]) * stack[-1][0]
1123+
stack.pop()
1124+
stack.append([num, idx])
1125+
else:
1126+
pass
1127+
if stack:
1128+
ans += stack[-1][0] * (len(nums) - 1 - stack[-1][1])
1129+
return ans
1130+
1131+
def removeSubstring(self, s: str, k: int) -> str:
1132+
stack = []
1133+
for c in s:
1134+
if not stack:
1135+
stack.append([c, 1])
1136+
else:
1137+
if c == stack[-1][0]:
1138+
stack.append([c, stack[-1][1] + 1])
1139+
else:
1140+
stack.append([c, 1])
1141+
if c == ")" and stack[-1][1] == k and len(stack) >= 2*k and stack[-k-1][0] == "(" and stack[-k-1][1] >= k:
1142+
for _ in range(2*k):
1143+
stack.pop()
1144+
return "".join([i[0] for i in stack])
1145+
1146+
def distinctPoints(self, s: str, k: int) -> int:
1147+
record = [0, 0]
1148+
mp = defaultdict(str)
1149+
def compute(c):
1150+
if c == "U":
1151+
return [0, 1]
1152+
elif c == "D":
1153+
return [0, -1]
1154+
elif c == "L":
1155+
return [1, 0]
1156+
else:
1157+
return [-1, 0]
1158+
1159+
for i in range(k):
1160+
temp = compute(s[i])
1161+
record[0] += temp[0]
1162+
record[1] += temp[1]
1163+
mp[f"{record[0]}-{record[1]}"] = 1
1164+
for i in range(k, len(s), 1):
1165+
temp = compute(s[i-k])
1166+
record[0] -= temp[0]
1167+
record[1] -= temp[1]
1168+
temp = compute(s[i])
1169+
record[0] += temp[0]
1170+
record[1] += temp[1]
1171+
mp[f"{record[0]}-{record[1]}"] = 1
1172+
1173+
return len(mp)
1174+
1175+
def climbStairs(self, n: int, costs: List[int]) -> int:
1176+
dp = [0 for _ in range(n)]
1177+
dp[0] = costs[0] + 1
1178+
if n > 1:
1179+
dp[1] = min(dp[0] + 1, 4) + costs[1]
1180+
if n > 2:
1181+
dp[2] = min(9, dp[0]+4, dp[1]+1) + costs[2]
1182+
1183+
for i in range(3, n, 1):
1184+
dp[i] = min([dp[i-1] + 1, dp[i-2] + 4, dp[i-3] + 9]) + costs[i]
1185+
1186+
return dp[-1]
11011187

11021188
s = Solution()
1103-
print(s.maxPartitionsAfterOperations("aabcacc", 2))
1189+
print(s.removeSubstring(s = "((()))()()()", k = 1))

test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from

0 commit comments

Comments
 (0)