-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
add distribute coins #7975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChrisO345
merged 6 commits into
TheAlgorithms:master
from
alexpantyukhin:distribute_coins
Nov 10, 2022
Merged
add distribute coins #7975
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a6a8cd
add distribute coins
alexpantyukhin 4adf610
updating DIRECTORY.md
ab349bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7947599
fix review notes
alexpantyukhin f19b5e5
fix typehint
alexpantyukhin efd6ebe
fix type in TreeNode
alexpantyukhin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
""" | ||
Author : Alexander Pantyukhin | ||
Date : November 7, 2022 | ||
|
||
Task: | ||
You are given a tree root of a binary tree with n nodes, where each node has | ||
node.data coins. There are exactly n coins in whole tree. | ||
|
||
In one move, we may choose two adjacent nodes and move one coin from one node | ||
to another. A move may be from parent to child, or from child to parent. | ||
|
||
Return the minimum number of moves required to make every node have exactly one coin. | ||
|
||
Example 1: | ||
|
||
3 | ||
/ \ | ||
0 0 | ||
|
||
Result: 2 | ||
|
||
Example 2: | ||
|
||
0 | ||
/ \ | ||
3 0 | ||
|
||
Result 3 | ||
|
||
leetcode: https://leetcode.com/problems/distribute-coins-in-binary-tree/ | ||
|
||
Implementation notes: | ||
User depth-first search approach. | ||
|
||
Let n is the number of nodes in tree | ||
Runtime: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from collections import namedtuple | ||
|
||
|
||
@dataclass | ||
class TreeNode: | ||
data: float | ||
left: TreeNode | None = None | ||
right: TreeNode | None = None | ||
|
||
CoinsDistribResult = namedtuple("CoinsDistribResult", 'moves excess') | ||
|
||
def distributeCoins(root: TreeNode | None) -> int: | ||
""" | ||
>>> distributeCoins(TreeNode(3, TreeNode(0), TreeNode(0))) | ||
2 | ||
>>> distributeCoins(TreeNode(0, TreeNode(3), TreeNode(0))) | ||
3 | ||
>>> distributeCoins(TreeNode(0, TreeNode(0), TreeNode(3))) | ||
3 | ||
>>> distributeCoins(None) | ||
0 | ||
>>> distributeCoins(TreeNode(0, TreeNode(0), TreeNode(0))) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The nodes number should be same as the number of coins | ||
>>> distributeCoins(TreeNode(0, TreeNode(1), TreeNode(1))) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The nodes number should be same as the number of coins | ||
""" | ||
|
||
if root is None: | ||
return 0 | ||
|
||
# Validation | ||
def count_nodes(node): | ||
alexpantyukhin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> count_nodes(None): | ||
0 | ||
""" | ||
if node is None: | ||
return 0 | ||
|
||
return count_nodes(node.left) + count_nodes(node.right) + 1 | ||
|
||
def count_coins(node): | ||
alexpantyukhin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> count_coins(None): | ||
0 | ||
""" | ||
if node is None: | ||
return 0 | ||
|
||
return count_coins(node.left) + count_coins(node.right) + node.data | ||
|
||
if count_nodes(root) != count_coins(root): | ||
raise ValueError("The nodes number should be same as the number of coins") | ||
|
||
# Main calculation | ||
def get_distrib(node): | ||
alexpantyukhin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> get_distrib(None) | ||
namedtuple("CoinsDistribResult", "0 2") | ||
""" | ||
|
||
if node is None: | ||
return CoinsDistribResult(0, 1) | ||
|
||
left_distrib_moves, left_distrib_excess = get_distrib(node.left) | ||
right_distrib_moves, right_distrib_excess = get_distrib(node.right) | ||
|
||
coins_to_left = 1 - left_distrib_excess | ||
coins_to_right = 1 - right_distrib_excess | ||
|
||
result_moves = left_distrib_moves + right_distrib_moves + \ | ||
abs(coins_to_left) + abs(coins_to_right) | ||
result_excess = node.data - coins_to_left - coins_to_right | ||
|
||
return CoinsDistribResult(result_moves, result_excess) | ||
|
||
return get_distrib(root)[0] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.