当前位置:网站首页>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;
}
};
边栏推荐
- 详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
- 【AutoSAR 七 工具链简介】
- One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
- Introduction and use of ftrace tool
- FAQ | FAQ for building applications for large screen devices
- Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
- Detailed explanation of pod life cycle
- Hdu3507 (slope DP entry)
- Nacos+openfeign error reporting solution
- [daily training] 871 Minimum refueling times
猜你喜欢
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
【AutoSAR 十二 模式管理】
Redis21 classic interview questions, extreme pull interviewer
【案例分享】让新时代教育发展与“数”俱进
【AutoSAR 五 方法论】
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
pod生命周期详解
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
随机推荐
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
form表单实例化
Leetcode 294. Flip game II (game theory)
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
About qbytearray storage hexadecimal and hexadecimal conversion
Gan model architecture in mm
pod生命周期详解
【AutoSAR 十三 NVM】
Markdown使用教程
【雅思阅读】王希伟阅读P1(阅读判断题)
Set up nacos2 X cluster steps and problems encountered
【雅思阅读】王希伟阅读P2(阅读填空)
logback配置文件
The most painful programming problem in 2021, adventure of code 2021 Day24
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Helm basic learning
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
Attributeerror: 'tuple' object has no attribute 'layer' problem solving