Add nege weight num and dp examples

master
mbinary 2019-06-02 23:16:01 +08:00
parent 247df5fb71
commit 108294c097
6 changed files with 195 additions and 0 deletions

View File

@ -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]

View File

@ -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)

70
graph/dfs.py Normal file
View File

@ -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<n1 and j<n2:
if s1[i]!=s2[j]:
if flag:
return False
flag = True
j+=1
else:
i+=1
j+=1
return True
def dfs(begin):
ans = 1
w = words[begin]
n = len(w)
if n+1 in lenDic:
for nd in lenDic[n+1]:
#print(w,words[nd],isAdj(w,words[nd]))
if isAdj(w,words[nd]):
ans = max(ans,1+dfs(nd))
return ans
lenDic = {}
for i in range(len(words)):
n = len(words[i])
if n in lenDic:
lenDic[n].add(i)
else:
lenDic[n]={i}
lens = sorted(lenDic)
n = len(lens)
ans = 0
for i in range(n):
if ans < n-i:
for nd in lenDic[lens[i]]:
ans = max(ans,dfs(nd))
return ans

View File

@ -0,0 +1,60 @@
#coding: utf-8
''' mbinary
#######################################################################
# File : addNegaBin.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2019-06-02 22:32
# Description:
给出基数为 -2 的两个数 arr1 arr2返回两数相加的结果
数字以 数组形式 给出数组由若干 0 1 组成按最高有效位到最低有效位的顺序排列例如arr = [1,1,0,1] 表示数字 (-2)^3 + (-2)^2 + (-2)^0 = -3数组形式 的数字也同样不含前导零 arr 为例这意味着要么 arr == [0]要么 arr[0] == 1
返回相同表示形式的 arr1 arr2 相加的结果两数的表示形式为不含前导零由若干 0 1 组成的数组
eg
输入arr1 = [1,1,1,1,1], arr2 = [1,0,1]
输出[1,0,0,0,0]
解释arr1 表示 11arr2 表示 5输出表示 16
#######################################################################
'''
from nega import nega
def addNegaBin(arr1: list, arr2: list) -> 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)}')

12
math/numWeight/nega.py Normal file
View File

@ -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]