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. 兄弟连学python---网络简介

    网络简介 1.什么是网络 网络是辅助双方能够连接在一起的工具 使用网络的目的 为了联通多方然后进行通讯,能够让软件在不同的电脑上运行,相互传输数据 网络的发展 网络协议 什么是协议 约定俗成的,没有理 ...

  2. 20. Web proxies (网页代理 4个)

    用于评估Web应用程序漏洞的基于Java的Web代理. 它支持在运行时编辑/查看HTTP / HTTPS消息,以更改Cookie和表单字段等项. 它包括网络流量记录器,网络蜘蛛,哈希计算器和用于测试常 ...

  3. C# DataView数据处理的几种方法

    1.找到刚删除的所有列 DataView dv = dt.DefaultView; dv.Delete(); dv.Delete(); //找出所有刚删除掉的行 1.5 dv.RowStateFilt ...

  4. win10 uwp 读取resw资源文件

    ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse(); ResourceMap resourceMa ...

  5. 在linxu机器ansible上运行启动django项目命令

    source py3env/bin/activate  进入虚拟环境 cd /xiangmulujing     进入项目路径 然后就可以执行运行命令了 python manage.py runser ...

  6. Docker系列(二)镜像下载、操作容器、镜像导入导出等操作

    一.镜像操作 列出镜像 $sudo docker images 从dockerhub检索image $docker search image_name 下载image $docker pull ima ...

  7. js 父类选择前后差异

    $(this).parent().prev().text(""); $(this).parent().text(""); 如果这俩个顺序调换则再使用prev() ...

  8. 将Json数据 填充到 实例类 的函数

    /// <summary> /// 将Json数据 填充到 实例类 /// </summary> /// <typeparam name="T"> ...

  9. 【Dubbo源码学习】负载均衡算法(2)-轮询算法的实现

    @Overrideprotected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL ur ...

  10. 前端需要了解的HTTP协议

    目录 HTTP协议 HTTP概述 HTTP之URL HTTP状态码 HTTP首部 HTTP首部字段 HTTP通用首部字段 HTTP请求首部字段 HTTP工作原理 HTTP报文内的HTTP信息 HTTP ...