当前位置:网站首页>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;
}
};
边栏推荐
- How SQLSEVER removes data with duplicate IDS
- About qbytearray storage hexadecimal and hexadecimal conversion
- Vulkan practice first bullet
- logback配置文件
- MySQL multi table joint deletion
- cordova-plugin-device获取设备信息插件导致华为审核不通过
- Basic use of shell script
- 【日常训练】871. 最低加油次数
- University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
- 深度剖析数据在内存中的存储
猜你喜欢

1.12 - 指令

Rust string slicing, structs, and enumeration classes

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)

Vulkan-实践第一弹

字符设备注册常用的两种方法和步骤

Nacos+openfeign error reporting solution

指针进阶(一)

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

Detailed explanation of pod life cycle

University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
随机推荐
leetcode-241:为运算表达式设计优先级
Two common methods and steps of character device registration
leetcode-224:基本计算器
node_ Modules cannot be deleted
1.11 - 总线
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
[MCU project training] eight way answering machine
【luogu P4320】道路相遇(圆方树)
Rust字符串切片、结构体和枚举类
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
Markdown tutorial
如何系统学习机器学习
【Pulsar文档】概念和架构/Concepts and Architecture
Vulkan is not a "panacea"“
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
Extension of flutter
1.12 - Instructions
Machine learning: numpy version linear regression predicts Boston house prices
mm中的GAN模型架构