LeetCode 2409. Count Days Spent Together【前缀和,容斥原理】简单

这篇具有很好参考价值的文章主要介绍了LeetCode 2409. Count Days Spent Together【前缀和,容斥原理】简单。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

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.

题意:Alice 和 Bob 计划分别去罗马开会。给出四个字符串 arriveAliceleaveAlicearriveBobleaveBob 。Alice 会在日期 arriveAliceleaveAlice 之间在城市里(日期为闭区间),而 Bob 在日期 arriveBobleaveBob 之间在城市里(日期为闭区间)。每个字符串都包含 5 个字符,格式为 "MM-DD" ,对应着一个日期的月和日。

返回 Alice和 Bob 同时在罗马的天数。可以假设所有日期都在 同一个 自然年,而且 不是 闰年。每个月份的天数分别为:[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


解法 前缀和+容斥原理

设计一个 f 来计算输入中的每个日子在一年中是第几天。计算输入中的每个日子在一年中是第几天时,可以利用前缀和数组来降低每次计算的复杂度。知道每个日子是一年中的第几天后,比较算出两人到达日子的最大值,离开日子的最小值,然后利用减法计算重合的日子。这也是容斥原理的应用。文章来源地址https://www.toymoban.com/news/detail-416402.html

class Solution {
private:
    int days[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
public:
    int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
        function<int(string)> f = [&](const string &a) {
            int m = (a[0] - '0') * 10 + (a[1] - '0');
            int d = (a[3] - '0') * 10 + (a[4] - '0');
            return days[m - 1] + d;
        };
        return max(min(f(leaveAlice), f(leaveBob)) - max(f(arriveAlice), f(arriveBob)) + 1, 0);
    }
};

到了这里,关于LeetCode 2409. Count Days Spent Together【前缀和,容斥原理】简单的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [数论第四节]容斥原理/博弈论/NIM游戏

    (|Acup Bcup C|=|A|+|B|+|C|-|Acap B|-|Acap C|-|Bcap C|+|Acap Bcap C|) (|displaystyle cup_{i=1}^n A_i |=sum_{i}|A_i|-sum_{i,j} |A_i cap A_j|+ldots +(-1)^{n+1}|cap_{i=1}^n A_i |) 时间复杂度: (C_n^1+C_n^2+C_n^3···+C_n^n=2^n-1) (O(2^n-1)) 等式右边有 (2^n-1) 项,每一项表示选取若干个集合相交的情况,可以通过

    2024年02月13日
    浏览(30)
  • Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)

    Capacity To Ship Packages Within D Days Medium 8.6K 179 Companies A conveyor belt has packages that must be shipped from one port to another within days days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maxi

    2024年02月09日
    浏览(27)
  • 算法基础学习笔记——⑬高斯消元\组合计数\容斥原理

    ✨ 博主: 命运之光 ✨ 专栏: 算法基础学习 目录 ✨高斯消元 ✨组合计数 🍓通过预处理逆元的方式求组合数: 🍓Lucas定理: 🍓分解质因数法求组合数: 前言: 算法学习笔记记录日常分享,需要的看哈O(∩_∩)O,感谢大家的支持! 高斯消元(Gaussian Elimination)是一种用于解线

    2024年02月07日
    浏览(28)
  • Leetcode 357. Count Numbers with Unique Digits

    Given an integer n, return the count of all numbers with unique digits, x, where 0 = x 1 0 n 0 = x 10^n 0 = x 1 0 n . f(0) = 1 f(1) = 10 f(k) = 9 * 9 * 8 * … * (9 - k + 2)

    2024年02月19日
    浏览(33)
  • LeetCode2085. Count Common Words With One Occurrence

    Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays. Example 1: Input: words1 = [“leetcode”,“is”,“amazing”,“as”,“is”], words2 = [“amazing”,“leetcode”,“is”] Output: 2 Explanation: “leetcode” appears exactly once in each of the two arrays. We count this s

    2024年01月16日
    浏览(27)
  • Leetcode 3045. Count Prefix and Suffix Pairs II

    Leetcode 3045. Count Prefix and Suffix Pairs II 1. 解题思路 2. 代码实现 题目链接:3045. Count Prefix and Suffix Pairs II 这一题的话思路上就是一个Trie树的思路来寻找前序字符,然后由于题目要求要同时满足前序和后序两个条件,因此找到每一个单词的前序子串之后再判断一下其是否同时为后

    2024年02月21日
    浏览(31)
  • [Leetcode] 0014. 最长公共前缀

    点击上方,跳转至Leetcode 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串  \\\"\\\" 。 示例 1: 示例 2: 提示: 1 = strs.length = 200 0 = strs[i].length = 200 strs[i] 仅由小写英文字母组成 横向扫描:写一个longestCommonPrefix方法,用于返回两个字符串的

    2024年02月10日
    浏览(37)
  • leetcode14. 最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 解题方法: 1.首先找到数组中长度最短的数据,与数组第一个数进行交换(公共前缀的长度肯定不会大于列表中长度最短的字符串) 2.接着 因为求最长公共前缀,将数组第

    2024年01月21日
    浏览(36)
  • 【leetcode】动态规划::前缀和

    标题:【leetcode】前缀和 @水墨不写bug 正文开始: 给定一个长度为n的数组a1​,a2​,....an​. 接下来有q次查询, 每次查询有两个参数l, r. 对于每个询问, 请输出al​+al+1​+....+ar​ 输入描述: 第一行包含两个整数n和q. 第二行包含n个整数, 表示a1​,a2​,....an​. 接下来q行,每行包含

    2024年04月11日
    浏览(27)
  • 【leetcode】前缀和

    内容摘抄自: 小而美的算法技巧:前缀和数组 | labuladong 的算法小抄 一维数组的前缀和 看这个  preSum  数组,若想求索引区间  [1, 4]  内的所有元素之和, 就可以通过  preSum[5] - preSum[1]  得出。 二维数组的前缀和 如leetcode 304: 注意任意子矩阵的元素和可以转化成它周边几

    2024年02月13日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包