Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
yi-ge
cpp-practice
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpp-practice
/
src
/
math
/
count_days_spent_together.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
39 lines (36 loc) · 1.85 KB
main
Breadcrumbs
cpp-practice
/
src
/
math
/
count_days_spent_together.cpp
Copy path
Top
File metadata and controls
Code
Blame
39 lines (36 loc) · 1.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// 统计共同度过的日子数
// https://leetcode.cn/problems/count-days-spent-together
// INLINE ../../images/math/count_days_spent_together.jpeg
// 分别计算出每个日子是一年中的第几天后求差
// https://leetcode.cn/problems/count-days-spent-together/solution/tong-ji-gong-tong-du-guo-de-ri-zi-shu-by-1iwp/
#include <headers.hpp>
class Solution {
public:
int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob,
string leaveBob) {
vector<int> datesOfMonths = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31}; // 每个月对应的天数,注意二月份是28天
vector<int> prefixSum(1, 0); // 统计到每个月的天数的前缀和
for (int date : datesOfMonths) {
prefixSum.emplace_back(prefixSum.back() + date);
}
int arriveAliceDay = calculateDayOfYear(
arriveAlice, prefixSum); // 计算爱丽丝到达的那天是这一年的第几天
int leaveAliceDay = calculateDayOfYear(
leaveAlice, prefixSum); // 计算爱丽丝离开的那天是这一年的第几天
int arriveBobDay = calculateDayOfYear(
arriveBob, prefixSum); // 计算鲍勃到达的那天是这一年的第几天
int leaveBobDay = calculateDayOfYear(
leaveBob, prefixSum); // 计算鲍勃离开的那天是这一年的第几天
return max(
0,
min(leaveAliceDay, leaveBobDay) - max(arriveAliceDay, arriveBobDay) +
1); // 计算两人共同在一起的天数,即离开时间的最小值减去到达时间的最大值再加1
}
int calculateDayOfYear(string day, const vector<int> &prefixSum) {
int month = stoi(day.substr(0, 2)); // 获取日期字符串中的月份
int date = stoi(day.substr(3)); // 获取日期字符串中的日期
return prefixSum[month - 1] + date; // 返回这一年的第几天
}
};
You can’t perform that action at this time.