LC.P2409[统计共同度过的日子数]

方法一:模拟

将日期转化为天数进行模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
static int[] months = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] sum = new int[13];

public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
for (int i = 1; i <= 12; ++i) sum[i] = sum[i - 1] + months[i - 1];
int a1 = calculateDay(arriveAlice);
int b1 = calculateDay(leaveAlice);
int a2 = calculateDay(arriveBob);
int b2 = calculateDay(leaveBob);
return Math.max(0, Math.min(b1, b2) - Math.max(a1, a2) + 1);
}

private int calculateDay(String s) {
int month = Integer.parseInt(s.substring(0, 2));
int day = Integer.parseInt(s.substring(3));
return sum[month - 1] + day;
}
}
  • 时间复杂度:$O(1)$
  • 空间复杂度:$O(1)$