mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Add doctests, type hints; fix bug for dynamic_programming/minimum_partition.py (#10012)
* Add doctests, type hints; fix bug * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
895dffb412
commit
fa077e6703
@ -3,13 +3,25 @@ Partition a set into two subsets such that the difference of subset sums is mini
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def find_min(arr):
|
def find_min(arr: list[int]) -> int:
|
||||||
|
"""
|
||||||
|
>>> find_min([1, 2, 3, 4, 5])
|
||||||
|
1
|
||||||
|
>>> find_min([5, 5, 5, 5, 5])
|
||||||
|
5
|
||||||
|
>>> find_min([5, 5, 5, 5])
|
||||||
|
0
|
||||||
|
>>> find_min([3])
|
||||||
|
3
|
||||||
|
>>> find_min([])
|
||||||
|
0
|
||||||
|
"""
|
||||||
n = len(arr)
|
n = len(arr)
|
||||||
s = sum(arr)
|
s = sum(arr)
|
||||||
|
|
||||||
dp = [[False for x in range(s + 1)] for y in range(n + 1)]
|
dp = [[False for x in range(s + 1)] for y in range(n + 1)]
|
||||||
|
|
||||||
for i in range(1, n + 1):
|
for i in range(n + 1):
|
||||||
dp[i][0] = True
|
dp[i][0] = True
|
||||||
|
|
||||||
for i in range(1, s + 1):
|
for i in range(1, s + 1):
|
||||||
@ -17,7 +29,7 @@ def find_min(arr):
|
|||||||
|
|
||||||
for i in range(1, n + 1):
|
for i in range(1, n + 1):
|
||||||
for j in range(1, s + 1):
|
for j in range(1, s + 1):
|
||||||
dp[i][j] = dp[i][j - 1]
|
dp[i][j] = dp[i - 1][j]
|
||||||
|
|
||||||
if arr[i - 1] <= j:
|
if arr[i - 1] <= j:
|
||||||
dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]]
|
dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]]
|
||||||
@ -28,3 +40,9 @@ def find_min(arr):
|
|||||||
break
|
break
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from doctest import testmod
|
||||||
|
|
||||||
|
testmod()
|
||||||
|
Loading…
Reference in New Issue
Block a user