Skip to content

Commit 45778a5

Browse files
add 2409
1 parent de2c09a commit 45778a5

File tree

3 files changed

+101
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+101
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java) || Easy ||
5252
| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java) || Easy ||
5353
| 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java) || Easy ||
54+
| 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java) || Easy ||
5455
| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java) || Easy ||
5556
| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java) || Medium ||
5657
| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
public class _2409 {
4+
public static class Solution1 {
5+
/**
6+
* Brute force: check each day of the 365 days if both of them are in Rome.
7+
*/
8+
public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
9+
int[] monthToDays = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
10+
int days = 0;
11+
String[] arriveAliceParts = arriveAlice.split("-");
12+
String[] leaveAliceParts = leaveAlice.split("-");
13+
String[] arriveBobParts = arriveBob.split("-");
14+
String[] leaveBobParts = leaveBob.split("-");
15+
for (int i = 1; i < monthToDays.length; i++) {
16+
int daysInMonth = monthToDays[i];
17+
for (int j = 1; j <= daysInMonth; j++) {
18+
if (bothInRome(i, j, arriveAliceParts, leaveAliceParts, arriveBobParts, leaveBobParts)) {
19+
days++;
20+
}
21+
}
22+
}
23+
return days;
24+
}
25+
26+
private boolean bothInRome(int month, int day, String[] arriveAliceParts, String[] leaveAliceParts, String[] arriveBobParts, String[] leaveBobParts) {
27+
int aliceArriveMonth = Integer.parseInt(arriveAliceParts[0]);
28+
int aliceArriveDay = Integer.parseInt(arriveAliceParts[1]);
29+
int aliceLeaveMonth = Integer.parseInt(leaveAliceParts[0]);
30+
int aliceLeaveDay = Integer.parseInt(leaveAliceParts[1]);
31+
32+
int bobArriveMonth = Integer.parseInt(arriveBobParts[0]);
33+
int bobArriveDay = Integer.parseInt(arriveBobParts[1]);
34+
int bobLeaveMonth = Integer.parseInt(leaveBobParts[0]);
35+
int bobLeaveDay = Integer.parseInt(leaveBobParts[1]);
36+
37+
return inRome(aliceArriveMonth, aliceArriveDay, aliceLeaveMonth, aliceLeaveDay, month, day) && inRome(bobArriveMonth, bobArriveDay, bobLeaveMonth, bobLeaveDay, month, day);
38+
}
39+
40+
private boolean inRome(int arriveMonth, int arriveDay, int leaveMonth, int leaveDay, int month, int day) {
41+
if (month < arriveMonth || month > leaveMonth) {
42+
return false;
43+
}
44+
if (month > arriveMonth && month < leaveMonth) {
45+
return true;
46+
}
47+
if (month == arriveMonth) {
48+
if (arriveMonth == leaveMonth) {
49+
return arriveDay <= day && leaveDay >= day;
50+
} else {
51+
return arriveDay <= day;
52+
}
53+
}
54+
//now, only this case: month == leaveMonth
55+
return day <= leaveDay;
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2409;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2409Test {
10+
private static _2409.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2409.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.countDaysTogether("08-15", "08-18", "08-16", "08-19"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.countDaysTogether("10-01", "10-31", "11-01", "12-31"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(49, solution1.countDaysTogether("09-01", "10-19", "06-19", "10-20"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(27, solution1.countDaysTogether("08-06", "12-08", "08-06", "09-01"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(27, solution1.countDaysTogether("08-06", "12-08", "02-04", "09-01"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)