当前位置:网站首页>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;
}
};
边栏推荐
- Is there a free text to speech tool to help recommend?
- NC24840 [USACO 2009 Mar S]Look Up
- [IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
- NC24325 [USACO 2012 Mar S]Flowerpot
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
- Nc17059 queue Q
- Array common operation methods sorting (including ES6) and detailed use
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
- Overlay of shutter (Pop-Up)
猜你喜欢

如何系统学习机器学习

1.11 - bus

Sentry developer contribution Guide - configure pycharm

Automated defect analysis in electronic microscopic images

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

Why is the website slow to open?

University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement

Shell 实现文件基本操作(切割、排序、去重)
随机推荐
如何系统学习机器学习
Preview word documents online
多进程编程(一):基本概念
Gan model architecture in mm
1.12 - 指令
线程的启动与优先级
MySQL 23 classic interview hanging interviewer
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
pod生命周期详解
使用jenkins之二Job
Rust string slicing, structs, and enumeration classes
Lex & yacc & bison & flex configuration problems
多进程编程(五):信号量
[MCU project training] eight way answering machine
Two common methods and steps of character device registration
NC50528 滑动窗口
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Hdu3507 (slope DP entry)
NC50965 Largest Rectangle in a Histogram