当前位置:网站首页>leetcode-2280:表示一个折线图的最少线段数
leetcode-2280:表示一个折线图的最少线段数
2022-07-02 23:55:00 【菊头蝙蝠】
leetcode-2280:表示一个折线图的最少线段数
题目
给你一个二维整数数组 stockPrices ,其中 stockPrices[i] = [dayi, pricei] 表示股票在 dayi 的价格为 pricei 。折线图 是一个二维平面上的若干个点组成的图,横坐标表示日期,纵坐标表示价格,折线图由相邻的点连接而成。比方说下图是一个例子:

请你返回要表示一个折线图所需要的 最少线段数 。
示例 1:
输入:stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
输出:3
解释:
上图为输入对应的图,横坐标表示日期,纵坐标表示价格。
以下 3 个线段可以表示折线图:
- 线段 1 (红色)从 (1,7) 到 (4,4) ,经过 (1,7) ,(2,6) ,(3,5) 和 (4,4) 。
- 线段 2 (蓝色)从 (4,4) 到 (5,4) 。
- 线段 3 (绿色)从 (5,4) 到 (8,1) ,经过 (5,4) ,(6,3) ,(7,2) 和 (8,1) 。
可以证明,无法用少于 3 条线段表示这个折线图。
示例 2:
输入:stockPrices = [[3,4],[1,2],[7,8],[2,3]]
输出:1
解释:
如上图所示,折线图可以用一条线段表示。
解题
方法一:判断斜率相等(转化成乘法)
将除法a[0]/b[0]==a[1]/b[1]转化为乘法,就可以避免精度问题,四舍五入。很有可能两个不同的斜率,但是四舍五入成相等了。
这道题其实就是计算这个折线有多少种斜率
(注意转换为乘法会溢出,因此要用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;
}
};
边栏推荐
- Set up nacos2 X cluster steps and problems encountered
- 为什么网站打开速度慢?
- [daily training] 871 Minimum refueling times
- 【AutoSAR 十二 模式管理】
- lex && yacc && bison && flex 配置的问题
- [Luogu p4320] road meets (round square tree)
- 【AutoSAR 八 OS】
- Briefly talk about other uses of operation and maintenance monitoring
- Kubernetes simple introduction to writing YML
- Nc50528 sliding window
猜你喜欢

【AutoSAR 九 C/S原理架构】

Pageoffice - bug modification journey

Hundreds of continuous innovation to create free low code office tools

AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决

2022中国3D视觉企业(引导定位、分拣场景)厂商名单

【AutoSAR 十三 NVM】

【AutoSAR 十 IO架构】
![[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)

百数不断创新,打造自由的低代码办公工具

Hdu3507 (slope DP entry)
随机推荐
Preview word documents online
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
Linux Software: how to install redis service
1.11 - 总线
【AutoSAR 五 方法论】
Set up nacos2 X cluster steps and problems encountered
Rust所有权(非常重要)
mm中的GAN模型架构
【AutoSAR 七 工具链简介】
Shell 实现文件基本操作(sed-编辑、awk-匹配)
百度智能云牵头打造智能云综合标准化平台
NC24325 [USACO 2012 Mar S]Flowerpot
Logback configuration file
MySQL 23 classic interview hanging interviewer
NC20806 区区区间间间
lex && yacc && bison && flex 配置的问题
【AutoSAR 二 AppL概述】
Automated defect analysis in electronic microscopic images
[pulsar document] concepts and architecture
Gan model architecture in mm