-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1560. Most Visited Sector in a Circular Track.cpp
74 lines (53 loc) · 2.11 KB
/
1560. Most Visited Sector in a Circular Track.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds[0] and ends at sector rounds[1]
Return an array of the most visited sectors sorted in ascending order.
Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).
Example 1:
Input: n = 4, rounds = [1,3,1,2]
Output: [1,2]
Explanation: The marathon starts at sector 1. The order of the visited sectors is as follows:
1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon)
We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.
Example 2:
Input: n = 2, rounds = [2,1,2,1,2,1,2,1,2]
Output: [2]
Example 3:
Input: n = 7, rounds = [1,3,5,7]
Output: [1,2,3,4,5,6,7]
Constraints:
2 <= n <= 100
1 <= m <= 100
rounds.length == m + 1
1 <= rounds[i] <= n
rounds[i] != rounds[i + 1] for 0 <= i < m
class Solution {
public:
vector<int> mostVisited(int n, vector<int>& rounds) {
vector<int> res;
vector<int> sector(n, 0);
int len=rounds.size();
for(int i=1;i<len;i++){
int start=rounds[i-1];
int end=rounds[i];
if(start<=end){
for(int j=start;j<end;j++)
sector[j]++;
} else {
for(int j=start; j<n;j++)
sector[j]++;
for(int j=0;j<end;j++)
sector[j]++;
}
}
sector[rounds[0]-1]++; // for initial start
int maxi=sector[0];
for(int i=1;i<n;i++){
if(sector[i]>maxi)
maxi=sector[i];
}
for(int i=0;i<n;i++){
if(sector[i]==maxi)
res.push_back(i+1); // we are considering 0 as round 1 and n-1 as round n
}
return res;
}
};