class Solution: def longestSubarray(self, A: List[int], limit: int) -> int: # main idea: convert diff of any two elements within array <= limit ==> diff between min and max elments of subarray # time O(N) space O(N) maxd = collections.deque() # monotonically decreasing order by index mind = collections.deque() #...
[Read More]
1423. Maximum Points You Can Obtain from Cards
```python class Solution: def maxScore(self, A: List[int], k: int) -> int: # time O(N) space O(K) n = len(A) total = sum(A) if n <= k : return total window = n - k min_ = total l = 0 cur_sum = 0 for r in range(n): cur_sum += A[r]...
[Read More]
1153. String Transforms Into Another String
time O(N) space O(26)
[Read More]
31. Next Permutation
```python class Solution: def nextPermutation(self, nums: List[int]) -> None: “”” Do not return anything, modify nums in-place instead. “”” # main idea is to (1) find a larger number than current nums (2) increment between next bigger and current number is as small as possible ==> start from lower digit/...
[Read More]
112. Path Sum
```python Definition for a binary tree node. class TreeNode: def init(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right time O(N) space O(H) H: stack class Solution: def hasPathSum(self, root: TreeNode, sum: int) -> bool: if not root: return False if not root.left and not root.right...
[Read More]