leetcode:122. Best Time to Buy and Sell Stock II

基于FPGA的ov5640摄像头简易图像采集装置详解 本文介绍一种基于FPGA的,通过流模式DDR实现OV5640摄像头数据采集并显示的方法。 阅读详情

 

最佳买卖点:

写的啥,太复杂了

/*
thinking:
1.get the smaller num to buy, 
2.assume cur num to buy if next is smaller ,not buy,
3.when cur<before, cur<next buy,


================
for each num:
buy or not: not bought ,< pre,<next
sell or not:have bought, > pre,>next 

*/

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        auto sum=0,buy=0,sell=0;
        auto len=prices.size();
        auto cant_buy=false,cant_sell=false;
     
        if(len<2) return -1;

        for(auto i=0;i<len;i++)
        {
            // buy
            if(!cant_buy && i<len-1 && prices[i]<prices[i+1] )
            {            
                buy=prices[i];
                // sum-=buy;
                cant_buy=true;//can not buy
                cant_sell=false;//can sell
                printf("<=====buy:%d\n",buy);  
            }
            //sell
            if(!cant_sell && i<len-1 && prices[i]>prices[i+1] )
            {
             sell=prices[i];
                sum-=buy;
                sum+=sell;
                cant_sell=true;//can not sell
                cant_buy=false;//can buy
                printf("=>sell:%d\n",sell);
                
            }
            printf("====price:%d\n",prices[i]);
        }
           return sum; 
        
    }
};

 

,,,,写的不对,,

/*
thinking:
1.get the smaller num to buy, 
2.assume cur num to buy if next is smaller ,not buy,
3.when cur<before, cur<next buy,


================
for each num:
buy or not: not bought ,< pre,<next
sell or not:have bought, > pre,>next 

*/

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        auto sum=0,buy=0,sell=0;
        auto len=prices.size();
        auto have_buy=false,have_sell=true;
     
        if(len<2) return -1;

        buy=prices[0];
        for(auto i=0;i<len;i++)
        {
            if( buy>prices[i])
            {
                buy=prices[i]; 
                have_buy=true;
                // // sell=prices[i];
                // continue;
            }
            // have_buy=true;
         
            printf("==>buy:%d\n",buy);
            if( have_buy && sell< prices[i] )
            {     
                sell=prices[i];
                 printf("sell:%d\n",sell);
                // continue;
            }
             // have_sell=true; 
            
            if(sell_now)
            { 
                sum+=sell-buy;
                buy=prices[i+1];sell=prices[i+1];
            }
               printf("====price:%d\n",prices[i]);
        }
           return sum; 
        
    }
};

 

强迫症,浪费太多时间了。。。。

就是考虑不清楚,条件没写清。

枚举法呀。再来。

浪费时间:直接学习别人的吧。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        auto sum=0,buy=0,sell=0;
        auto len=prices.size();
        auto get_buy_p=false,get_sell_p=false;
     
        if(len<2) return -1;

        //find the reverse sort
        
        if(prices[0]<prices[1])
            buy=prices[0];
        
        buy=prices[0];
        for(auto i=0;i<len-1;i++)
        {         
            if( prices[i] < prices[i+1])
            { 
                buy=prices[i]; 
                get_buy_p=true;
            }
            
            
            if(get_buy_p && prices[i] > prices[i+1] )
            {     
                sell=prices[i];
                get_sell_p=true;
                 printf("sell:%d\n",sell);
            }

            
            if(get_sell_p && get_buy_p)
            { 
                sum+=sell-buy;
                get_sell_p=false;  get_buy_p=false;
                // buy=prices[i+1];sell=prices[i+1];
            }
               printf("====price:%d\n",prices[i]);
        }
           return sum; 
        
    }
};

看懂大神的再写

果然是技巧啊,,,,厉害:

 

First we post the code here.

 

int maxProfit(vector<int> &prices) {
    int ret = 0;
    for (size_t p = 1; p < prices.size(); ++p) 
      ret += max(prices[p] - prices[p - 1], 0);    
    return ret;
}

 

Second, suppose the first sequence is "a <= b <= c <= d", the profit is "d - a = (b - a) + (c - b) + (d - c)" without a doubt. And suppose another one is "a <= b >= b' <= c <= d", the profit is not difficult to be figured out as "(b - a) + (d - b')". So you just target at monotone sequences.

 

 

 

Qt实现离线缓存地图(附完整源码) 阅读详情

相关推荐

基于opencv-ANN的水果识别模型训练

本文通过opencv-ann-mlp训练模型,实现对五种水果进行分类识别。本文详细介绍该项目的各个过程,包括数据准备、预处理、样本读取、训练数据集制作、训练标签制作、模型参数设置、模型训练,模型测试验证等,总结该过程需要注意的事项。

qq_15985873的博客 3973

【LeetCode】解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 367

基于Python的网络拓扑图绘制

最近写论文画了许多图,在这里记录一些。当然,如果仅仅是展示性图片的话也可以使用visio,但是这里我仍然想探究一下如何使用pyhon画出美观的网络拓扑图。(1)根据邻接矩阵画出网络拓扑图(2)画出网络拓扑图,并对节点和链路进行表示,包括对节点和链路的标号,颜色区分,以及尺寸区分。

m0_53961910的博客 7093

LeetCode 121: Best Time to Buy and Sell Stock && 122 : Best Time to Buy and Sell Stock II

问题描述 思路 两个值记录,一个值记录以i为结束位置为止最大盈利,另一个值记录到现在的最大盈利(可能不是以i为结束位置)。 Java实现 class Solution { public int maxProfit(int[] prices) { int maxCurrent=0; int maxSoFar=0; for(int i ...

zero 219

Halcon实战:基于图像清晰度自动筛选最佳对焦图片

本文详细介绍了如何使用Halcon实现基于图像清晰度的自动筛选最佳对焦图片技术。通过分析灰度值统计和标准差等核心指标,结合实战代码示例,帮助开发者高效处理工业检测中的对焦问题,提升自动化检测的准确性和效率。

weixin_27015733的博客 440

[VisionPro] CogPMAlignTool 工具详解

[VisionPro] [CogPMAlignTool] [模板匹配]

weixin_43163656的博客 1万+

Leetcode: 121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell Stock II

121 Question: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share o...

兔子窝 239

数字IC后端时序违例修复:setup/hold冲突与时钟树优化实战

时序收敛是数字IC后端设计中最关键的环节之一,直接影响芯片能否在目标频率下稳定工作。建立时间与保持时间的本质是寄存器对数据稳定性的物理约束,而实际工程中,setup违例和hold违例往往此消彼长,冲突的根源在于数据路径与时钟路径的延迟匹配。时钟树综合(CTS)作为连接逻辑与物理的关键步骤,其skew、insertion delay和transition的优化直接决定了时序修复的成败。通过useful skew技术、合理的单元替换和路径结构调整,可以系统性地解决setup/hold冲突,实现时序收敛。本文结合

weixin_28292761的博客 222

LeetCode122:Best Time to Buy and Sell Stock II

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie...

diehuojiang5959的博客 312

告别点不亮!STM32F103驱动HT1621段码屏的常见坑点与调试心得

本文详细解析了STM32F103驱动HT1621段码屏的五大常见问题及解决方案,包括硬件连接、时序控制、RAM地址映射等关键难点。通过实测数据和代码示例,帮助开发者快速排查显示异常问题,提升驱动开发效率。特别针对HT1621与STM32F103的兼容性问题提供了专业调试建议。

weixin_30925435的博客 379

Shopee前端反爬三要素:x-sap-Ri、x-sap-Sec与af-ac-enc-dat深度解析

电商前端反爬机制本质是运行时环境可信验证,其核心在于动态生成的请求指纹、会话签名与行为加密载荷。这类机制不依赖静态Token,而是基于浏览器真实交互(如鼠标轨迹、页面可见性、时间精度)实时计算,体现JS运行时环境不可模拟性。技术价值在于将风控前移至客户端,大幅提升自动化绕过成本;典型应用场景包括商品价格监控、竞品数据采集与合规爬虫开发。本文聚焦Shopee实战案例,深入拆解x-sap-Ri(设备+行为哈希指纹)、x-sap-Sec(动态密钥签名)及af-ac-enc-dat(AES加密行为载荷)三大字段的生

weixin_29015127的博客 401

Leetcode 122:Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one...

qq_34314736的博客 211

【图像超分】论文复现:CVPR 2024 | 轻量化超分 | 窗口注意力改进 | CAMixerSR的Pytorch源码复现,跑通源码,获得指标和超分结果,计算复杂度,架构梳理拆解,逐行注释!

本文介绍了超分辨率重建方法CAMixerSR的代码实现与运行指南。文章亮点包括:完整跑通源码获取PSNR/SSIM指标和可视化结果;详细解析模型架构与关键代码注释,帮助快速复现基线模型。实验部分提供了测试配置文件和训练设置说明,支持2K/4K/8K大图像超分和x2/x4轻量化超分。环境依赖PyTorch和BasicSR框架,数据集包含DIV2K等常用基准。测试结果与论文指标一致,可视化输出保存至指定目录。训练配置可自定义数据集路径、GPU数量等参数。该实现突出了CAMixerSR在降低W-MSA复杂度方面的

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 438

Leetcode 122: Best Time to Buy and Sell Stock II

Leetcode 122: Best Time to Buy and Sell Stock II

Enycin的专栏 546

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LeetCode 122 : Best Time to Buy and Sell Stock II

LeetCode 122 : Best Time to Buy and Sell Stock II题目大意已知股票在一段时间内的价格数组,并可以在任意时间买进或者卖出,求可以获得的最大利润。 Description Say you have an array for which the ith element is the price of a given stock on day i.Desig

Cecilia_whf的博客 488

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode: 122. Best Time to Buy and Sell Stock II

LeetCode: 122. Best Time to Buy and Sell Stock II 题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You ...

杨领well的专栏 659

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[leetcode]: 122. Best Time to Buy and Sell Stock II

1.题目描述Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy

沐沐余风的专栏 341

LeetCode : 122. Best Time to Buy and Sell Stock II 股票最大收益

试题 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy ...

心之所向 249

LeetCode题解: 122. Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one

qq_23666815的博客 278

leetcode题解-121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell St && 66. Plus One

今天的两道题都很简单,就直接在一篇博客里进行记录即可。 首先说一下66. Plus One这道题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading

呜呜哈的博客 479

LeetCode: Best Time to Buy and Sell Stock II [122]

【题目】 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). H

Harry Huang 883

LeetCode 122: Best Time to Buy and Sell Stock II

Difficulty: 3 Frequency: 1 Problem: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may comp

claviclelau的专栏 811

LeetCode之路:122. Best Time to Buy and Sell Stock II

LeetCode之路:122. Best Time to Buy and Sell Stock II 一、引言这是一道非常有趣的题目! 这是一道非常有趣的题目!! 这是一道非常有趣的题目!!!重要的事情先说三遍 : )好了,接下来让我们看看这道题: Say you have an array for which the i^th element is the price of a given stock on

(ÒωÓױ) 1428

LeetCode3道股票题

121. Best Time to Buy and Sell Stock 题目:买操作必须在卖操作之前,最多交易一次。得到最大利润 关键词: 动态规划, array 思路: 动态规划初始值和动态变量(不需要数组,因为只需要最大profit值,和求子数组最大max一样)。 因为只是交易一次,只要确定谁是卖价和买价。初始买价格,判断prices[i] - buy 是否比存的profit大。如果是,更新profit。判断prices[i]和buy谁大,如果新的prices[i]比存的buy小,更新buy。 解

yuki_bird的博客 247

LeetCode122:Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy on...

卷卷萌的博客 254
上一篇: 面:常见面试题整理(操作系统)
下一篇: 搬:JNI编程-- undefined reference to `__android_log_print' 的解决办法
nedwons
博客等级 码龄9年 311粉丝 572原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值