Skip to content

Commit 0869489

Browse files
committed
Overhaul and cleanup notebooks; hides solutions, splits and renames
several notebooks
1 parent fc715c0 commit 0869489

15 files changed

+723
-520
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Solution(object):
2+
def canPartitionKSubsets(self, nums, k):
3+
nums.sort(reverse=True)
4+
buck, kSum = [0] * k, sum(nums) // k
5+
6+
def dfs(idx):
7+
if idx == len(nums):
8+
return len(set(buck)) == 1 # every bucket sums to target
9+
for i in range(k):
10+
buck[i] += nums[idx]
11+
if buck[i] <= kSum and dfs(idx + 1):
12+
return True
13+
buck[i] -= nums[idx]
14+
if buck[i] == 0:
15+
break
16+
return False
17+
return dfs(0)
18+
19+
20+
# Annotated and slower-but-clearer version of @jingkuan's solution based on @chemikadze's
21+
# solution using @chengyuge925's solution.
22+
class Solution:
23+
def canPartitionKSubsets(self, nums, k):
24+
buckets = [0]*k
25+
target = sum(nums) // k
26+
27+
# We want to try placing larger numbers first
28+
nums.sort(reverse=True)
29+
30+
31+
# DFS determines which bucket to put the 'current element' (nums[idx] ) into
32+
def dfs(idx):
33+
# If we've placed all of the items, we're done;
34+
# check if we correctly made k equal subsets of
35+
# size sum(nums) // k
36+
if idx == len(nums):
37+
return set(buckets) == set([target])
38+
39+
# For each bucket
40+
for i in range(k):
41+
# Try adding the current element to it
42+
buckets[i] += nums[idx]
43+
44+
# If it's a valid placement and we correctly placed the next element, we're
45+
# done placing the current element.
46+
if buckets[i] <= target and dfs(idx + 1):
47+
return True
48+
49+
# Otherwise, remove the current element from the ith bucket and
50+
# try the next one.
51+
buckets[i] -= nums[idx]
52+
53+
# This is an optimization that is not strictly necessary.
54+
# If bucket[i] == 0, it means:
55+
# - We put nums[idx] into an empty bucket
56+
# - We tried placing every other element after and failed.
57+
# - We took nums[idx] out of the bucket, making it empty again.
58+
# So trying to put nums[idx] into a _different_ empty bucket will not produce
59+
# a correct solution; we will just waste time (we place elements left to right,
60+
# so if this bucket is now empty, every one after it is too).
61+
#
62+
# Otherwise (bucket[i] > 0), we just go to the next bucket and
63+
# try placing nums[idx] there. If none of them work out, we wind up
64+
# breaking out of the loop when range(k) ends and returning False.
65+
if buckets[i] == 0:
66+
break
67+
68+
# We couldn't place the current element anywhere that
69+
# leads to a valid solution, so we will need to backtrack
70+
# and try something else.
71+
return False
72+
73+
# Start by trying to place nums[0]
74+
return dfs(0)

notebooks/Backtracking (WIP).ipynb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# WIP\n",
8+
"Add explanation and more examples."
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Backtracking"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"## [698. Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)\n",
23+
"</br>\n",
24+
"<details>\n",
25+
"<summary><b>Click for answer.</b></summary>\n",
26+
" \n",
27+
"(Original solution [here](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/discuss/146579/Easy-python-28-ms-beats-99.5))\n",
28+
"\n",
29+
"Each subset must be equal to `sum(nums)/k`, and there is only a solution if `sum(nums) % k == 0`. To start, we sort the numbers into descending order. \n",
30+
"</details>"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 1,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"# No tests; passes LeetCode\n",
40+
"%run ../leetcode/698-partition-to-k-equal-sum-subsets/solution.py"
41+
]
42+
}
43+
],
44+
"metadata": {
45+
"kernelspec": {
46+
"display_name": "Python 3 (ipykernel)",
47+
"language": "python",
48+
"name": "python3"
49+
},
50+
"language_info": {
51+
"codemirror_mode": {
52+
"name": "ipython",
53+
"version": 3
54+
},
55+
"file_extension": ".py",
56+
"mimetype": "text/x-python",
57+
"name": "python",
58+
"nbconvert_exporter": "python",
59+
"pygments_lexer": "ipython3",
60+
"version": "3.8.10"
61+
}
62+
},
63+
"nbformat": 4,
64+
"nbformat_minor": 4
65+
}

0 commit comments

Comments
 (0)