Skip to content

Commit 6b7682b

Browse files
committed
Apr 06
1 parent 3c1daa4 commit 6b7682b

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def largestDivisibleSubset(self, nums: list[int]) -> list[int]:
3+
n = len(nums)
4+
count, previous = [0] * n, [0] * n
5+
nums.sort()
6+
7+
max_count, index = 0, -1
8+
for i in range(n):
9+
count[i], previous[i] = 1, -1
10+
for j in range(i-1, -1, -1):
11+
if nums[i] % nums[j] == 0 and count[j] + 1 > count[i]:
12+
count[i] = count[j] + 1
13+
previous[i] = j
14+
if count[i] > max_count:
15+
max_count = count[i]
16+
index = i
17+
18+
subset = []
19+
while index != -1:
20+
subset.append(nums[index])
21+
index = previous[index]
22+
return subset
23+
24+
25+
def main():
26+
nums = [1, 2, 3]
27+
assert Solution().largestDivisibleSubset(nums) == [2, 1]
28+
29+
nums = [1, 2, 4, 8]
30+
assert Solution().largestDivisibleSubset(nums) == [8, 4, 2, 1]
31+
32+
33+
if __name__ == '__main__':
34+
main()

2025-04-April-LeetCoding-Challenge/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
| April 02 | [2873. Maximum Value of an Ordered Triplet I](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/) | Easy | Solved |
99
| April 03 | [2874. Maximum Value of an Ordered Triplet II](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/) | Medium | Solved |
1010
| April 04 | [1123. Lowest Common Ancestor of Deepest Leaves](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) | Medium | Solved |
11-
| April 05 | []() | | |
12-
| April 06 | []() | | |
11+
| April 05 | [1863. Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | Easy | Solved |
12+
| April 06 | [368. Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | Medium | Unsolved |
1313
| April 07 | []() | | |
1414
| April 08 | []() | | |
1515
| April 09 | []() | | |
@@ -39,6 +39,6 @@
3939
## Summary
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
42-
| Easy | 1 | 1 | 0 |
43-
| Medium | 3 | 3 | 0 |
42+
| Easy | 2 | 2 | 0 |
43+
| Medium | 4 | 3 | 1 |
4444
| Hard | 0 | 0 | 0 |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def subsetXORSum(self, nums: list[int]) -> int:
3+
or_sum = 0
4+
for num in nums:
5+
or_sum |= num
6+
return or_sum * (1 << (len(nums) - 1))
7+
8+
9+
def main():
10+
nums = [1, 3]
11+
assert Solution().subsetXORSum(nums) == 6
12+
13+
nums = [5, 1, 6]
14+
assert Solution().subsetXORSum(nums) == 28
15+
16+
nums = [3, 4, 5, 6, 7, 8]
17+
assert Solution().subsetXORSum(nums) == 480
18+
19+
20+
if __name__ == '__main__':
21+
main()

0 commit comments

Comments
 (0)