Skip to content

Commit 34426a2

Browse files
committed
feat: add solutions to lc problem: No.1560
No.1560.Most Visited Sector in a Circular Track
1 parent b9ef46e commit 34426a2

File tree

6 files changed

+186
-22
lines changed

6 files changed

+186
-22
lines changed

solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,90 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:考虑开始、结束的位置关系**
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Solution:
65+
def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
66+
if rounds[0] <= rounds[-1]:
67+
return list(range(rounds[0], rounds[-1] + 1))
68+
return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))
6369
```
6470

6571
### **Java**
6672

6773
<!-- 这里可写当前语言的特殊实现逻辑 -->
6874

6975
```java
76+
class Solution {
77+
public List<Integer> mostVisited(int n, int[] rounds) {
78+
int m = rounds.length - 1;
79+
List<Integer> ans = new ArrayList<>();
80+
if (rounds[0] <= rounds[m]) {
81+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
82+
ans.add(i);
83+
}
84+
} else {
85+
for (int i = 1; i <= rounds[m]; ++i) {
86+
ans.add(i);
87+
}
88+
for (int i = rounds[0]; i <= n; ++i) {
89+
ans.add(i);
90+
}
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
vector<int> mostVisited(int n, vector<int>& rounds) {
103+
int m = rounds.size() - 1;
104+
vector<int> ans;
105+
if (rounds[0] <= rounds[m])
106+
{
107+
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
108+
}
109+
else
110+
{
111+
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
112+
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
113+
}
114+
return ans;
115+
}
116+
};
117+
```
70118
119+
### **Go**
120+
121+
```go
122+
func mostVisited(n int, rounds []int) []int {
123+
m := len(rounds) - 1
124+
var ans []int
125+
if rounds[0] <= rounds[m] {
126+
for i := rounds[0]; i <= rounds[m]; i++ {
127+
ans = append(ans, i)
128+
}
129+
} else {
130+
for i := 1; i <= rounds[m]; i++ {
131+
ans = append(ans, i)
132+
}
133+
for i := rounds[0]; i <= n; i++ {
134+
ans = append(ans, i)
135+
}
136+
}
137+
return ans
138+
}
71139
```
72140

73141
### **...**

solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,79 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
57+
if rounds[0] <= rounds[-1]:
58+
return list(range(rounds[0], rounds[-1] + 1))
59+
return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))
5660
```
5761

5862
### **Java**
5963

6064
```java
65+
class Solution {
66+
public List<Integer> mostVisited(int n, int[] rounds) {
67+
int m = rounds.length - 1;
68+
List<Integer> ans = new ArrayList<>();
69+
if (rounds[0] <= rounds[m]) {
70+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
71+
ans.add(i);
72+
}
73+
} else {
74+
for (int i = 1; i <= rounds[m]; ++i) {
75+
ans.add(i);
76+
}
77+
for (int i = rounds[0]; i <= n; ++i) {
78+
ans.add(i);
79+
}
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
vector<int> mostVisited(int n, vector<int>& rounds) {
92+
int m = rounds.size() - 1;
93+
vector<int> ans;
94+
if (rounds[0] <= rounds[m])
95+
{
96+
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
97+
}
98+
else
99+
{
100+
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
101+
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
102+
}
103+
return ans;
104+
}
105+
};
106+
```
61107
108+
### **Go**
109+
110+
```go
111+
func mostVisited(n int, rounds []int) []int {
112+
m := len(rounds) - 1
113+
var ans []int
114+
if rounds[0] <= rounds[m] {
115+
for i := rounds[0]; i <= rounds[m]; i++ {
116+
ans = append(ans, i)
117+
}
118+
} else {
119+
for i := 1; i <= rounds[m]; i++ {
120+
ans = append(ans, i)
121+
}
122+
for i := rounds[0]; i <= n; i++ {
123+
ans = append(ans, i)
124+
}
125+
}
126+
return ans
127+
}
62128
```
63129

64130
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> mostVisited(int n, vector<int>& rounds) {
4+
int m = rounds.size() - 1;
5+
vector<int> ans;
6+
if (rounds[0] <= rounds[m])
7+
{
8+
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
9+
}
10+
else
11+
{
12+
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
13+
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func mostVisited(n int, rounds []int) []int {
2+
m := len(rounds) - 1
3+
var ans []int
4+
if rounds[0] <= rounds[m] {
5+
for i := rounds[0]; i <= rounds[m]; i++ {
6+
ans = append(ans, i)
7+
}
8+
} else {
9+
for i := 1; i <= rounds[m]; i++ {
10+
ans = append(ans, i)
11+
}
12+
for i := rounds[0]; i <= n; i++ {
13+
ans = append(ans, i)
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
class Solution {
22
public List<Integer> mostVisited(int n, int[] rounds) {
3-
int[] ans = new int[n];
4-
for (int i = 0; i < rounds.length; i++) {
5-
rounds[i]--;
6-
}
7-
ans[rounds[0]]++;
8-
for (int i = 0; i < rounds.length - 1; i++) {
9-
int start = rounds[i];
10-
int end = rounds[i + 1];
11-
if (end <= start) {
12-
end += n;
3+
int m = rounds.length - 1;
4+
List<Integer> ans = new ArrayList<>();
5+
if (rounds[0] <= rounds[m]) {
6+
for (int i = rounds[0]; i <= rounds[m]; ++i) {
7+
ans.add(i);
138
}
14-
for (int j = start + 1; j <= end; j++) {
15-
ans[j % n]++;
9+
} else {
10+
for (int i = 1; i <= rounds[m]; ++i) {
11+
ans.add(i);
1612
}
17-
}
18-
19-
int max = Arrays.stream(ans).max().orElse(-1);
20-
List<Integer> list = new ArrayList<>();
21-
for (int i = 0; i < ans.length; i++) {
22-
if (ans[i] == max) {
23-
list.add(i + 1);
13+
for (int i = rounds[0]; i <= n; ++i) {
14+
ans.add(i);
2415
}
2516
}
26-
return list;
17+
return ans;
2718
}
2819
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
3+
if rounds[0] <= rounds[-1]:
4+
return list(range(rounds[0], rounds[-1] + 1))
5+
return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))

0 commit comments

Comments
 (0)