当前位置:网站首页>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;
}
};
边栏推荐
- Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- Multiprocess programming (II): Pipeline
- Hdu3507 (slope DP entry)
- leetcode-871:最低加油次数
- [IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
- Vulkan-实践第一弹
- 【AutoSAR 六 描述文件】
- An excellent orm in dotnet circle -- FreeSQL
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
猜你喜欢
为什么网站打开速度慢?
【AutoSAR 十二 模式管理】
Redis21 classic interview questions, extreme pull interviewer
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Sentry developer contribution Guide - configure pycharm
指针进阶(一)
1.12 - 指令
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
【AutoSAR 六 描述文件】
Vulkan performance and refinement
随机推荐
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
tail -f 、tail -F、tailf的区别
The most painful programming problem in 2021, adventure of code 2021 Day24
[MCU project training] eight way answering machine
Thread start and priority
指针进阶(一)
LeedCode1480. Dynamic sum of one-dimensional array
MySQL multi table joint deletion
【AutoSAR 六 描述文件】
Shell 实现文件基本操作(切割、排序、去重)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
【AutoSAR 九 C/S原理架构】
1.12 - 指令
Vulkan performance and refinement
Overlay of shutter (Pop-Up)
Problèmes de configuration lex & yacc & Bison & Flex
JSON conversion tool class
1.11 - 总线
There is an unknown problem in inserting data into the database
About the practice topic of screen related to unity screen, unity moves around a certain point inside