码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode算法题---第3天


    注:大佬解答来自LeetCode官方题解

    121.买卖股票的最佳时期

    1.题目

    2.个人解答

    1. function maxProfit(prices) {
    2. //更新最低价格和最大利润
    3. let minPrice = prices[0];
    4. let maxProfit = 0;
    5. for (let i = 1; i < prices.length; i++) {
    6. // 如果当前价格比最低价格还低,更新最低价格
    7. if (prices[i] < minPrice) {
    8. minPrice = prices[i];
    9. }
    10. // 计算当前价格卖出时的利润,并更新最大利润
    11. else if (prices[i] - minPrice > maxProfit) {
    12. maxProfit = prices[i] - minPrice;
    13. }
    14. }
    15. return maxProfit;
    16. }

     3.大佬解答

    122.买卖股票最佳时期Ⅱ

    1.题目

    2.个人解答

    1. var maxProfit = function (prices) {
    2. //更新最低价格和最大利润
    3. let minPrice = prices[0];
    4. let maxProfit = 0;
    5. for (let index = 1; index < prices.length; index++) {
    6. if (prices[index] < minPrice) {
    7. minPrice = prices[index];
    8. } else {
    9. maxProfit += prices[index]-minPrice;
    10. minPrice=prices[index]
    11. }
    12. }
    13. return maxProfit
    14. };

     3.大佬解答

    1. var maxProfit = function(prices) {
    2. const n = prices.length;
    3. let dp0 = 0, dp1 = -prices[0];
    4. for (let i = 1; i < n; ++i) {
    5. let newDp0 = Math.max(dp0, dp1 + prices[i]);
    6. let newDp1 = Math.max(dp1, dp0 - prices[i]);
    7. dp0 = newDp0;
    8. dp1 = newDp1;
    9. }
    10. return dp0;
    11. };

     

     

    1. var maxProfit = function(prices) {
    2. let ans = 0;
    3. let n = prices.length;
    4. for (let i = 1; i < n; ++i) {
    5. ans += Math.max(0, prices[i] - prices[i - 1]);
    6. }
    7. return ans;
    8. };

     55.跳跃游戏

    1.题目

    2.个人解答

    1. function canJump(nums) {
    2. let maxReach = 0;
    3. for (let i = 0; i < nums.length; i++) {
    4. if (i > maxReach) {
    5. return false; // 如果当前位置无法到达,则返回false
    6. }
    7. maxReach = Math.max(maxReach, i + nums[i]); // 更新maxReach
    8. }
    9. return maxReach >= nums.length - 1;
    10. }

     3.大佬解答

     

  • 相关阅读:
    Vue表单修饰符:v-model.lazy、v-model.number、v-model.trim
    基于PHP+MySQL的服装购物商城系统#毕业设计
    改变Next.js默认端口的方法
    v87.01 鸿蒙内核源码分析 (内核启动篇) | 从汇编到 main () | 百篇博客分析 OpenHarmony 源码
    使用DNS查询Web服务器IP地址
    为什么emplace_back比push_back更快?快是有条件的
    java毕业设计房屋租赁网站Mybatis+系统+数据库+调试部署
    【入门-05】存储空间
    派克Parker直线电机助力锂电池顶盖焊接及叠片设备高效生产
    清理Maven仓库中下载失败的文件
  • 原文地址:https://blog.csdn.net/m0_71469120/article/details/133387759
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号