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

}

相关推荐
w_zero_one3 分钟前
链表(2)
java·数据结构·链表
学术小李17 分钟前
cudaMalloc第一个参数为何是指针的指针
算法
kukubuzai20 分钟前
二分查找系列一
数据结构·算法·leetcode
老歌老听老掉牙24 分钟前
动态规划:从递归到最优决策的算法艺术
算法·动态规划
朔北之忘 Clancy27 分钟前
2024 CSP-J 第二轮真题解析
c++·算法·青少年编程·noip·csp·信奥赛·noi
小七在进步1 小时前
类和对象(五)
java·开发语言·算法
wzdark1 小时前
算法与数据结构教学的可视化新模式探索4
数据结构
就叫年华吧丶1 小时前
长文档点目录定位总是不准?一个 content-visibility + 平滑滚动引发的连环坑(Vue3 实战)
前端·javascript·算法·vue
FPGA信号处理1 小时前
【信号检测与估计】第四节课:非高斯噪声下的 BLUE、极大似然与 EM 算法
人工智能·算法·机器学习