算法训练营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

}

相关推荐
suhan44 分钟前
【数据结构】五、树:7.哈夫曼树、哈夫曼编码
数据结构·算法·排序算法
kkhimself1 小时前
POJ-3126
算法
suhan1 小时前
【数据结构】五、树:6.平衡二叉树AVL
数据结构·算法·排序算法
suhan1 小时前
【数据结构】五、树:5.二叉排序树BST
数据结构·算法·排序算法
爱读书的小胖1 小时前
80. 删除有序数组中的重复项 II
java·数据结构·算法
茉莉玫瑰花茶1 小时前
GO [ 标准库 · unsafe ]
后端·go
臻选云栗子2 小时前
PCDN跑不起来?先别甩锅给平台,问题可能出在这
算法·程序员
小画家~2 小时前
vue3 里面的 itemList.reduce
前端·数据结构·算法
tryxr2 小时前
索引概念和索引底层数据结构
数据结构·索引
All for pursuit.2 小时前
【完结】LeetCodeHot100题 详细解题过程及代码汇总
数据结构·c++·算法·leetcode