-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.java
More file actions
45 lines (45 loc) · 1.21 KB
/
day6.java
File metadata and controls
45 lines (45 loc) · 1.21 KB
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
//Problem 1:Most visited sector in circular track
//https://leetcode.com/problems/most-visited-sector-in-a-circular-track/?envType=problem-list-v2&envId=array
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
int[]arr=new int[n];
List<Integer> ans=new ArrayList<>();
int i=0,j,k;
while(i<rounds.length-1){
j=rounds[i];
if(rounds[i]<rounds[i+1]){
while(j<rounds[i+1]){
arr[j-1]++;
j++;
}
}
else{
k=1;
while(j<=n){
arr[j-1]++;
j++;
}
while(k<rounds[i+1]){
arr[k-1]++;
k++;
}
}
i++;
}
arr[rounds[rounds.length-1]-1]++;
int max=Integer.MIN_VALUE;
for(i=0;i<n;i++){
if(arr[i]>max){
ans.clear();
ans.add(i+1);
max=arr[i];
}
else if(arr[i]==max){
ans.add(i+1);
}
}
return ans;
}
}
//TC:O(nm)
//SC:O(n)