leetcode每日刷题计划-简单篇day11
Num 121 买卖股票的最佳时期 Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len=prices.capacity();
if(prices.capacity()==) return ;
int minn=prices[];
int maxn=prices[];
int ans=;
for(int i=;i<=len-;i++)
{
maxn=prices[i];
minn=prices[i];
for(int j=i+;j<=len-;j++)
{
if(prices[j]>maxn)
maxn=prices[j];
}
ans=max(ans,maxn-minn);
}
return ans;
}
};
Num 122 买卖股票的最佳时期 || Best Time to Buy and Sell Stock ||
先上一个正确但是TLE的方法。。。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans[];
int len=prices.capacity();
if(len==||len==) return ;
memset(ans,,sizeof(ans));
if(len==) return max(,prices[]-prices[]);
ans[len-]=max(,(int)(prices[len-]-prices[len-]));
for(int j=len-;j>=;j--)
{
for(int i=;i<len--j;i++)
{
ans[j]=max(ans[j],max(ans[j+i],ans[j+i]+(int)(prices[j+i]-prices[j])));
}
}
return ans[];
}
};
TLE 代码
默认里面带的max函数只能处理两个
另外这道题是贪心,刚开始重复计算tle了
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans=;
int len=prices.capacity();
for(int i=;i<len-;i++)
ans=ans+max(,prices[i+]-prices[i]);
return ans;
}
};
leetcode每日刷题计划-简单篇day11的更多相关文章
- leetcode每日刷题计划-简单篇day5
刷题成习惯以后感觉挺好的 Num 27 移除元素 Remove Element 跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len class Solution { public: i ...
- leetcode每日刷题计划-简单篇day3
收到swe提前批面试hhh算是ep挂了的后续 努力刷题呀争取今年冲进去! Num 21 合并两个有序链表 Merge Two Sorted Lists 注意新开的链表用来输出结果的是ListNode ...
- leetcode每日刷题计划-简单篇day1
orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...
- leetcode每日刷题计划-简单篇day12
Num 125 验证回文串 Valid Palindrome 非常有收货的一道题嘻嘻嘻,本来是考试期间划水挑的题,坑点有点多 第一个是注意对temp1和temp2中途更新的判断 第二个是字符串频繁的作 ...
- leetcode每日刷题计划-简单篇day10
跳题,不熟悉的留到周末再做. 保持冷静,不信画饼. num 100 相同的树 Same Tree 做法可能不是特别简洁,注意一下.最后判断完子树以后,要确定根的数值是一样的 然后在isleaf的判定先 ...
- leetcode每日刷题计划-简单篇day9
Num 38 报数 Count and Say 题意读起来比较费劲..看懂了题还是不难的 注意最后的长度是sz的长度,开始写错写的len 在下次计算的时候len要更新下 说明 直接让char和int进 ...
- leetcode每日刷题计划-简单篇day6
突发奇想&胡思乱想的一天 银行家算法证明错了并挂在黑板上的可怜希希 Num 53 最大子序和 Maximum Subarray O(n)的算法实现了,分治法有空补 class Solution ...
- leetcode每日刷题计划-简单篇day2
今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...
- leetcode每日刷题计划-简单篇day13
Num 169 先码,回头再说,摩尔算法... tle了 class Solution { public: int majorityElement(vector<int>& num ...
随机推荐
- npm,bower安装失败
安装一些软件时,总是会出现一些莫名其妙的问题.我在安装node.js时,默认安装到C:\Program Files\nodejs之后,安装bower时,bower_components默认装到C:\W ...
- 前端自定义format函数,做字符串格式化功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Mysql主外键
主键 primary key 创建表时直接加上主键: create table student1(id int primary key, name varchar(20), age int, g ...
- [Python学习笔记] 数字类型及操作
数字类型 整数类型 十进制:1110,-123 二进制:以0B或0b开头 0b110,-0B101 八进制:以0O或0o开头 0o123,-0O567 十六进制:以0X或0x开头 0x555,-0X8 ...
- 国内写的比较好的markdown教程
国内写的比较好的markdown教程,预览图如下 点击跳转
- taro 报错及解决
1.解决:taro 升级到最新版(npm install -g @tarojs/cli) 错误 组件编译 组件src/pages/xxx/xxx.tsx编译失败! TypeError: callee. ...
- 服务器对接码云webhooks
服务安装git php代码(外网必须可以访问) <?php //本地路径滚adminasdfdasfasdf $file = "/wwwroot/webhooksLog.txt&quo ...
- Winfrom BackgroundWorker
using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection ...
- Git在已有的分支上新建个人分支开发
在Dev分支上新建一个分支(可以通过Git TE网页创建) 然后就可以从Source下拉列表中看到新建的分支(new_name1)了. 远程分支创建完成之后,就可以在本机上面使用Git GUI Her ...
- apache 2.2 和 2.4 访问控制区别 (require 替代 deny)
apache 2.4权限配置 Order命令已从Apache 2.4中删除 注意:使用require指令时,需要在指令外添加<RequireAll></RequireAll>标 ...