Day 2 数组 双指针 滑窗:977.有序数组平方, 209. 长度最小子数组, 59.螺旋矩阵II

977. 有序数组的平方

  • 思路
    • example: [-2,-1,0,2,3] --> [0,1,4,4,9]
    • Two Pointer, -> <-
      • left, right = 0, n-1
      • while left <= right, stop when left > right
      • assign to res[idx] (逆序) based on the status at left and right, then move left/right, and update idx (idx -= 1)
  • 复杂度. 时间:O(n), 空间: O(1) if not including output
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        left, right = 0, n-1 
        index = n-1
        res = [0 for _ in range(n)]
        while left <= right:
            if abs(nums[left]) > abs(nums[right]):
                res[index] = nums[left] ** 2
                left += 1
            else:
                res[index] = nums[right] ** 2
                right -= 1
            index -= 1
        return res  
  • It's also possible to consider TP with <--*-->, where * is the "center".
  • 思考:可以in-place?
# 错误
# e.g., [-5,-3,-2,-1]
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        right = n-1 
        left = 0 # fixed
        while right >= 0:
            if abs(nums[left]) > abs(nums[right]):
                nums[left], nums[right] = nums[right], nums[left] 
            nums[right] = nums[right] ** 2
            right -= 1
        return nums  

LC 209. 长度最小的子数组

  • 思路
    • example
    • 正整数,长度最小的 连续子数组
    • TP->sliding window, -->--> 滑窗
      • feasible window: [left, right] 左闭右闭 (右扩左缩)
      • left, right = 0, 0 (起点相同)
      • outside loop: right
        • 先处理right节点,当前窗口:[left, right], 检查状态: feasible or not (feasible: sum >= target)
        • inside loop: left
          • while window sum >= target (feasible): move left until we get an invalid window
          • if not feasible, left is not moving
        • 当前窗口不可行,右移right扩展窗口
  • 复杂度. 时间:O(n), 空间: O(1)
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        n = len(nums)
        left, right = 0, 0 # 相同起点
        cur_sum = 0 # current windown sum
        res = float('inf')
        while right < n:
            cur_sum += nums[right] # if left == right, then curren windown only consists of one number 
            while cur_sum >= target and left <= right: # make sure left <= right 
            # note that target >= 1, so left is always <= right
                res = min(res, right - left + 1) # update result 
                cur_sum -= nums[left] # remove nums[left]
                left += 1 # move left
            right += 1 # move right 
        return res if res != float('inf') else 0 # 有可能 res = float('inf')

59. 螺旋矩阵 II

  • 思路
    • example
    • Simulation: Given , output matrix (from to ), clockwise
      • use top and bottom for row bounds, use left and right for column bounds
      • 累加计数
      • while top < bottom and left < right
        • stop when top == bottom and left == right (one number left), or top > bottom and left > right (done)
          • 如果最后只有一个元素落下,单独处理
  • 复杂度. 时间:, 空间: if not counting the output
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        res = [[0] *n for _ in range(n)]
        top, bottom = 0, n-1 # row bounds
        left, right = 0, n-1 # column bounds
        cnt = 1
        while top < bottom and left < right:
            # four "rays"
            #-->
            for j in range(left, right):
                res[top][j] = cnt
                cnt += 1
            # downward (on the "right" column)
            for i in range(top, bottom):
                res[i][right] = cnt
                cnt += 1
            # <-- 
            for j in range(right, left, -1):
                res[bottom][j] = cnt 
                cnt += 1
            # upward (left column)
            for i in range(bottom, top, -1):
                res[i][left] = cnt 
                cnt += 1
            # update bounds
            top += 1
            bottom -= 1
            left += 1
            right -= 1
        if top == bottom: # one element left out
            res[top][left] = cnt  
        return res
  • 长方形?

你可能感兴趣的:(Day 2 数组 双指针 滑窗:977.有序数组平方, 209. 长度最小子数组, 59.螺旋矩阵II)