Golang | Leetcode Golang题解之第122题买卖股票的最佳时机II

题目:

题解:

Go 复制代码
func maxProfit(prices []int) (ans int) {
    for i := 1; i < len(prices); i++ {
        ans += max(0, prices[i]-prices[i-1])
    }
    return
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}
相关推荐
techdashen7 小时前
Go Map 详解:键值对实际上是如何存储的
开发语言·后端·golang
sylviiiiiia8 小时前
leetcode hot 100
python·算法·leetcode
wabs66610 小时前
关于二叉树【力扣199.二叉树的右视图的思考】
数据结构·c++·算法·leetcode·二叉树·层序遍历
a1879272183110 小时前
【算法】动态规划第三篇:背包第一课——贪心之死与正序倒序开关
算法·leetcode·动态规划·贪心·dp·暴力·算法讲解
Cccp.12310 小时前
【leetcode】(四)排序算法汇总和链表
leetcode·链表·排序算法
ttwuai11 小时前
Go 后台初始化 MySQL 脚本失败怎么办?先查 DDL 还是生成配置
开发语言·mysql·golang
名字还没想好☜11 小时前
Go 的 TCP 粘包与拆包:用长度前缀协议 + bufio 正确读消息
后端·tcp/ip·golang·go·php
a1879272183112 小时前
【算法】动态规划第五篇:区间 DP——从“看什么都像背包“到“最后戳谁“
算法·leetcode·动态规划·dp·区间·区间dp·算法讲解
rannn_11113 小时前
【力扣hot100】多维动态规划|62、64、5、1143、72
算法·leetcode·动态规划
计科杨某人13 小时前
简单算法题(基础入门题)
c++·算法·题解·入门·基础算法