mirror of
https://github.com/heqin-zhu/algorithm.git
synced 2024-03-22 13:30:46 +08:00
Add nege weight num and dp examples
This commit is contained in:
parent
247df5fb71
commit
108294c097
32
dynamicProgramming/last-stone-weight.py
Normal file
32
dynamicProgramming/last-stone-weight.py
Normal 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]
|
21
dynamicProgramming/max-len-of-repeated-subarray.py
Normal file
21
dynamicProgramming/max-len-of-repeated-subarray.py
Normal 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
70
graph/dfs.py
Normal 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
|
||||
|
60
math/numWeight/addNegaBin.py
Normal file
60
math/numWeight/addNegaBin.py
Normal 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 表示 11,arr2 表示 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
12
math/numWeight/nega.py
Normal 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]
|
Loading…
Reference in New Issue
Block a user