-
Notifications
You must be signed in to change notification settings - Fork 0
LeetCode 416. Partition Equal Subset Sum #51
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9562b4a
LeetCode 416: Add TLE solution with brute-force
huyfififi 2a1d33d
LeetCode 416: Add step 1 solution backward iterating
huyfififi e487d13
LeetCode 416: Add step 2 solution (clean for loop)
huyfififi 718ed91
LeetCode 416: Escape code
huyfififi 25c1f4d
LeetCode 416: Add step 3 solution
huyfififi 86aa67c
LeetCode 416: Add step 4 solution - pass by value and prune operations
huyfififi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Step 1 | ||
|
|
||
| 合計値が `sum(nums) // 2` と一致する組み合わせを見つければいい、と考えた。 | ||
| 素直にやるなら、各 index においてその要素を使うか使わないかで分岐していって、subsetの合計値をチェックしていけばいいのだが... | ||
|
|
||
| > `1 <= nums.length <= 200` | ||
|
|
||
| という制約から、各インデックスで2つに分岐すると考えると 2^200 通り試すことになってしまう。これはかなり多めに見積もった値であり、枝刈りができるとはいえ、TLEするだろうなと予想はつく。一応実装してみたが、案の定TLEした。-> `step1_tle.py` | ||
|
|
||
| 何も思いつかないので、問題のTopicsを覗いてみたらDPを使うっぽい。 | ||
| TLEした方法だと、再帰関数が二つの引数 `(subset_sum, checking_index)` を必要としていて、これをメモ化したところで... と思ったのだが、後々に同じものが出てくる可能性があるか。 | ||
|
|
||
| 試しに `@functools.cache` をヘルパー関数につけてみたら、39番目のテストケースでTLE していたものが 100 番目のテストケースでMLE するようになった。高速化はできたが、記録しておかなければならないペアがとても多くなってしまうので、筋の良い方法ではなさそう。 | ||
|
|
||
| 少しどうにか動的計画法を使えないか考えてみたが、思いつかなかったので、LeetCodeのDiscussionを眺めて、実装した。-> `step1.py` | ||
| 逆順で辿ることで、同じ要素を2回使用してしまうことを防いでいるみたいだ。なるほど。 | ||
|
|
||
| まぁ、一旦こういう手法もあるんだくらいに留めて練習してみようかな。 | ||
|
|
||
| # Step 2 | ||
|
|
||
| [Wikipedia - Subset Sum Problem](https://en.wikipedia.org/wiki/Subset_sum_problem) | ||
|
|
||
| > The subset sum problem (SSP) is a decision problem in computer science. In its most general formulation, there is a multiset *S* of integers and a target-sum *T*, and the question is to decide whether any subset of the integers sum to precisely *T*. | ||
|
|
||
| > SSP is a special case of the knapsack problem and of the multiple subset sum problem. | ||
|
|
||
| この問題は、各itemが最大でも一回しか使えない 0/1 Knapsack Problem に分類されるらしい。 | ||
|
|
||
| # Step 4 | ||
|
|
||
| [https://github.com/hemispherium/LeetCode\_Arai60/pull/10#discussion\_r2618523247](https://github.com/hemispherium/LeetCode_Arai60/pull/10#discussion_r2618523247) | ||
|
|
||
| いつでも参照渡しが効率的なわけではない :eyes: |
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,22 @@ | ||
| class Solution: | ||
| def canPartition(self, nums: list[int]) -> bool: | ||
| sum_ = sum(nums) | ||
| if sum_ % 2 == 1: | ||
| return False | ||
|
|
||
| half_sum = sum_ // 2 | ||
|
|
||
| possible = [False] * (half_sum + 1) | ||
| possible[0] = True | ||
|
|
||
| for num in nums: | ||
| for i in range(half_sum - 1, -1, -1): | ||
| if not possible[i]: | ||
| continue | ||
|
|
||
| if i + num > half_sum: | ||
| continue | ||
|
|
||
| possible[i + num] = True | ||
|
|
||
| return possible[half_sum] | ||
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,28 @@ | ||
| class Solution: | ||
| def canPartition(self, nums: list[int]) -> bool: | ||
| sum_ = sum(nums) | ||
| if sum_ % 2 == 1: | ||
| return False | ||
|
|
||
| half_sum = sum_ // 2 | ||
|
|
||
| found = False | ||
|
|
||
| def find_target_subset(subset_sum: int, checking: int) -> None: | ||
| nonlocal found | ||
|
|
||
| if checking == len(nums): | ||
| return | ||
|
|
||
| if subset_sum == half_sum: | ||
| found |= True | ||
| return | ||
|
|
||
| if subset_sum > half_sum: | ||
| return | ||
|
|
||
| find_target_subset(subset_sum, checking + 1) | ||
| find_target_subset(subset_sum + nums[checking], checking + 1) | ||
|
|
||
| find_target_subset(0, 0) | ||
| return found |
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,27 @@ | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool canPartition(const std::vector<int>& nums) { | ||
| int total_sum = 0; | ||
| for (const auto& num : nums) { | ||
| total_sum += num; | ||
| } | ||
|
|
||
| if (total_sum % 2 == 1) { return false; } | ||
|
|
||
| int half_sum = total_sum / 2; | ||
|
|
||
| std::vector<uint8_t> possible(half_sum + 1); | ||
| possible[0] = 1; | ||
|
|
||
| for (const auto& num : nums) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
| for (int sum = half_sum; sum >= num; --sum) { | ||
| possible[sum] |= possible[sum - num]; | ||
| } | ||
| } | ||
|
|
||
| return possible[half_sum]; | ||
| } | ||
| }; | ||
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,24 @@ | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool canPartition(const std::vector<int>& nums) { | ||
| int total_sum = 0; | ||
| for (const auto& num : nums) { | ||
| total_sum += num; | ||
| } | ||
| if (total_sum % 2 == 1) { return false; } | ||
|
|
||
| int half_sum = total_sum / 2; | ||
| std::vector<uint8_t> possible(half_sum + 1); | ||
| possible[0] = 1; | ||
| for (const auto& num : nums) { | ||
| for (int sum = half_sum; sum >= num; --sum) { | ||
| possible[sum] |= possible[sum - num]; | ||
| } | ||
| } | ||
|
|
||
| return possible[half_sum]; | ||
| } | ||
| }; |
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,27 @@ | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool canPartition(const std::vector<int>& nums) { | ||
| int total_sum = 0; | ||
| for (int num : nums) { | ||
| total_sum += num; | ||
| } | ||
| if (total_sum % 2 == 1) { return false; } | ||
|
|
||
| int half_sum = total_sum / 2; | ||
| std::vector<uint8_t> possible(half_sum + 1); | ||
| possible[0] = true; | ||
| for (int num : nums) { | ||
| for (int sum = half_sum; sum >= num; --sum) { | ||
| possible[sum] |= possible[sum - num]; | ||
| if (sum == half_sum && possible[half_sum]) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i + num == half_sum の時に return True して、最後は return False しても良いと思いました。