Skip to content

Commit 97828cf

Browse files
add 1604
1 parent 9c9d7a1 commit 97828cf

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-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+
|1604|[Alert Using Same Key-Card Three or More Times in a One Hour Period](https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1604.java) ||Medium|String, Ordered Map|
1112
|1603|[Design Parking System](https://leetcode.com/problems/design-parking-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1603.java) ||Easy|Design|
1213
|1601|[Maximum Number of Achievable Transfer Requests](https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1601.java) ||Hard|Backtracking|
1314
|1598|[Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1598.java) ||Easy|Stack|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class _1604 {
10+
public static class Solution1 {
11+
public List<String> alertNames(String[] keyName, String[] keyTime) {
12+
Map<String, List<String>> map = new HashMap<>();
13+
for (int i = 0; i < keyName.length; i++) {
14+
if (!map.containsKey(keyName[i])) {
15+
map.put(keyName[i], new ArrayList<>());
16+
}
17+
map.get(keyName[i]).add(keyTime[i]);
18+
}
19+
List<String> people = new ArrayList<>();
20+
for (String person : map.keySet()) {
21+
List<String> times = map.get(person);
22+
List<Integer> minutes = new ArrayList<>();
23+
for (String time : times) {
24+
String[] hourAndMin = time.split(":");
25+
Integer minute = Integer.parseInt(hourAndMin[0]) * 60 + Integer.parseInt(hourAndMin[1]);
26+
minutes.add(minute);
27+
}
28+
Collections.sort(minutes);
29+
for (int i = 0; i < minutes.size() - 2; i++) {
30+
if (minutes.get(i + 2) - minutes.get(i) <= 60) {
31+
people.add(person);
32+
break;
33+
}
34+
}
35+
}
36+
Collections.sort(people);
37+
return people;
38+
}
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1604;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
11+
public class _1604Test {
12+
private static _1604.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1604.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList("daniel"), solution1.alertNames(new String[]{"daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"}, new String[]{"10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00"}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(Arrays.asList("bob"), solution1.alertNames(new String[]{"alice", "alice", "alice", "bob", "bob", "bob", "bob"}, new String[]{"12:01", "12:00", "18:00", "21:00", "21:20", "21:30", "23:00"}));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(Arrays.asList(), solution1.alertNames(new String[]{"john", "john", "john"}, new String[]{"23:58", "23:59", "00:01"}));
32+
}
33+
34+
@Test
35+
public void test4() {
36+
assertEquals(Arrays.asList("clare", "leslie"), solution1.alertNames(new String[]{"leslie", "leslie", "leslie", "clare", "clare", "clare", "clare"}, new String[]{"13:00", "13:20", "14:00", "18:00", "18:51", "19:30", "19:49"}));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)