123. Best Time to Buy and Sell Stock III(同小米风口的猪)

易灵思FPGA-软件Efinity和Modelsim联仿设置 文章目录前言一、modelsim破解过程二、使用步骤1.引入库2.读入数据总结欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智 阅读详情
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 at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

这道题实际上就是最大差值问题。之一更新最小值或最大值,以及当前最大差值即可。

这道题由于是两次购入,所有分别从左右两端求得然后相加即可。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        const int size = prices.size();

        if(size == 0)
            return res;

        vector<int> vec(size, 0);

        //从左往右计算vec[i](vec1[i])表示,第1..i+1天的最大收益,因为数组下标从零开始
        int min = prices[0];
        for(int i=1; i<size; ++i){
            vec[i] = vec[i-1];   //如果收益没有之前的多,至少要保持
            if(prices[i] - min > vec[i])  //以当天为界限,计算之前的最大收益,所以是减
                vec[i] = prices[i] - min;
            if(prices[i] < min)
                min = prices[i];
        }

        //从右往左计算vec2[i]表示,第i+1天到最后一天的最大收益
        //这里由于采用vec2[i]空间复杂度为O(2n),所以利用profit变量省去vec2[i],直接在vec[i]上相加即可
        int max = prices[size-1], profit = 0;
        for(int i=size-2; i>=0; --i){
            if(max - prices[i] > profit)  //以当天为界,未来的最大收益,则是更远的天数减去更近的天数
                profit = max - prices[i];
            vec[i] += profit;  //在这里直接+=,直接将之前算的某天以前的最大收益和目前某天以后最大收益相加,节省空间复杂度
            if(prices[i] > max)
                max = prices[i];
        }

        for(int i=0; i<size; ++i)  //找出某一天满足最大收益最大
            res = std::max(res, vec[i]);

        return res;
    }
};
Vivado FIR Complier_IP核使用方法 文章依靠该IP核对应手册PG149写的,因为笔者需求简单,没有深入挖掘功能,可以自行下载学习。 阅读详情

相关推荐

Python实战:3步搞定DICOM转JPG/PNG(附批量处理脚本)

本文详细介绍了如何使用Python在3步内将DICOM医学影像转换为JPG/PNG格式,并提供了批量处理脚本。通过Pydicom、Pillow和NumPy库的组合,解决了像素值处理、批量转换效率和医学图像特性保留等关键问题,适合医疗AI开发者和放射科医生快速集成到工作流中。

numpy6sculptor的博客 594

Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。 设计一个算法来计算最大收益。你至多可以完成两次交易

(ง •̀_•́)ง 6541

分离与合体题解(区间DP)

题目: 经过在机房里数日的切磁,LYD从社神牛那里学会了分离与合体,出关前,杜神牛给了他一个测试 杜神牛造了个区域,它们紧邻着排成了一行,编号1-n。在这經个区域里都放着一把OI界 的金钥匙,每一把都有一定的价值,LYD当然想得到它们了。然而杜神牛规定LYD不可以一下子 把它们全部拿走,而是每次只可以拿一把。为了尽快地拿到所有的金钥匙,LYD自然就用上了刚学的分离与合体特技。 开始LYD可以选择从1~n-11~n-11~n-1的任何一个区域(记为K)进入,进入后LYD会在K区域发 生分离,从而分离为两个小L

cqbz_lipengcheng的博客 372

98. Validate Binary Search Tree(判断合法二叉搜索树)

Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key. The right

FreeeLinux's blog 1707

手把手教你用EG2153搭建开关电源:SOP-8封装驱动芯片的5个设计技巧

本文详细介绍了使用EG2153自振荡半桥驱动芯片搭建开关电源的五个核心设计技巧。内容涵盖从深入理解芯片工作机制、频率编程计算误区、SOP-8封装的热管理方案,到死区时间评估与外部保护电路设计,以及实战调试与故障排查指南,旨在帮助工程师实现稳定可靠的小功率电源设计。

weixin_29200485的博客 380

CocosCreator | 自定义启动页之H5

01 效果对比 默认启动页: 自定义启动页: 02 启动页存在的意义 我们在 creator 中的开发的游戏都是基于 cocos engine,所以在展示我们的游戏之前,是必须要先加载 cocos engine,然后才是我们自己的业务代码 流程图如下: 而构建完成后未精简的 cocos engine 代码为 1.9M 如果再加上物理相关代码的话,一共是2M 别忘了,我们自己的首场景也要加载过程哦 所以,从游戏启动,到真...

Valiancer 5900

532. K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in

FreeeLinux's blog 1191

ROSTCM6软件下载及语义网络分析详细操作教程(附网盘链接)

ROSTCM6是武汉大学沈阳教授研发编码的国内目前唯一的以辅助人文社会科学研究的大型免费社会计算平台。该软件可以实现微博分析、聊天分析、全网分析、网站分析、浏览分析、分词、词频统计、英文词频统计、流量分析、聚类分析等一系列文本分析。

m0_64336780的博客 4万+

257. Binary Tree Paths(打印二叉树所有路径)

Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are:["1->2->5", "1->3"]首先我采用了dfs,但是我的方法有点麻烦。方法一:dfs/**

FreeeLinux's blog 883

【第2章>第10节】深度学习DenseNet模型分析与matlab仿真

本文介绍了基于DenseNet深度学习模型的图像分类MATLAB实现方法。主要内容包括:1. DenseNet模型原理,详细解析了初始卷积层、密集块、过渡层等核心结构;2. 在MATLAB环境下完成DenseNet工具箱安装和测试样本准备;3. 实现图像预处理(统一缩放为224×224)、模型训练和测试的完整流程;4. 提供了混淆矩阵和准确率等测试结果分析。文章附带完整的MATLAB程序代码和操作视频教程,适合深度学习初学者快速掌握DenseNet模型的图像分类应用。通过实际测试,验证了该方案的有效性。

FPGA/MATLAB学习教程/源码/项目合作开发 330

【k8s故障处理篇】calico-node启动失败“Felix is not live: Get “http://localhost:9099/liveness” 解决办法(V1.30.3版本)

【k8s故障处理篇】calico-node启动失败“Felix is not live: Get “http://localhost:9099/liveness” 解决办法(V1.30.3版本)

jks212454的博客 1091

459. Repeated Substring Pattern

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Englis

FreeeLinux's blog 754

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...

214. Shortest Palindrome(KMP)

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For exam

FreeeLinux's blog 703

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

200. Number of Islands+130. Surrounded Regions(并查集/DFS)

首先看200题:Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may

FreeeLinux's blog 668

87. Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr

FreeeLinux's blog 623

92. Reverse Linked List II(链表局部逆序**)

Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.此问题看似简单,实际很麻烦,如果不使用二级指针,那么如果链表仅有两项,并且要求m=1,

FreeeLinux's blog 596

3. Longest Substring Without Repeating Characters [DP]

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "

FreeeLinux's blog 585

485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.

FreeeLinux's blog 580

148. Sort List(快排、归并)

Sort a linked list in O(n log n) time using constant space complexity.首先给出快排解法:class Solution { public: ListNode *sortList(ListNode *head) { if(head == NULL) return NULL;

FreeeLinux's blog 562

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not

FreeeLinux's blog 560

41. First Missing Positive(找到数组中未出现的最小正整数)

Total Accepted: 87074 Total Submissions: 348588 Difficulty: Hard Contributors: Admin Given an unsorted integer array, find the first missing positive integer. For example, Given [1,

FreeeLinux's blog 550

11. Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two l

FreeeLinux's blog 537

349. Intersection of Two Arrays/350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: Each element in the result must be unique. The result can be in an

FreeeLinux's blog 522

【算法拾光】队列|在每个树⾏中找最⼤值 + 最后⼀块⽯头的重量

本文总结两道经典算法题:一是通过层序遍历(BFS)逐层找最大值,利用队列分层处理,每层维护最大值;二是用大根堆(优先队列)模拟石头碰撞过程,每次取最大两块石头,差值重新入堆,直至剩余一块或无石。前者适用于树的层次遍历,后者适合动态获取最值场景。核心思想:队列用于分层遍历,堆用于高效取极值。

wuqingyigexiaoba的博客 202

把 KV Cache 压到 FP4:BitDecoding-FP4 的算法-系统协同设计复盘

长上下文推理卡在KV Cache的显存与带宽上,而现有FP4方案要么精度崩、要么速度废。这篇文章复盘BitDecoding-FP4如何用三个算法补丁加一个系统重构,在B300上把服务吞吐提升2.4–3.0倍,精度几乎不掉。

helloagent的博客 319

【算法拾光】链表 哈希表|K个一组翻转链表 + 两数之和

K个一组翻转链表需分段反转,借助哑节点与前驱指针维护连接,避免断链;两数之和利用哈希表实现O(1)查找,空间换时间,高效定位目标组合。前者考察链表指针操作,后者聚焦映射关系构建,均为面试高频题。

wuqingyigexiaoba的博客 341

排序算法深度笔记(下):非比较类排序的线性突破与工程边界

排序算法时间复杂度额外空间复杂度稳定性是否基于比较选择排序O(N^2)(O(1))不稳定是冒泡排序O(N^2)(O(1))稳定是插入排序O(N^2)(O(1))稳定是归并排序O(N\log N)(O(N))稳定是快速排序O(N\log N)O(log N)不稳定是堆排序O(N\log N)O(1)不稳定是计数排序(O(N + M))O(M))稳定否基数排序O(N)稳定否桶排序平均O(N) / 最坏O(N^2)

2401_87069538的博客 617
上一篇: 网络编程定时器一:使用升序链表
下一篇: 网络编程定时器二:使用时间轮
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值