Skip to content

Commit f7cdf24

Browse files
author
Partho Biswas
committed
368. Largest Divisible Subset
1 parent a8a324b commit f7cdf24

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
709709
|51| **[1027. Longest Arithmetic Sequence](https://tinyurl.com/y7w7mmfn)** | [Python](https://tinyurl.com/wu6rdaw/1027_Longest_Arithmetic_Sequence.py), [Swift](https://tinyurl.com/wuja3c4/1027_Longest_Arithmetic_Sequence.swift)| [Art 1](https://tinyurl.com/ycto2x5o), [Art 2](https://tinyurl.com/yab7byj5) | Medium | **I didn't know that map can be used in 1d bottom-up dp. learned new things** |
710710
|52| **[304. Range Sum Query 2D - Immutable](https://tinyurl.com/ybhblo57)** | [Python](https://tinyurl.com/wu6rdaw/304_Range_Sum_Query_2D_Immutable.py), [Swift](https://tinyurl.com/wuja3c4/304_Range_Sum_Query_2D_Immutable.swift)| [Art 1](https://tinyurl.com/ycto2x5o), [Art 2](https://tinyurl.com/yab7byj5) | Medium | **very good question, classical** |
711711
|53| **[1477. Find Two Non-overlapping Sub-arrays Each With Target Sum](https://tinyurl.com/ybte7ydl)** | [Python](https://tinyurl.com/wu6rdaw/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.py), [Swift](https://tinyurl.com/wuja3c4/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.swift)| [Art 1](https://tinyurl.com/y7rshs2j), [Art 2](https://tinyurl.com/y8x7cgxl) | Medium | **Very interesting and deceiving and tricky problem. An unique combination of sliding window and DP** |
712+
|54| **[368. Largest Divisible Subset](https://tinyurl.com/ya8sv5jr)** | [Python](https://tinyurl.com/wu6rdaw/368_Largest_Divisible_Subset.py), [Swift](https://tinyurl.com/wuja3c4/368_Largest_Divisible_Subset.swift)| Official Solution | Medium | **Interesting problem.** |
712713

713714

714715
</p>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# DP bottom-up tabulation
2+
class Solution(object):
3+
def largestDivisibleSubset(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: List[int]
7+
"""
8+
# The container that holds all intermediate solutions.
9+
# key: the largest element in a valid subset.
10+
subsets = {-1: set()}
11+
12+
for num in sorted(nums):
13+
currentSubSets = []
14+
for key in subsets:
15+
if num % key == 0:
16+
newSet = set(subsets[key])
17+
newSet.add(num)
18+
currentSubSets.append(newSet)
19+
if currentSubSets is not None:
20+
subsets[num] = max(currentSubSets, key=len)
21+
else:
22+
subsets[num] = {num}
23+
return list(max(subsets.values(), key=len))
24+
25+
26+
# # DP top-down recursive - To Be Finished
27+
# class Solution(object):
28+
# def largestDivisibleSubset(self, nums):
29+
# """
30+
# :type nums: List[int]
31+
# :rtype: List[int]
32+
# """
33+
# # The container that holds all intermediate solutions.
34+
# # key: the largest element in a valid subset.
35+
# if not nums:
36+
# return []
37+
# nums.sort()
38+
# memo = {}
39+
# self.largestDivisibleSubsetHelper(nums, memo)
40+
# return list(max(memo.values(), key=len))
41+
42+
# def largestDivisibleSubsetHelper(self, nums, memo):
43+
# """
44+
# :type nums: List[int]
45+
# :rtype: List[int]
46+
# """
47+
# pass
48+
49+
"""
50+
1,2,3
51+
i
52+
j
53+
54+
1%2=0 or 2%1=0 > 1,2
55+
1%3=0 or 3%1=0 > 1,2
56+
57+
58+
1,2,4,8
59+
60+
12 14 18 24 28 48 >>1248
61+
62+
63+
1248
64+
65+
1 2 4 8
66+
1 1
67+
2 2
68+
4 4
69+
8 8
70+
71+
"""

0 commit comments

Comments
 (0)