当前位置:网站首页>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;
}
};
边栏推荐
- 【案例分享】让新时代教育发展与“数”俱进
- Preview word documents online
- [MCU project training] eight way answering machine
- pageoffice-之bug修改之旅
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- [Luogu p4320] road meets (round square tree)
- Problèmes de configuration lex & yacc & Bison & Flex
- NC24840 [USACO 2009 Mar S]Look Up
- Introduction and use of ftrace tool
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢
为什么网站打开速度慢?
【单片机项目实训】八路抢答器
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
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
logback配置文件
使用jenkins之二Job
文件操作IO-Part2
【AutoSAR 二 AppL概述】
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
MySQL 23 classic interview hanging interviewer
随机推荐
Markdown tutorial
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
【AutoSAR 十一 通信相关机制】
Logback configuration file
Gan model architecture in mm
Extension of flutter
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Nc17059 queue Q
Introduction of UART, RS232, RS485, I2C and SPI
pod生命周期详解
字符设备注册常用的两种方法和步骤
Leetcode 294. Flip game II (game theory)
【AutoSAR 四 BSW概述】
NC24840 [USACO 2009 Mar S]Look Up
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
【Pulsar文档】概念和架构/Concepts and Architecture
DotNet圈里一个优秀的ORM——FreeSql
Sentry developer contribution Guide - configure pycharm
Form form instantiation