diff --git a/2294. Partition Array Such That Maximum Difference Is K/2294. Partition Array Such That Maximum Difference Is K.py b/2294. Partition Array Such That Maximum Difference Is K/2294. Partition Array Such That Maximum Difference Is K.py new file mode 100644 index 0000000..06c852c --- /dev/null +++ b/2294. Partition Array Such That Maximum Difference Is K/2294. Partition Array Such That Maximum Difference Is K.py @@ -0,0 +1,45 @@ +""" +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. + +Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k. + +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. + +Example 1: + +Input: nums = [3,6,1,2,5], k = 2 +Output: 2 +Explanation: +We can partition nums into the two subsequences [3,1,2] and [6,5]. +The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2. +The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1. +Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed. +Example 2: + +Input: nums = [1,2,3], k = 1 +Output: 2 +Explanation: +We can partition nums into the two subsequences [1,2] and [3]. +The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1. +The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0. +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]. +Example 3: + +Input: nums = [2,2,4,5], k = 0 +Output: 3 +Explanation: +We can partition nums into the three subsequences [2,2], [4], and [5]. +The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0. +The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0. +The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0. +Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed. +""" +class Solution: + def partitionArray(self, nums: List[int], k: int) -> int: + nums.sort() + min_val,result = -sys.maxsize-1,0 + for num in nums: + if num > min_val + k: + min_val = num + result += 1 + return result \ No newline at end of file diff --git a/513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py b/513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py index 00569f6..953395e 100644 --- a/513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py +++ b/513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py @@ -34,18 +34,19 @@ # self.left = left # self.right = right class Solution: - def findBottomLeftValue(self, root: TreeNode) -> int: + def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: + if not root: return None q = collections.deque() q.append(root) - bottom_left = root.val + result = None while q: - length = len(q) - for i in range(length): + n = len(q) + for x in range(n): node = q.popleft() - if i == 0: - bottom_left = node.val + if x == 0: + result = node.val if node.left: q.append(node.left) if node.right: q.append(node.right) - return bottom_left \ No newline at end of file + return result \ No newline at end of file