Skip to content

Commit ed2633b

Browse files
committedApr 20, 2025
Add solution #1562
1 parent 937aaf4 commit ed2633b

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,370 LeetCode solutions in JavaScript
1+
# 1,371 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1193,6 +1193,7 @@
11931193
1559|[Detect Cycles in 2D Grid](./solutions/1559-detect-cycles-in-2d-grid.js)|Medium|
11941194
1560|[Most Visited Sector in a Circular Track](./solutions/1560-most-visited-sector-in-a-circular-track.js)|Easy|
11951195
1561|[Maximum Number of Coins You Can Get](./solutions/1561-maximum-number-of-coins-you-can-get.js)|Medium|
1196+
1562|[Find Latest Group of Size M](./solutions/1562-find-latest-group-of-size-m.js)|Medium|
11961197
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11971198
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11981199
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 1562. Find Latest Group of Size M
3+
* https://leetcode.com/problems/find-latest-group-of-size-m/
4+
* Difficulty: Medium
5+
*
6+
* Given an array arr that represents a permutation of numbers from 1 to n.
7+
*
8+
* You have a binary string of size n that initially has all its bits set to zero. At each step
9+
* i (assuming both the binary string and arr are 1-indexed) from 1 to n, the bit at position
10+
* arr[i] is set to 1.
11+
*
12+
* You are also given an integer m. Find the latest step at which there exists a group of ones
13+
* of length m. A group of ones is a contiguous substring of 1's such that it cannot be extended
14+
* in either direction.
15+
*
16+
* Return the latest step at which there exists a group of ones of length exactly m. If no such
17+
* group exists, return -1.
18+
*/
19+
20+
/**
21+
* @param {number[]} arr
22+
* @param {number} m
23+
* @return {number}
24+
*/
25+
var findLatestStep = function(arr, m) {
26+
const lengths = new Array(arr.length + 2).fill(0);
27+
const count = new Map();
28+
let result = -1;
29+
30+
for (let step = 0; step < arr.length; step++) {
31+
const pos = arr[step];
32+
const left = lengths[pos - 1];
33+
const right = lengths[pos + 1];
34+
const newLength = left + right + 1;
35+
36+
lengths[pos - left] = newLength;
37+
lengths[pos + right] = newLength;
38+
lengths[pos] = newLength;
39+
40+
count.set(left, (count.get(left) || 0) - 1);
41+
count.set(right, (count.get(right) || 0) - 1);
42+
count.set(newLength, (count.get(newLength) || 0) + 1);
43+
44+
if (count.get(m) > 0) result = step + 1;
45+
}
46+
47+
return result;
48+
};

0 commit comments

Comments
 (0)
Please sign in to comment.