标签:珠宝 col 长度 数组 def matrix nlogn class 一个
一、0/1 背包
def knapSack(W, wt, val, n): K = [[0 for x in range(W+1)] for x in range(n+1)] # Build table K[][] in bottom up manner for i in range(n+1): for w in range(W+1): if i==0 or w==0: K[i][w] = 0 elif wt[i-1] <= w: K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]) else: K[i][w] = K[i-1][w] return K[n][W]
二、最长公共子串
Given two strings ‘X’ and ‘Y’, find the length of the longest common substring.
Input : X = "abcdxyz", y = "xyzabcd"
Output : 4
The longest common substring is "abcd" and is of length 4.
Input : X = "zxabcdezy", y = "yzabcdezx"
Output : 6
The longest common substring is "abcdez" and is of length 6.
def LCS(X, Y, m, n): matrix = [[0 for k in range(n+1)] for l in range(m+1)] result = 0 for i in range(m + 1): for j in range(n + 1): if (i == 0 or j == 0): matrix[i][j] = 0 elif (X[i-1] == Y[j-1]): matrix[i][j] = matrix[i-1][j-1] + 1 result = max(result, matrix[i][j]) else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1]) return result
def lengthOfLIS1(nums): sortNums = sorted(nums) n = len(nums) return LCS(nums, sortNums, n, n)
方法二:动态规划 O(N**2)
def lengthOfLIS2(nums): if not nums: return 0 dp = [1]*len(nums) for i in range (1, len(nums)): for j in range(i): if nums[i] >nums[j]: dp[i] = max(dp[i], dp[j]+1) return max(dp)
方法三:二分 O(nlogn)
#using binary search def lengthOfLIS(nums): def search(temp, left, right, target): if left == right: return left mid = left+(right-left)//2 return search(temp, mid+1, right, target) if temp[mid]<target else search(temp, left, mid, target) temp = [] for num in nums: pos = search(temp, 0, len(temp), num) if pos >=len(temp): temp.append(num) else: temp[pos]=num return len(temp)
另:
#using binary search def lengthOfLIS(nums): temp = [] for num in nums: pos = bisect(temp, num) if pos >=len(temp): temp.append(num) else: temp[pos]=num return len(temp)
注意:temp数组中存储的不是递增子序列,但长度和所要的答案数组长度相同。
标签:珠宝 col 长度 数组 def matrix nlogn class 一个
原文地址:https://www.cnblogs.com/oldby/p/12842190.html