当前位置:网站首页>Leetcode-2280: represents the minimum number of line segments of a line graph
Leetcode-2280: represents the minimum number of line segments of a line graph
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-2280: Represents the minimum number of line segments of a line graph
subject
Here's a two-dimensional array of integers stockPrices
, among stockPrices[i] = [dayi, pricei]
Indicates that the stock is dayi The price of is pricei . Broken line diagram It is a graph composed of several points on a two-dimensional plane , The abscissa represents the date , The ordinate represents the price , A line chart is formed by connecting adjacent points . For example, the following figure is an example :
Please return what you need to represent a line chart Minimum number of line segments .
Example 1:
Input :stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
Output :3
explain :
The figure above shows the corresponding input diagram , The abscissa represents the date , The ordinate represents the price .
following 3 A line segment can represent a line graph :
- Line segment 1 ( Red ) from (1,7) To (4,4) , after (1,7) ,(2,6) ,(3,5) and (4,4) .
- Line segment 2 ( Blue ) from (4,4) To (5,4) .
- Line segment 3 ( green ) from (5,4) To (8,1) , after (5,4) ,(6,3) ,(7,2) and (8,1) .
Can prove that , Cannot use less than 3 A line segment represents this line chart .
Example 2:
Input :stockPrices = [[3,4],[1,2],[7,8],[2,3]]
Output :1
explain :
As shown in the figure above , A line graph can be represented by a line segment .
Problem solving
Method 1 : Judge that the slope is equal ( Convert to multiplication )
Divide by a[0]/b[0]==a[1]/b[1] Into Multiplication , Accuracy problems can be avoided , rounding . There are probably two different slopes , But it's rounded to equal .
This problem is actually to calculate the slope of this broken line
( Note that conversion to multiplication will overflow , So use uint64_t)
class Solution {
public:
bool compareVec(vector<int>& a,vector<int>& b){
return (uint64_t)a[0]*b[1]==(uint64_t)a[1]*b[0];
}
int minimumLines(vector<vector<int>>& stockPrices) {
int n=stockPrices.size();
vector<int> pre;
int count=0;
sort(stockPrices.begin(),stockPrices.end(),[](vector<int>&a,vector<int>&b){
return a[0]<b[0];
});
for(int i=1;i<n;i++){
vector<int> vec={
stockPrices[i][0]-stockPrices[i-1][0],stockPrices[i][1]-stockPrices[i-1][1]};
if(i==1) count=1;
else{
if(compareVec(pre,vec)) continue;
else count++;
}
pre=vec;
}
return count;
}
};
边栏推荐
- Shell implements basic file operations (SED edit, awk match)
- 如何系统学习机器学习
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- Sentry developer contribution Guide - configure pycharm
- Overlay of shutter (Pop-Up)
- Multi process programming (III): message queue
- 【AutoSAR 六 描述文件】
- Two common methods and steps of character device registration
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- Hundreds of continuous innovation to create free low code office tools
猜你喜欢
Sentry developer contribution Guide - configure pycharm
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
【AutoSAR 四 BSW概述】
图解网络:什么是虚拟路由器冗余协议 VRRP?
2022上半年值得被看见的10条文案,每一句都能带给你力量!
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
Use Jenkins II job
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
字符设备注册常用的两种方法和步骤
【AutoSAR 十二 模式管理】
随机推荐
node_modules删不掉
【JetCache】JetCache的配置说明和注解属性说明
【AutoSAR 六 描述文件】
[MCU project training] eight way answering machine
Nc17059 queue Q
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Sentry developer contribution Guide - configure pycharm
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
[golang syntax] map common errors golang panic: assignment to entry in nil map
如何系统学习机器学习
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Vulkan并非“灵药“
【AutoSAR 十一 通信相关机制】
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Multiprocess programming (4): shared memory
百数不断创新,打造自由的低代码办公工具
Linux Software: how to install redis service
Extension of flutter
Problèmes de configuration lex & yacc & Bison & Flex