Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5355c0f

Browse files
add 1560
1 parent 5a7bc60 commit 5355c0f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1560|[Most Visited Sector in a Circular Track](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1560.java) ||Array|Easy|
1112
|1558|[Minimum Numbers of Function Calls to Make Target Array](https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1558.java) ||Medium|Greedy|
1213
|1557|[Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1557.java) ||Medium|Graph|
1314
|1556|[Thousand Separator](https://leetcode.com/problems/thousand-separator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1556.java) ||Easy|String|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Collections;
8+
9+
public class _1560 {
10+
public static class Solution1 {
11+
public List<Integer> mostVisited(int n, int[] rounds) {
12+
List<Integer> result = new ArrayList<>();
13+
Map<Integer, Integer> map = new HashMap<>();
14+
for (int i = 0; i < rounds.length - 1; i++) {
15+
if (rounds[i] > rounds[i + 1]) {
16+
if (i == 0) {
17+
map.put(rounds[i], 1);
18+
}
19+
for (int j = rounds[i] + 1; j <= n; j++) {
20+
map.put(j, map.getOrDefault(j, 0) + 1);
21+
}
22+
for (int j = 1; j <= rounds[i + 1]; j++) {
23+
map.put(j, map.getOrDefault(j, 0) + 1);
24+
}
25+
} else {
26+
int j;
27+
if (i == 0) {
28+
j = rounds[i];
29+
} else {
30+
j = rounds[i] + 1;
31+
}
32+
for (; j <= rounds[i + 1]; j++) {
33+
map.put(j, map.getOrDefault(j, 0) + 1);
34+
}
35+
}
36+
}
37+
38+
int mostVisitedCount = 0;
39+
for (int key : map.keySet()) {
40+
mostVisitedCount = Math.max(mostVisitedCount, map.get(key));
41+
}
42+
for (int key : map.keySet()) {
43+
if (map.get(key) == mostVisitedCount) {
44+
result.add(key);
45+
}
46+
}
47+
Collections.sort(result);
48+
return result;
49+
}
50+
}
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1560;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1560Test {
12+
private static _1560.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1560.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList(1, 2), solution1.mostVisited(4, new int[]{1, 3, 1, 2}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(Arrays.asList(2), solution1.mostVisited(2, new int[]{2, 1, 2, 1, 2, 1, 2, 1, 2}));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7), solution1.mostVisited(7, new int[]{1, 3, 5, 7}));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)