算法训练营day28(补), 贪心算法2

//122. 买卖股票的最佳时机 II

func maxProfit(prices \[\]int) int {

result := 0 //利润总和

for i := 1; i < len(prices); i++ {

if pricesi-pricesi-1 > 0 {

result = result + (pricesi - pricesi-1)

}

}

return result

}

//55. 跳跃游戏

func canJump(nums \[\]int) bool {

step := 0 //步数

for i := 0; i <= step; i++ {

if i+numsi > step {

step = i + numsi

}

if step >= len(nums)-1 {

return true

}

}

return false

}

//45. 跳跃游戏 II

func jump(nums \[\]int) int {

cur := 0 //当前指针

next := 0 //下一个指针

result := 0 //步骤统计

for i := 0; i < len(nums); i++ {

if i+numsi > next {

next = i + numsi

}

if i == cur && cur != len(nums)-1 {

result++

cur = next

if cur >= len(nums)-1 {

return result

}

} else if i == cur {

return result

}

}

return result

}

相关推荐
一只齐刘海的猫4 小时前
【Leetcode】找到字符串中所有字母异位词
算法·leetcode·职场和发展
海清河晏1114 小时前
数据结构 | 八大排序
数据结构·算法·排序算法
Code_Artist4 小时前
🦜用 GoAI 从零打造一个 AI Agent 脚手架工程:重新定义智能体开发范式!
go·agent·ai编程
liulilittle5 小时前
固定数组时间轮的槽过载优化:桶链表与批次执行
网络·数据结构·链表
IronMurphy5 小时前
【算法五十七】146. LRU 缓存
算法·缓存
Irissgwe6 小时前
数据结构-栈和队列
数据结构·c++·c·栈和队列
两片空白6 小时前
数据容器集合set/frozenset
数据结构
凌波粒6 小时前
LeetCode--108.将有序数组转换为二叉搜索树(二叉树)
算法·leetcode·职场和发展
liulilittle6 小时前
KCC:在 BBR 思路上的一次探索
网络·tcp/ip·算法·bbr·通信·拥塞控制·kcc