当前位置:网站首页>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;
}
};
边栏推荐
- Basic use of shell script
- Sentry developer contribution Guide - configure pycharm
- Introduction and use of ftrace tool
- [daily training] 871 Minimum refueling times
- Automated defect analysis in electronic microscopic images
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 研发一款国产ARM智能边缘计算网关需要什么
- Rust字符串切片、结构体和枚举类
- Andorid gets the system title bar height
猜你喜欢

How to systematically learn machine learning

【AutoSAR 七 工具链简介】

Introduction of UART, RS232, RS485, I2C and SPI

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力

【AutoSAR 二 AppL概述】

Is there a free text to speech tool to help recommend?
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

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

Rust所有权(非常重要)
随机推荐
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
Basic use of shell script
[MCU project training] eight way answering machine
Vulkan is not a "panacea"“
[Luogu p4320] road meets (round square tree)
关于QByteArray存储十六进制 与十六进制互转
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Multiprocess programming (4): shared memory
node_modules删不掉
pod生命周期详解
文件操作IO-Part2
Form form instantiation
Array common operation methods sorting (including ES6) and detailed use
关于XML一些介绍和注意事项
How SQLSEVER removes data with duplicate IDS
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
测试右移:线上质量监控 ELK 实战
Introduction and use of ftrace tool
mm中的GAN模型架构
【luogu P4320】道路相遇(圆方树)