首页 > 其他分享 >LeetCode 1353. Maximum Number of Events That Can Be Attended

LeetCode 1353. Maximum Number of Events That Can Be Attended

时间:2024-05-18 11:20:16浏览次数:13  
标签:Attended attend 1353 Number events int minHeap event day

原题链接在这里:https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/description/

题目:

You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.

You can attend an event i at any day d where startTimei <= d <= endTimei. You can only attend one event at any time d.

Return the maximum number of events you can attend.

Example 1:

Input: events = [[1,2],[2,3],[3,4]]
Output: 3
Explanation: You can attend all the three events.
One way to attend them all is as shown.
Attend the first event on day 1.
Attend the second event on day 2.
Attend the third event on day 3.

Example 2:

Input: events= [[1,2],[2,3],[3,4],[1,2]]
Output: 4

Constraints:

  • 1 <= events.length <= 105
  • events[i].length == 2
  • 1 <= startDayi <= endDayi <= 105

题解:

Sort the events based on start.

Have a minHeap to keep the end of event.

Incrementing the day from 1 to 100000, first poll the event that is endded before today d.

For all the event that starts today, add them into the minHeap.

If minHeap is not empay, greedy attend a event that is ending soon. res++, minHeap.poll().

Time Complexity: O(nlogn + d). n = events.length. d = 100000.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int maxEvents(int[][] events) {
 3         Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));
 4         PriorityQueue<Integer> minHeap = new PriorityQueue<>();
 5         int res = 0;
 6         int i = 0;
 7         int n = events.length;
 8         for(int d = 1; d <= 100000; d++){
 9             while(!minHeap.isEmpty() && minHeap.peek() < d){
10                 minHeap.poll();
11             }
12 
13             while(i < n && events[i][0] == d){
14                 minHeap.add(events[i++][1]);
15             }
16 
17             if(!minHeap.isEmpty()){
18                 minHeap.poll();
19                 res++;
20             }
21         }
22 
23         return res;
24     }
25 }

类似Meeting Rooms II.

标签:Attended,attend,1353,Number,events,int,minHeap,event,day
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18199158

相关文章

  • 【jmeter】.SampleException: Mismatch between expected number of columns: 生成报
    1、问题现象Causedby:org.apache.jmeter.report.core.SampleException:Consumerfailedwithmessage:Consumerfailedwithmessage:Mismatchbetweenexpectednumberofcolumns:17andcolumnsinCSVfile:3,checkyourjmeter.save.saveservice.*configurationor......
  • LeetCode 2595. Number of Even and Odd Bits
    原题链接在这里:https://leetcode.com/problems/number-of-even-and-odd-bits/description/题目:Youaregivena positive integer n.Let even denotethenumberofevenindicesinthebinaryrepresentationof n (0-indexed)withvalue 1.Let odd denotethen......
  • 264 ugly number II 丑数
    问题描述Anuglynumberisapositiveintegerwhoseprimefactorsarelimitedto2,3,and5.Givenanintegern,returnthenth*uglynumber*.解释:一个丑数是一个正整数,其公因子只有2,3,5。给定数字n,求第n个丑数案例:Input:n=10Output:12Explanation:[1,2,......
  • LeetCode 1915. Number of Wonderful Substrings
    原题链接在这里:https://leetcode.com/problems/number-of-wonderful-substrings/description/题目:A wonderful stringisastringwhere atmostone letterappearsan odd numberoftimes.Forexample, "ccjjc" and "abab" arewonderful,but "ab&......
  • skipped: maximum number of running instances reached (1)
    Python的 apscheduler今天出现skipped:maximumnumberofrunninginstancesreached(1)问题产生的原因:设置了大量的任务,而APScheduler无法同时处理所有任务解决方法:调整APScheduler使用的线程池大小来增加并发处理任务的能力fromapscheduler.schedulers......
  • MySQL ROW_NUMBER 函数
    MySQLROW_NUMBER()语法MySQL ROW_NUMBER()从8.0版开始引入了功能。这ROW_NUMBER()是一个窗口函数或分析函数,它为从1开始应用的每一行分配一个序号。请注意,如果你使用MySQL版本低于8.0,你可以效仿的一些功能ROW_NUMBER()函数使用各种技术。以下显示了ROW_NUMBER()函数的语法:......
  • LeetCode 3009. Maximum Number of Intersections on the Chart
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart/description/题目:Thereisalinechartconsistingof n pointsconnectedbylinesegments.Youaregivena 1-indexed integerarray y.The kth pointhascoordinates......
  • CF55D Beautiful numbers
    题目链接:https://www.luogu.com.cn/problem/CF55D数位dp解法:所有非零位都能整除这个数,那么就是说这些非零位的公倍数能够整除这个数。那么按照通常情况我们定义dp数组的时候应该定义成dp[pos][num][gbs],表示当前枚举到了第几位、上次枚举到的数、之前所有数位的最小公倍数。那......
  • LeetCode 2597. The Number of Beautiful Subsets
    原题链接在这里:https://leetcode.com/problems/the-number-of-beautiful-subsets/description/题目:Youaregivenanarray nums ofpositiveintegersanda positive integer k.Asubsetof nums is beautiful ifitdoesnotcontaintwointegerswithanabsolut......
  • CF1842H Tenzing and Random Real Numbers 题解
    题目链接点击打开链接题目解法实数的概率好反直觉!对\(1\)做限制不是很好做,考虑变成正负性的限制(即对\(0\)做限制)令\(y_i=x_i-0.5\),那么限制就变成了\(y_i+y_j\le0,\;y_i+y_j\ge0\)这里要给出一些实数概率的结论:实数下,\(x=y\)的概率为\(0\),因为\(\frac{1}{\inft......