class Solution: def reverseParentheses(self, s: str) -> str: s_stack=[] l,r=0,0 while r<len(s): if s[r]=='(‘: if r!=l: s_stack.append(s[l:r]) s_stack.append(s[r]) l=r+1 elif s[r]==’)’: cur=s[l:r] ss=” while ss!='(‘: ss=s_stack.pop() if ss!='(‘: cur=ss+cur else: cur=cur[::-1] s_stack.append(cur) l=r+1 r+=1 if s[-1]!=’)’: s_stack.append(s[l:r]) rep=”” for str1 in s_stack: rep+=str1 return rep ”’ def ss: if not s: return s l,r=0,len(s)-1 whileContinueContinue reading “1190. Reverse Substrings Between Each Pair of Parentheses”
Category Archives: leetcode
1191. K-Concatenation Maximum Sum
”’ Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return the maximum sub-array sum in the modified array. Note that the length ofContinueContinue reading “1191. K-Concatenation Maximum Sum”
1200. Minimum Absolute Difference
class Solution(object): def minimumAbsDifference(self, arr): “”” :type arr: List[int] :rtype: List[List[int]] “”” arr.sort() rep=[[arr[0],arr[1]]] gap=arr[1]-arr[0] for k in range(1,len(arr)-1): cur_gap=arr[k+1]-arr[k] if cur_gap<gap: rep=[[arr[k],arr[k+1]]] gap=cur_gap elif cur_gap==gap: rep.append([arr[k],arr[k+1]]) else: continue return rep
1207. Unique Number of Occurrences
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dict_arr={} for num in arr: if num not in dict_arr: dict_arr[num]=1 else: dict_arr[num]+=1 rep=set() cnt=0 for k in dict_arr: cnt+=1 rep.add(dict_arr[k]) return len(rep)==cnt
1208. Get Equal Substrings Within Budget
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: rep=[] for k in range(len(s)): gap=self.getGapOfStr(s[k],t[k]) rep.append(gap) res=0 print(rep) l=0 total=0 r=0 while l<len(rep): if r<len(rep) and total+rep[r]<=maxCost: while r<len(rep) and total+rep[r]<=maxCost: total+=rep[r] r+=1 res=max(res,r-l) total-=rep[l] l+=1 return res def getGapOfStr(self,str1,str2): strs=’abcdefghijklmnopqrstuvwxyz’ dict_s={} for k in range(len(strs)): dict_s[strs[k]]=k return abs(dict_s[str1]-dict_s[str2])
1209. Remove All Adjacent Duplicates in String II
class Solution: def removeDuplicates(self, s: str, k: int) -> str: rep=[] for ss in s: if not rep: rep.append([ss,1]) elif ss!=rep[-1][0]: rep.append([ss,1]) else: if rep[-1][1]==k-1: rep.pop() else: rep[-1][1]+=1 res=”” for cur in rep: for k in range(cur[1]): res+=cur[0] return res
1220. Count Vowels Permutation
class Solution: def countVowelPermutation(self, n: int) -> int: ”’ Each vowel ‘a’ may only be followed by an ‘e’. Each vowel ‘e’ may only be followed by an ‘a’ or an ‘i’. Each vowel ‘i’ may not be followed by another ‘i’. Each vowel ‘o’ may only be followed by an ‘i’ or a ‘u’.ContinueContinue reading “1220. Count Vowels Permutation”
1219. Path with Maximum Gold
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: rep=0 for row in range(len(grid)): for col in range(len(grid[0])): if grid[row][col]!=0: rep=max(rep,self.getrcMaximumGold(row,col,grid)) return rep def getrcMaximumGold(self,row,col,grid): if grid[row][col]==0: return 0 if grid[row][col]==’X’: return 0 rep=grid[row][col] grid[row][col]=’X’ cur=0 if row-1>=0: cur=max(self.getrcMaximumGold(row-1,col,grid),cur) if col-1>=0: cur=max(self.getrcMaximumGold(row,col-1,grid),cur) if row+1<len(grid): cur=max(self.getrcMaximumGold(row+1,col,grid),cur) if col+1<len(grid[0]): cur=max(self.getrcMaximumGold(row,col+1,grid),cur) grid[row][col]=rep return rep+cur
1218. Longest Arithmetic Subsequence of Given Difference
1218. Longest Arithmetic Subsequence of Given Difference Difficulty: 中等 Given an integer array arr and an integer <font face=”monospace” style=”display: inline;”>difference</font>, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference. Example 1: Input: arr = [1,2,3,4], difference = 1 Output: 4ContinueContinue reading “1218. Longest Arithmetic Subsequence of Given Difference”
1217. Play with Chips.py
class Solution: def minCostToMoveChips(self, chips: List[int]) -> int: cnt_0=0 cnt_1=0 for chip in chips: if chip%2==0: cnt_0+=1 else: cnt_1+=1 return min(cnt_0,cnt_1)