返回介绍

solution / 2400-2499 / 2409.Count Days Spent Together / README_EN

发布于 2024-06-17 01:03:06 字数 5197 浏览 0 评论 0 收藏 0

2409. Count Days Spent Together

中文文档

Description

Alice and Bob are traveling to Rome for separate business meetings.

You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from the dates arriveAlice to leaveAlice (inclusive), while Bob will be in the city from the dates arriveBob to leaveBob (inclusive). Each will be a 5-character string in the format "MM-DD", corresponding to the month and day of the date.

Return_ the total number of days that Alice and Bob are in Rome together._

You can assume that all dates occur in the same calendar year, which is not a leap year. Note that the number of days per month can be represented as: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].

 

Example 1:

Input: arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
Output: 3
Explanation: Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.

Example 2:

Input: arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
Output: 0
Explanation: There is no day when Alice and Bob are in Rome together, so we return 0.

 

Constraints:

  • All dates are provided in the format "MM-DD".
  • Alice and Bob's arrival dates are earlier than or equal to their leaving dates.
  • The given dates are valid dates of a non-leap year.

Solutions

Solution 1: Simulation

We convert the dates into days, and then calculate the number of days both people are in Rome.

The time complexity is $O(C)$, and the space complexity is $O(C)$. Here, $C$ is a constant.

class Solution:
  def countDaysTogether(
    self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str
  ) -> int:
    a = max(arriveAlice, arriveBob)
    b = min(leaveAlice, leaveBob)
    days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    x = sum(days[: int(a[:2]) - 1]) + int(a[3:])
    y = sum(days[: int(b[:2]) - 1]) + int(b[3:])
    return max(y - x + 1, 0)
class Solution {
  private int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  public int countDaysTogether(
    String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
    String a = arriveAlice.compareTo(arriveBob) < 0 ? arriveBob : arriveAlice;
    String b = leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob;
    int x = f(a), y = f(b);
    return Math.max(y - x + 1, 0);
  }

  private int f(String s) {
    int i = Integer.parseInt(s.substring(0, 2)) - 1;
    int res = 0;
    for (int j = 0; j < i; ++j) {
      res += days[j];
    }
    res += Integer.parseInt(s.substring(3));
    return res;
  }
}
class Solution {
public:
  vector<int> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
    string a = arriveAlice < arriveBob ? arriveBob : arriveAlice;
    string b = leaveAlice < leaveBob ? leaveAlice : leaveBob;
    int x = f(a), y = f(b);
    return max(0, y - x + 1);
  }

  int f(string s) {
    int m, d;
    sscanf(s.c_str(), "%d-%d", &m, &d);
    int res = 0;
    for (int i = 0; i < m - 1; ++i) {
      res += days[i];
    }
    res += d;
    return res;
  }
};
func countDaysTogether(arriveAlice string, leaveAlice string, arriveBob string, leaveBob string) int {
  days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  f := func(s string) int {
    m, _ := strconv.Atoi(s[:2])
    d, _ := strconv.Atoi(s[3:])
    res := 0
    for i := 0; i < m-1; i++ {
      res += days[i]
    }
    res += d
    return res
  }
  a, b := arriveAlice, leaveBob
  if arriveAlice < arriveBob {
    a = arriveBob
  }
  if leaveAlice < leaveBob {
    b = leaveAlice
  }
  x, y := f(a), f(b)
  ans := y - x + 1
  if ans < 0 {
    return 0
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文