Skip to content

Commit 6bf13b1

Browse files
committed
Add solution #1560
1 parent ca98d08 commit 6bf13b1

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,368 LeetCode solutions in JavaScript
1+
# 1,369 LeetCode solutions in JavaScript
22

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

@@ -1191,6 +1191,7 @@
11911191
1557|[Minimum Number of Vertices to Reach All Nodes](./solutions/1557-minimum-number-of-vertices-to-reach-all-nodes.js)|Medium|
11921192
1558|[Minimum Numbers of Function Calls to Make Target Array](./solutions/1558-minimum-numbers-of-function-calls-to-make-target-array.js)|Medium|
11931193
1559|[Detect Cycles in 2D Grid](./solutions/1559-detect-cycles-in-2d-grid.js)|Medium|
1194+
1560|[Most Visited Sector in a Circular Track](./solutions/1560-most-visited-sector-in-a-circular-track.js)|Easy|
11941195
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|
11951196
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11961197
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 1560. Most Visited Sector in a Circular Track
3+
* https://leetcode.com/problems/most-visited-sector-in-a-circular-track/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer n and an integer array rounds. We have a circular track which consists of n
7+
* sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of
8+
* m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For
9+
* example, round 1 starts at sector rounds[0] and ends at sector rounds[1]
10+
*
11+
* Return an array of the most visited sectors sorted in ascending order.
12+
*
13+
* Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise
14+
* direction (See the first example).
15+
*/
16+
17+
/**
18+
* @param {number} n
19+
* @param {number[]} rounds
20+
* @return {number[]}
21+
*/
22+
var mostVisited = function(n, rounds) {
23+
const sectorVisits = new Array(n + 1).fill(0);
24+
let maxVisits = 0;
25+
26+
for (let i = 1; i < rounds.length; i++) {
27+
let start = rounds[i - 1];
28+
const end = rounds[i];
29+
30+
while (start !== end) {
31+
sectorVisits[start]++;
32+
maxVisits = Math.max(maxVisits, sectorVisits[start]);
33+
start = start === n ? 1 : start + 1;
34+
}
35+
}
36+
37+
sectorVisits[rounds[rounds.length - 1]]++;
38+
maxVisits = Math.max(maxVisits, sectorVisits[rounds[rounds.length - 1]]);
39+
40+
const result = [];
41+
for (let i = 1; i <= n; i++) {
42+
if (sectorVisits[i] === maxVisits) {
43+
result.push(i);
44+
}
45+
}
46+
47+
return result;
48+
};

0 commit comments

Comments
 (0)