当前位置:网站首页>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;
}
};
边栏推荐
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
- 百数不断创新,打造自由的低代码办公工具
- Free we media essential tools sharing
- Helm basic learning
- leetcode-871:最低加油次数
- 世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
- leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
- Shell 实现文件基本操作(切割、排序、去重)
- Linux Software: how to install redis service
- 1.11 - 总线
猜你喜欢
leetcode-2280:表示一个折线图的最少线段数
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
【AutoSAR 九 C/S原理架构】
Pageoffice - bug modification journey
【AutoSAR 四 BSW概述】
Rust所有权(非常重要)
Automated defect analysis in electron microscopic images-论文阅读笔记
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
1.12 - 指令
随机推荐
LeedCode1480. Dynamic sum of one-dimensional array
指针进阶(一)
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
mysql 多表联合删除
Meaning of Tencent cloud free SSL certificate extension file
mm中的GAN模型架构
tail -f 、tail -F、tailf的区别
Shell 实现文件基本操作(切割、排序、去重)
Introduction of UART, RS232, RS485, I2C and SPI
深度剖析数据在内存中的存储
【AutoSAR 四 BSW概述】
Briefly talk about other uses of operation and maintenance monitoring
Rust string slicing, structs, and enumeration classes
Graduation summary
node_modules删不掉
使用jenkins之二Job
leetcode-224:基本计算器
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Multiprocess programming (I): basic concepts
leetcode-849:到最近的人的最大距离