算法训练营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 分钟前
2026华为OD面试题020:日志文件异常检测
算法·leetcode·华为od
炸薯条!6 分钟前
从零开始学C++(4) --类和对象
开发语言·c++·算法
haluhalu.14 分钟前
prompts.chat:07-few-shot-prompting
人工智能·算法
Jerry23 分钟前
LeetCode 150. 逆波兰表达式求值
算法
浩瀚地学29 分钟前
【面试算法笔记】0106-数组-区间和
笔记·算法·面试
Jerry6 小时前
LeetCode 1047. 删除字符串中的所有相邻重复项
算法
可编程芯片开发8 小时前
基于霍尔传感器和PID控制器的有功功率检测控制系统simulink建模与仿真
算法
To_OC9 小时前
LC 17 电话号码的字母组合:我的回溯算法,就是从这道题开窍的
javascript·算法·leetcode
战族狼魂11 小时前
GPT-5.6与Grok 4.5重磅发布
人工智能·算法·大模型·大语言模型
白日焰火112 小时前
基于 OpenSpec 实现规范驱动开发
算法·交互