8.8 - medium总结31

终于到最后一篇了!到今天位置309道medium的题,全部总结完毕,还差一篇总结的总结,明天写。8.10开始hard,希望也是一天十题吧,这样可以在八月二十几号完成。

646. Maximum Length of Pair Chain: 类似LIS的dp,一维dp还算比较简单
647. Palindromic Substrings: 对于每一个值,从中间朝两边扩张就好了
648. Replace Words: 用一个Trie,比较容易解决
649. Dota2 Senate: 玩游戏的问题都先试一试记忆化搜索,这题好像不行,在竞赛的时候基本的想法是知道的,就是总是ban掉最自己之后的第一个对方的选手,不过用recursion的时候TLE了。这种循环比较的问题记得用deque,当时用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)表示对于当前的操作,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),也就是说当形成dp[b]之后,就一直按ctr-v,直到i。特殊情况是前六个值,剔除出去就好了,其实这个和爬楼梯很类似。
652. Find Duplicate Subtrees: 这道题竞赛时候也没做出来, 其实就是在divide and conquer的过程中,对每一个node更新一下hash,而这个hash要包含subtree的信息
654. Maximum Binary Tree: 典型的divide and conquer的题目,竞赛做出来的,这里就不再去做了。
655. Print Binary Tree: 这题也是竞赛做出来的,先把空间弄出来,然后再用preorder的方式去填充。

你可能感兴趣的:(8.8 - medium总结31)