Skip to content

Commit e06575c

Browse files
authored
Merge pull request 149ps#5 from 149ps/2094-lc
2094 lc
2 parents 0b9309c + 3f39590 commit e06575c

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.
3+
4+
Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.
5+
6+
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
7+
8+
Example 1:
9+
10+
Input: nums = [3,6,1,2,5], k = 2
11+
Output: 2
12+
Explanation:
13+
We can partition nums into the two subsequences [3,1,2] and [6,5].
14+
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
15+
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
16+
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
17+
Example 2:
18+
19+
Input: nums = [1,2,3], k = 1
20+
Output: 2
21+
Explanation:
22+
We can partition nums into the two subsequences [1,2] and [3].
23+
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
24+
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
25+
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
26+
Example 3:
27+
28+
Input: nums = [2,2,4,5], k = 0
29+
Output: 3
30+
Explanation:
31+
We can partition nums into the three subsequences [2,2], [4], and [5].
32+
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
33+
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
34+
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
35+
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
36+
"""
37+
class Solution:
38+
def partitionArray(self, nums: List[int], k: int) -> int:
39+
nums.sort()
40+
min_val,result = -sys.maxsize-1,0
41+
for num in nums:
42+
if num > min_val + k:
43+
min_val = num
44+
result += 1
45+
return result

513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@
3434
# self.left = left
3535
# self.right = right
3636
class Solution:
37-
def findBottomLeftValue(self, root: TreeNode) -> int:
37+
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
38+
if not root: return None
3839
q = collections.deque()
3940
q.append(root)
40-
bottom_left = root.val
41+
result = None
4142
while q:
42-
length = len(q)
43-
for i in range(length):
43+
n = len(q)
44+
for x in range(n):
4445
node = q.popleft()
45-
if i == 0:
46-
bottom_left = node.val
46+
if x == 0:
47+
result = node.val
4748
if node.left:
4849
q.append(node.left)
4950
if node.right:
5051
q.append(node.right)
51-
return bottom_left
52+
return result

0 commit comments

Comments
 (0)