Class Solution
- java.lang.Object
-
- g1501_1600.s1562_find_latest_group_of_size_m.Solution
-
public class Solution extends Object
1562 - Find Latest Group of Size M.Medium
Given an array
arrthat represents a permutation of numbers from1ton.You have a binary string of size
nthat initially has all its bits set to zero. At each stepi(assuming both the binary string andarrare 1-indexed) from1ton, the bit at positionarr[i]is set to1.You are also given an integer
m. Find the latest step at which there exists a group of ones of lengthm. A group of ones is a contiguous substring of1’s such that it cannot be extended in either direction.Return the latest step at which there exists a group of ones of length exactly
m. If 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.
Constraints:
n == arr.length1 <= m <= n <= 1051 <= arr[i] <= n- All integers in
arrare distinct.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intfindLatestStep(int[] arr, int m)
-