【leetcode】1562. Find Latest Group of Size M

题目如下:

Given an array arr that represents a permutation of numbers from 1 to n. You have a binary string of size n that initially has all its bits set to zero.

At each step i (assuming both the binary string and arr are 1-indexed) from 1 to n, the bit at position arr[i] is set to 1. You are given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s such that it cannot be extended in either direction.

Return the latest step at which there exists a group of ones of length exactly mIf no such group exists, return -1.

Example 1:

Input: arr = [3,5,1,2,4], m = 1
Output: 4
Explanation:
Step 1: "00100", groups: ["1"]
Step 2: "00101", groups: ["1", "1"]
Step 3: "10101", groups: ["1", "1", "1"]
Step 4: "11101", groups: ["111", "1"]
Step 5: "11111", groups: ["11111"]
The latest step at which there exists a group of size 1 is step 4.

Example 2:

Input: arr = [3,1,5,4,2], m = 2
Output: -1
Explanation:
Step 1: "00100", groups: ["1"]
Step 2: "10100", groups: ["1", "1"]
Step 3: "10101", groups: ["1", "1", "1"]
Step 4: "10111", groups: ["1", "111"]
Step 5: "11111", groups: ["11111"]
No group of size 2 exists during any step.

Example 3:

Input: arr = [1], m = 1
Output: 1

Example 4:

Input: arr = [2,1], m = 2
Output: 2

Constraints:

  • n == arr.length
  • 1 <= n <= 10^5
  • 1 <= arr[i] <= n
  • All integers in arr are distinct.
  • 1 <= m <= arr.length

解题思路:我的思路是将把每个group的起始索引和结束索引记录到一个数组sub_range里面,sub_range的初始值为[[1,max(arr)]],然后倒序处理arr,根据arr[i]的值对sub_range进行拆分,每做一次拆分,计算更新后的sub_range是否有某个元素的range的长度正好为m。

代码如下:

class Solution(object):
    def findLatestStep(self, arr, m):
        """
        :type arr: List[int]
        :type m: int
        :rtype: int
        """
        sub_range = [[1,max(arr)]]

        def binarySearch(a, l, r, x):
            if r >= l:
                mid = int(l + (r - l) / 2)
                if a[mid][0] <= x and a[mid][1] >= x:
                    return mid
                elif a[mid][0] > x:
                    return binarySearch(a, l, mid - 1, x)
                elif a[mid][1] < x:
                    return binarySearch(a, mid + 1, r, x)
            else:
                return -1


        if len(arr) == m:return m

        for i in range(len(arr)-1,-1,-1):
            mid = binarySearch(sub_range,0,len(sub_range)-1,arr[i])
            if mid == -1:continue
            if sub_range[mid][1] == sub_range[mid][0]:
                del sub_range[mid]
                continue
            end = sub_range[mid][1]
            sub_range[mid][1] = arr[i] - 1
            if end - (arr[i]+1) >= m:
                sub_range.insert(mid+1,[arr[i]+1,end])
            if sub_range[mid][1] - sub_range[mid][0] + 1 == m or  end - (arr[i]+1) + 1 == m:
                return i
            if sub_range[mid][1] - sub_range[mid][0] < m:
                del sub_range[mid]
                continue
        return -1
原文地址:https://www.cnblogs.com/seyjs/p/13985689.html