終于到最后一篇了宁仔!到今天位置309道m(xù)edium的題,全部總結(jié)完畢峦睡,還差一篇總結(jié)的總結(jié)翎苫,明天寫。8.10開始hard榨了,希望也是一天十題吧煎谍,這樣可以在八月二十幾號完成。
646. Maximum Length of Pair Chain: 類似LIS的dp阻逮,一維dp還算比較簡單
647. Palindromic Substrings: 對于每一個值粱快,從中間朝兩邊擴(kuò)張就好了
648. Replace Words: 用一個Trie,比較容易解決
649. Dota2 Senate: 玩游戲的問題都先試一試記憶化搜索叔扼,這題好像不行事哭,在競賽的時候基本的想法是知道的,就是總是ban掉最自己之后的第一個對方的選手瓜富,不過用recursion的時候TLE了鳍咱。這種循環(huán)比較的問題記得用deque,當(dāng)時用index來表示与柑,簡直要死要活谤辜。We have people = [int, int] representing how many people are in the queue, and bans = [int, int] representing how many "floating" bans there are. When we meet a person, if there is a floating ban waiting for them, then they are banned and we skip them. Eventually, the queue will just have one type of person, which is when we break.
class Solution(object):
def predictPartyVictory(self, senate):
"""
:type senate: str
:rtype: str
"""
A = collections.deque()
people = [0, 0]
bans = [0, 0]
for person in senate:
x = person == 'R'
people[x] += 1
A.append(x) # A is a list of 0 1
while all(people): #
x = A.popleft()
people[x] -= 1
if bans[x]:
bans[x] -= 1
else:
bans[x^1] += 1
A.append(x)
people[x] += 1
return "Radiant" if people[1] else "Dire"
650. 2 Keys Keyboard: dp問題,dp[i] = min(dp[i], dp[j] + (i/j-1)+1)表示對于當(dāng)前的操作价捧,i/j-1是說還要加上多少個j丑念,比如說 j = 2 i = 6 那么還需要 6/2-1 = 2兩次paste,然后加上一次copy
651. 4 Keys Keyboard: 想法很簡單结蟋,就是對于每一個值脯倚,它可能的形成方式是 dp[i] = dp[b]*(i-b-1),也就是說當(dāng)形成dp[b]之后,就一直按ctr-v推正,直到i恍涂。特殊情況是前六個值,剔除出去就好了植榕,其實(shí)這個和爬樓梯很類似再沧。
652. Find Duplicate Subtrees: 這道題競賽時候也沒做出來, 其實(shí)就是在divide and conquer的過程中,對每一個node更新一下hash尊残,而這個hash要包含subtree的信息
654. Maximum Binary Tree: 典型的divide and conquer的題目炒瘸,競賽做出來的,這里就不再去做了寝衫。
655. Print Binary Tree: 這題也是競賽做出來的什燕,先把空間弄出來,然后再用preorder的方式去填充竞端。