文章

2409. 统计共同度过的日子数

问题2409 统计共同度过的日子数 ☆☆☆☆☆

https://leetcode.cn/problems/count-days-spent-together/

这个题目从解题思路来说,确实没有什么难度,稍作分析,可以确定,取Alice和Bob较大的到达日期作为起始日期,取Alice和Bob较小的离开日期作为结束日期,然后计算起始日期到结束日期包含的天数即可。因为日期的格式是固定的,直接取对应子串即可解析出month和day,计算包含的天数时注意可能跨月。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	private static final int[] DAYS_PER_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
		int arriveAliceMonth = month(arriveAlice);
		int arriveAliceDay = day(arriveAlice);
		int leaveAliceMonth = month(leaveAlice);
		int leaveAliceDay = day(leaveAlice);
		int arriveBobMonth = month(arriveBob);
		int arriveBobDay = day(arriveBob);
		int leaveBobMonth = month(leaveBob);
		int leaveBobDay = day(leaveBob);

		int biggerArriveMonth, biggerArriveDay;
		int cmp = compare(arriveAliceMonth, arriveAliceDay, arriveBobMonth, arriveBobDay);
		if (cmp < 0) {
			biggerArriveMonth = arriveBobMonth;
			biggerArriveDay = arriveBobDay;
		} else {
			biggerArriveMonth = arriveAliceMonth;
			biggerArriveDay = arriveAliceDay;
		}

		int smallerLeaveMonth, smallerLeaveDay;
		cmp = compare(leaveAliceMonth, leaveAliceDay, leaveBobMonth, leaveBobDay);
		if (cmp < 0) {
			smallerLeaveMonth = leaveAliceMonth;
			smallerLeaveDay = leaveAliceDay;
		} else {
			smallerLeaveMonth = leaveBobMonth;
			smallerLeaveDay = leaveBobDay;
		}

		return days(biggerArriveMonth, biggerArriveDay, smallerLeaveMonth, smallerLeaveDay);
	}

	private int compare(int month1, int day1, int month2, int day2) {
		int cmp = month1 - month2;
		if (cmp != 0) {
			return cmp;
		}

		return day1 - day2;
	}

	private int days(int startMonth, int startDay, int endMonth, int endDay) {
		int cmp = compare(startMonth, startDay, endMonth, endDay);
		if (cmp > 0) {
			return 0;
		} else if (cmp == 0) {
			return 1;
		} else {
			if (startMonth == endMonth) {
				return endDay - startDay + 1;
			} else {
				int days = 0;
				days += DAYS_PER_MONTH[startMonth] - startDay + 1;
				for (int i = startMonth + 1; i < endMonth; i++) {
					days += DAYS_PER_MONTH[i];
				}
				days += endDay;

				return days;
			}
		}
	}

	private int month(String date) {
		return Integer.parseInt(date.substring(0, 2));
	}

	private int day(String date) {
		return Integer.parseInt(date.substring(3, 5));
	}

在 LeetCode 查看

本文由作者按照 CC BY 4.0 进行授权