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的更多相关文章

  1. leetcode每日刷题计划-简单篇day5

    刷题成习惯以后感觉挺好的 Num 27 移除元素 Remove Element 跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len class Solution { public: i ...

  2. leetcode每日刷题计划-简单篇day3

    收到swe提前批面试hhh算是ep挂了的后续 努力刷题呀争取今年冲进去! Num 21 合并两个有序链表 Merge Two Sorted Lists 注意新开的链表用来输出结果的是ListNode ...

  3. leetcode每日刷题计划-简单篇day1

    orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...

  4. leetcode每日刷题计划-简单篇day12

    Num 125 验证回文串 Valid Palindrome 非常有收货的一道题嘻嘻嘻,本来是考试期间划水挑的题,坑点有点多 第一个是注意对temp1和temp2中途更新的判断 第二个是字符串频繁的作 ...

  5. leetcode每日刷题计划-简单篇day10

    跳题,不熟悉的留到周末再做. 保持冷静,不信画饼. num 100 相同的树 Same Tree 做法可能不是特别简洁,注意一下.最后判断完子树以后,要确定根的数值是一样的 然后在isleaf的判定先 ...

  6. leetcode每日刷题计划-简单篇day9

    Num 38 报数 Count and Say 题意读起来比较费劲..看懂了题还是不难的 注意最后的长度是sz的长度,开始写错写的len 在下次计算的时候len要更新下 说明 直接让char和int进 ...

  7. leetcode每日刷题计划-简单篇day6

    突发奇想&胡思乱想的一天 银行家算法证明错了并挂在黑板上的可怜希希 Num 53 最大子序和 Maximum Subarray O(n)的算法实现了,分治法有空补 class Solution ...

  8. leetcode每日刷题计划-简单篇day2

    今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...

  9. leetcode每日刷题计划-简单篇day13

    Num 169 先码,回头再说,摩尔算法... tle了 class Solution { public: int majorityElement(vector<int>& num ...

随机推荐

  1. java-递归练习

    1.从键盘接收一个文件夹路径,统计该文件夹大小 public class Test1 { /** * @param args * 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小 * * 从键盘接 ...

  2. uwp应用在debug模式下运行正常,编译为release版本的时候抛出异常

    原因是在代码中使用了dynamic关键字,导致release时.net native优化了代码造成元数据丢失 所以在代码中要尽量不用dynamic.

  3. 電腦清理緩存bat文件源碼

    @echo off echo 正在清除系統垃圾文件,請稍等 ...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._ ...

  4. 一键安装Davinci

    官网:https://edp963.github.io/davinci/index.html 下载地址:https://github.com/edp963/davinci   环境:Centos7   ...

  5. tomcat 闪退问题排查

    由于启动tomcat回出现闪退情况,看不到异常 解决方法: 一. 打开startup.bat文件,在最下面 在文本的最后敲上pause,保存后重新运行startup.bat,这时候窗口会留在桌面上(调 ...

  6. NanoPC-T4/RK3399开发板Ubuntu FriendlyCore系统开机自动运行客户程序

    RK3399开机自动运行客户程序 比如hellohello.c 交叉编译:aarch64-linux-gcc hello.c -o hello使用SecureCRT软件通过串口下载到开发板rz修改文件 ...

  7. WPF 开源项目

    Modern UI for WPF :http://mui.codeplex.com/ 利用Wpf实现Win8 Modern样式的开源项目wpf toolkit :http://wpftoolkit. ...

  8. python3-基础7

    协程函数 面向过程编程 递归与二分法 内置函数   lambda 模块与包的使用 import from ... import ... 常用模块 ########################### ...

  9. 一种C语言实现面向对象特性的继承,多态

    基类: //.h typedef int (*TELE_SEND_CB)(char *pdata, int len); //函数表结构 typedef struct tele_pro_base_vtb ...

  10. jmeter 之beanshell preprocessor

    Bean Shell PreProcessor 可参考https://blog.csdn.net/shimengran107/article/details/76849748 是一个前置处理器,它可以 ...