class Solution: def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: dict_sort={} cnt=1 for num in arr2: dict_sort[num]=cnt cnt+=1 for num in arr1: if num not in dict_sort: dict_sort[num]=cnt+1 arr1.sort() arr1.sort(key=lambda x: dict_sort[x]) return arr1
Category Archives: leetcode
1128. Number of Equivalent Domino Pairs
class Solution: def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int: rep=0 tmp=[0 for i in dominoes] for i in range(len(dominoes)-1): if tmp[i]!=0: continue cnt=0 for j in range(i+1,len(dominoes)): if tmp[j]==0 and ((dominoes[i][0]==dominoes[j][0] and dominoes[i][1]==dominoes[j][1]) or (dominoes[i][0]==dominoes[j][1] and dominoes[i][1]==dominoes[j][0])): tmp[j]=1 cnt+=1 rep+=(cnt+1)*cnt//2 return rep
1137. N-th Tribonacci Number
class Solution: def tribonacci(self, n: int) -> int: if n==0: return 0 if n==1: return 1 if n==2: return 1 dp=[0,1,1] for i in range(3,n+1): dp.append(dp[-1]+dp[-2]+dp[-3]) return dp[-1]
1154. Day of the Year
class Solution: def dayOfYear(self, date: str) -> int: date_list=date.split(‘-‘) year,month,day=date_list[0],date_list[1],date_list[2] days_list=[31,28,31,30,31,30,31,31,30,31,30,31] rep=int(day) for k in range(int(month)-1): rep+=days_list[k] if ((int(year)%100!=0 and int(year)%4==0) or ((int(year)%100==0 and int(year)%400==0))) and int(month)>2: rep+=1 return rep
1155. Number of Dice Rolls With Target Sum
class Solution: def numRollsToTarget(self, d: int, f: int, target: int) -> int: dict_f={} for i in range(1,f+1): dict_f[i]=1 while d>1: dict_s={} for i in range(1,f+1): for key in dict_f: if i+key not in dict_s: dict_s[i+key]=dict_f[key] else: dict_s[i+key]+=dict_f[key] dict_f=dict_s d-=1 if target in dict_f: return dict_f[target]%(10**9 + 7) else: return 0
1156. Swap For Longest Repeated Character Substring
class Solution: def maxRepOpt1(self, text: str) -> int: rep=1 for k in range(1,len(text)-1): if text[k]!=text[k-1]: i=0 cnt=1 target=text[k-1] left=k-1-1 right=k+1 while left>=0 and text[left]==target: left-=1 cnt+=1 while right<len(text) and text[right]==target: right+=1 cnt+=1 if left>0: if target in text[:left] or target in text[right:]: rep=max(rep,cnt+1) else: rep=max(rep,cnt) else: if target in text[right:]: rep=max(rep,cnt+1) else: rep=max(rep,cnt) target=text[0] cnt=1ContinueContinue reading “1156. Swap For Longest Repeated Character Substring”
1160. Find Words That Can Be Formed by Characters
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: dict_chars=self.stringToDict(chars) rep=0 for word in words: dict_word=self.stringToDict(word) if self.charsContainsWord(dict_chars,dict_word): rep+=len(word) return rep def charsContainsWord(self,dict_chars,dict_word): for k in dict_word: if k not in dict_chars: return False elif dict_chars[k]<dict_word[k]: return False return True def stringToDict(self,word): dict_word={} for c in word: if c not in dict_word: dict_word[c]=1 else:ContinueContinue reading “1160. Find Words That Can Be Formed by Characters”
1170. Compare Strings by Frequency of the Smallest Character
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: queries_int=[] words_int=[] for query in queries: queries_int.append(self.wordToInt(query)) for word in words: words_int.append(self.wordToInt(word)) words_int.sort() rep=[] for num in queries_int: rep.append(self.getNumberLagerN(num,words_int[::-1])) return rep def getNumberLagerN(self,num,words_int): cnt=0 for n in words_int: if n>num: cnt+=1 else: break return cnt def wordToInt(self,word): strs=”” for s in “abcdefghijklmnopqrstuvwxyz”: if s inContinueContinue reading “1170. Compare Strings by Frequency of the Smallest Character”
1184. Distance Between Bus Stops
class Solution: def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int: if destination<start: destination,start=start,destination num1_0ToSta=self.getdistance(0,start,distance) num2_staToDes=self.getdistance(start,destination,distance) num3_desTo0=self.getdistance(destination,len(distance),distance) return min(num2_staToDes,num1_0ToSta+num3_desTo0) def getdistance(self,k1,k2,distance): rep=0 for k in range(k1,k2): rep+=distance[k] return rep
1189. Maximum Number of Balloons
”’ Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible. You can use each character in text at most once. Return the maximum number of instances that can be formed. Example 1: Input: text = “nlaebolko” Output: 1 Example 2: Input:ContinueContinue reading “1189. Maximum Number of Balloons”