diff --git a/dynamicProgramming/last-stone-weight.py b/dynamicProgramming/last-stone-weight.py new file mode 100644 index 0000000..a3798b9 --- /dev/null +++ b/dynamicProgramming/last-stone-weight.py @@ -0,0 +1,32 @@ +#coding: utf-8 +''' mbinary +####################################################################### +# File : last-stone-weight.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.xyz +# Github: https://github.com/mbinary +# Created Time: 2019-05-28 23:30 +# Description: +leetcode 1049: https://leetcode-cn.com/problems/last-stone-weight-ii/ + +有一堆石头,每块石头的重量都是正整数。 + +每一回合,从中选出任意两块石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下: + +如果 x == y,那么两块石头都会被完全粉碎; +如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。 +最后,最多只会剩下一块石头。返回此石头最小的可能重量。如果没有石头剩下,就返回 0。 + +####################################################################### +''' + +class Solution: + def lastStoneWeightII(self, stones: List[int]) -> int: + sm = sum(stones) + ans = sm//2 + dp = [0]*(ans+1) + for x in stones: + for j in range(ans,x-1,-1): + dp[j] = max(dp[j],dp[j-x]+x) + return sm-2*dp[ans] diff --git a/dynamicProgramming/max-len-of-repeated-subarray.py b/dynamicProgramming/max-len-of-repeated-subarray.py new file mode 100644 index 0000000..7ce0fcc --- /dev/null +++ b/dynamicProgramming/max-len-of-repeated-subarray.py @@ -0,0 +1,21 @@ +#coding: utf-8 +''' mbinary +####################################################################### +# File : max-len-of-repeated-subarray.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.xyz +# Github: https://github.com/mbinary +# Created Time: 2019-05-27 08:25 +# Description: + 给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。 +####################################################################### +''' +def findLength(A,B): + n,m = len(A),len(B) + dp = [[0]*(m+1) for i in range(n+1)] + for i in range(1,n+1): + for j in range(1,m+1): + if A[i-1]==B[j-1]: + dp[i][j]=dp[i-1][j-1]+1 + return max(max(row) for row in dp) diff --git a/graph/dfs.py b/graph/dfs.py new file mode 100644 index 0000000..6f45a84 --- /dev/null +++ b/graph/dfs.py @@ -0,0 +1,70 @@ +#coding: utf-8 +''' mbinary +####################################################################### +# File : dfs.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.xyz +# Github: https://github.com/mbinary +# Created Time: 2019-05-27 10:02 +# Description: + from leetcode-cn #1048: https://leetcode-cn.com/problems/longest-string-chain/ + 给出一个单词列表,其中每个单词都由小写英文字母组成。 + + 如果我们可以在 word1 的任何地方添加一个字母使其变成 word2,那么我们认为 word1 是 word2 的前身。例如,"abc" 是 "abac" 的前身。 + + 词链是单词 [word_1, word_2, ..., word_k] 组成的序列,k >= 1,其中 word_1 是 word_2 的前身,word_2 是 word_3 的前身,依此类推。 + + 从给定单词列表 words 中选择单词组成词链,返回词链的最长可能长度。 + +####################################################################### +''' + +class Solution: + def longestStrChain(self, words: List[str]) -> int: + def isAdj(s1,s2): + if len(s1)>len(s2): + s1,s2 = s2,s1 + n1,n2 = len(s1),len(s2) + if n2-n1!=1: + return False + i=j=0 + flag = False + while i list: + if len(arr1) < len(arr2): + arr1, arr2 = arr2, arr1 + for i in range(-1, -len(arr2) - 1, -1): + if arr1[i] == 1 and arr2[i] == 1: + arr1[i] = 0 + mux = 0 + for j in range(i - 1, -len(arr1) - 1, -1): + if arr1[j] == mux: + mux = 1 - mux + arr1[j] = mux + else: + arr1[j] = mux + break + else: + arr1 = [1, 1] + arr1 + + elif arr1[i] == 0 and arr2[i] == 1: + arr1[i] = arr2[i] + #print(arr1,arr2,i) + while len(arr1) > 1 and arr1[0] == 0: + arr1.pop(0) + return arr1 + +if __name__=='__main__': + while 1: + print("input q to quit or input x1 x2: ") + s = input() + if s=='q': + break + n1,n2 =[int(i) for i in s.split()] + l1,l2 = nega(n1),nega(n2) + print(n1,l1) + print(n2,l2) + print(f'{n1}+{n2}={n1+n2}: {addNegaBin(l1,l2)}') diff --git a/math/convertWeight.py b/math/numWeight/convertWeight.py similarity index 100% rename from math/convertWeight.py rename to math/numWeight/convertWeight.py diff --git a/math/numWeight/nega.py b/math/numWeight/nega.py new file mode 100644 index 0000000..9750510 --- /dev/null +++ b/math/numWeight/nega.py @@ -0,0 +1,12 @@ +def nega(n:int,base=-2:int)->:list: + '''return list of num, the first is the highest digit''' + if base>-2: + raise Exception(f"[Error]: invalid base: {base}, base should be no more than -2") + ans = [] + while n: + k = n%base + if k<0: + k-=base + ans.append(k) + n = (n-k)//base + return ans[::-1]