1. 程式人生 > >DP專題8 - leetcode354. Russian Doll Envelopes/338. Counting Bits

DP專題8 - leetcode354. Russian Doll Envelopes/338. Counting Bits

354. Russian Doll Envelopes

題目描述

有許多給定長寬(w, h)的信封。當且僅當當前信封的長寬都大於另一個的時,才可以把另一個信封放入當前信封中。(不允許翻轉信封)
問:你可以套的最大信封數?

例子

Input: [[5,4],[6,4],[6,7],[2,3]]
Output: 3

Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

思想
排序後,求最長遞增子序列。
(法1 - 常規DP)
時間複雜度O(n^2)。
dp[i]表示以envelopes[i]結尾的最長遞增子序列。
(法2 - 改進)
dp[i]表示長為i+1的最小元素(長度相同情況下有一個最小的h

)。若大於dp[-1],直接append;否則,找到第一個大於該元素的,替換。

(w,h)首先按w升序,我們把w當成索引(肯定有序)。
然後按h降序,設計演算法使得h升序。(若h剛開始升序排列,則類似[5,3],[6,4],[6,7],[6,4]直接被丟棄了。)

解法1 - TLE
常規DP,時間複雜度O(n^2)。
dp[i]表示以envelopes[i]結尾的最長遞增子序列。

class Solution(object):
    def maxEnvelopes(self, envelopes):
        """
        :type envelopes: List[List[int]]
        :rtype: int
        """
if not envelopes: return 0 n = len(envelopes) envelopes.sort(key = lambda x:(x[0], x[1])) dp = [1] * n for i in range(1, n): for j in range(i): if envelopes[i][0] > envelopes[j][0] and envelopes[i][1] > envelopes[
j][1]: dp[i] = max(dp[i], dp[j] + 1) return max(dp)

解法2
改進,二維的LIS,時間複雜度O(nlogn)。

class Solution(object):
    def maxEnvelopes(self, envelopes):
        """
        :type envelopes: List[List[int]]
        :rtype: int
        """
        if not envelopes:
            return 0
        
        n = len(envelopes)
        envelopes.sort(key = lambda x:(x[0], -x[1]))
        dp = [envelopes[0][1]]
        for _, h in envelopes[1:]:
            if h > dp[-1]:
                dp.append(h)
            else:
                pos = self.helper(dp, h)
                dp[pos] = h
        return len(dp)
        
    def helper(self, arr, target):
        l, r = 0, len(arr)-1
        while l <= r:
            mid = (l + r) >> 1
            if arr[mid] < target:
                l = mid + 1
            else:
                if mid > 0 and arr[mid-1] >= target:
                    r = mid - 1
                else:
                    return mid

338. Counting Bits

題目描述

給定一個非負整數num。對0 ≤ i ≤ num區間內的每個數字i,計算二進位制表示中的1的數目。

例子
Example 1:

Input: 2
Output: [0,1,1]

Example 2:

Input: 5
Output: [0,1,1,2,1,2]

思想
(法1 - 利用內建函式)
(法2 - DP)
如何統計1的數目?
假設n為100…,則n-1為00…1,n&(n-1)=0。說明n&(n-1)消去了末尾的1
(法3 - DP)
dp[i]表示 i的二進位制中1的數目。
首先記錄i的最後一位是否為1;然後i的二進位制表示右移一位,變成⌊i/2⌋ 的二進位制。

解法1
利用內建函式

class Solution(object):
    def countBits(self, num):
        return [bin(i).count('1') for i in range(num+1)]

解法2
(DP)
假設n為100…,則n-1為00…1,n&(n-1)=0。說明n&(n-1)消去了末尾的1

class Solution(object):
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        res = [0]
        for i in range(1, num+1):
            res.append(res[i & (i-1)] + 1)
        return res

解法3
dp[i]表示 i的二進位制中1的數目。dp[i] = dp[i/2] + (i&1)
首先記錄i的最後一位是否為1;然後i的二進位制表示右移一位,變成⌊i/2⌋ 的二進位制。

class Solution(object):
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        dp = [0]
        for i in range(1, num+1):
            dp.append(dp[i>>1] + (i&1))
        return dp