首页 > 其他分享 >LeetCode 3009. Maximum Number of Intersections on the Chart

LeetCode 3009. Maximum Number of Intersections on the Chart

时间:2024-05-13 09:09:57浏览次数:30  
标签:count tm int chart Chart points Number line Intersections

原题链接在这里:https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart/description/

题目:

There is a line chart consisting of n points connected by line segments. You are given a 1-indexed integer array y. The kth point has coordinates (k, y[k]). There are no horizontal lines; that is, no two consecutive points have the same y-coordinate.

We can draw an infinitely long horizontal line. Return the maximum number of points of intersection of the line with the chart.

Example 1:

Input: y = [1,2,1,2,1,3,2]
Output: 5
Explanation: As you can see in the image above, the line y = 1.5 has 5 intersections with the chart (in red crosses). You can also see the line y = 2 which intersects the chart in 4 points (in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 5 points. So the answer would be 5.

Example 2:

Input: y = [2,1,3,4,5]
Output: 2
Explanation: As you can see in the image above, the line y = 1.5 has 2 intersections with the chart (in red crosses). You can also see the line y = 2 which intersects the chart in 2 points (in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 2 points. So the answer would be 2.

Constraints:

  • 2 <= y.length <= 105
  • 1 <= y[i] <= 109
  • y[i] != y[i + 1] for i in range [1, n - 1]

题解:

It is to find out on the y axis, what is the maximum overlap intervals count.

Then use sweep line, at the start of interval, increase the count, after the end of interval, decrease the count. 

And make the maximum.

Chanllenging point is how to avoid double count of inclusive endpoint. like [1, 2], [2, 0]. 2 is counted as twice.

So double the endpoint value and use +1, -1 to make them exclusive with each other, then above becomes [2, 3], [4, 1].

The last interval, we don't need to +1, or -1, there is no following overlapping.

Time Complexity: O(nlogn). n = y.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int maxIntersectionCount(int[] y) {
 3         TreeMap<Integer, Integer> tm = new TreeMap<>();
 4         int n = y.length;
 5         for(int i = 1; i < n; i++){
 6             int s = 2 * y[i - 1];
 7             int e = 2 * y[i] + (i == n - 1 ? 0 : (y[i - 1] < y[i] ? -1 : 1));
 8 
 9             int min = Math.min(s, e);
10             int max = Math.max(s, e);
11             tm.put(min, tm.getOrDefault(min, 0) + 1);
12             tm.put(max + 1, tm.getOrDefault(max + 1, 0) - 1); // sweep line, after the max the count needs to decrease by 1
13         }
14 
15         int res = 0;
16         int count = 0;
17         for(int v : tm.values()){
18             count += v;
19             res = Math.max(res, count);
20         }
21 
22         return res;
23     }
24 }

 

标签:count,tm,int,chart,Chart,points,Number,line,Intersections
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18188575

相关文章

  • FlexibleButton - 一个小巧灵活的C语言按键处理库+SerialChart - 能将串口数据实时绘
    1、FlexibleButton-一个小巧灵活的C语言按键处理库FlexibleButton是一个基于标准C语言的小巧灵活的按键处理库,支持单击、连击、短按、长按、自动消抖,可以自由设置组合按键,可用于中断和低功耗场景。项目主页:https://github.com/murphyzhao/FlexibleButton该按键库解耦了......
  • Echarts设置饼状图保证你看的明明白白
    简单的饼状图<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>ECharts-动画</title>......
  • echarts折线鼠标悬浮时只显示了一条线的数据
    项目中对悬浮的值formatter进行了唯一给值,并没有针对每一个进行赋值问题代码大致为:formatter:(params)=>{return`${params[0].name}<br/>${params[0].值1}${params[0].值2}:${params[0].value==0?"-":Number(params[0].value).toLoca......
  • CF55D Beautiful numbers
    题目链接:https://www.luogu.com.cn/problem/CF55D数位dp解法:所有非零位都能整除这个数,那么就是说这些非零位的公倍数能够整除这个数。那么按照通常情况我们定义dp数组的时候应该定义成dp[pos][num][gbs],表示当前枚举到了第几位、上次枚举到的数、之前所有数位的最小公倍数。那......
  • 关于vue3中使用echarts设置tooltip的type为axis不显示的问题
    因为vue3中的数据对象是用的proxy监听的,要取值需要用value等方法取出来,解决方法:使用markRaw让echarts从监听对象变成普通对象!在Vue3中,markRaw是一个用于告诉Vue的响应性系统不要对某个对象进行转换或追踪其响应性的函数。当你有一个对象,并且你确定你不需要它成为响应性......
  • ECharts自定义提示框浮层内容
    因为提示框内容支持字符串模板和回调函数两种形式字符串模板模板变量有{a},{b},{c},{d},{e},分别表示系列名,数据名,数据值等等,但是trigger属性为axis的时候它数据条就很多了,就可以用{a0},{a1},{a2}这样子去拿数据跟数组下标一样(官网有详细示例)示例:在`option`中的`tooltip`里边写......
  • Echarts -- 实现动态加载series
    Echarts--实现动态加载series:https://blog.csdn.net/m0_74444744/article/details/134467184?ops_request_misc=&request_id=&biz_id=102&utm_term=echarts%20series&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-1344671......
  • 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......
  • TinyVue 3.15.0 正式发布,推出全新的 Charts 图表组件底座,功能更强、图表更丰富!
    你好,我是Kagol。我们非常高兴地宣布,2024年4月8日,TinyVue发布了v3.15.0......
  • .NET开源、功能强大、跨平台的图表库 - LiveCharts2
    https://www.cnblogs.com/Can-daydayup/p/18166862 思维导航前言项目介绍项目源代码BlazorWasm中快速使用项目更多图表截图项目源码地址优秀项目和框架精选DotNetGuide技术社区交流群前言今天大姚给大家分享一个.NET开源(MITLicense)、功能强大、简单、灵活、跨......