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

}

相关推荐
Code_Solitude15 分钟前
c语言:二分法的完整补充
c语言·开发语言·算法
一切皆是因缘际会16 分钟前
掌控信息论:同源星际通信基础理论
人工智能·算法·ai
List<String> error_P2 小时前
freertos数据结构--链表
数据结构·链表
动词ing2 小时前
【C语言题目练习】算法 整数反转
c语言·开发语言·算法
Nil2082 小时前
leetcode 55跳跃游戏
算法·leetcode·游戏
longlongzihan3 小时前
LeetCode 128. 最长连续序列 —— 从排序到哈希集合
数据结构·c++·算法·leetcode·排序算法·哈希表
mmmmath_33 小时前
二叉树の一些知识
数据结构·算法
DongQiShanRen3 小时前
裁决台账双向互校(下):台账哈希链、三向对账与最小落地
人工智能·深度学习·算法·目标跟踪·自然语言处理·rust·哈希算法
程序员清风3 小时前
数据类型转换与日期时间处理
数据结构·pandas
SWAGGY..3 小时前
【C++进阶】:(7)红黑树的原理与 C++ 实现:结构设计、插入调整及性质验证
android·java·开发语言·c++·算法