首页 > 其他分享 >leetcode面试经典150题-122. 买卖股票的最佳时机 II

leetcode面试经典150题-122. 买卖股票的最佳时机 II

时间:2024-08-13 20:06:48浏览次数:10  
标签:150 int testing len II 122 prices leetcode

https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150

 

go

package leetcode150

import "testing"

func TestMaxProfit2(t *testing.T) {
    prices := []int{7, 1, 5, 3, 6, 4}
    res := maxProfit2(prices)
    println(res)
}

func maxProfit2(prices []int) int {
    if len(prices) <= 1 {
        return 0
    }
    i := 0
    total := 0
    for i+1 < len(prices) {
        for ; i < len(prices); i++ {
            if i+1 < len(prices) && prices[i] >= prices[i+1] {
                continue
            }
            break
        }
        minPrice := prices[i]
        for ; i < len(prices); i++ {
            if i+1 < len(prices) && prices[i] <= prices[i+1] {
                continue
            }
            break
        }
        maxPrice := prices[i]
        total += maxPrice - minPrice
    }
    return total
}

 

标签:150,int,testing,len,II,122,prices,leetcode
From: https://www.cnblogs.com/MoonBeautiful/p/18357604

相关文章

  • leetcode面试经典150题- 121. 买卖股票的最佳时机
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150gopackageleetcode150import("testing")funcTestMaxProfit(t*testing.T){prices:=[]int{2,1,2,0,1}......
  • 在 S7-1200/S7-1500 中,如何测量一个完整程序、子程序或特定组织块的运行时间?
    RUNTIME"指令的第一次调用用来设置测量时间的起点,并将其保存在DB变量"Memory"中来为第二次调用做参考。然后调用 "TestBlock" 程序块。当程序块被执行后,"RUNTIME" 指令第二次调用,第二次调用来计算"TestBlock"程序块的运行时间并将结果(秒)写入DB变量"runtimeResult"中......
  • Modbus-Ascii注意事项
    1:消息以冒号 : 字符开头(ASCII表示为0x3A),以回车换行对 \r\n (ASCII表示为0x0D和0x0A)结尾;所有其他字段传输的数据所允许的十六进制表示字符为的 0-9、A-F,所以除了头和尾其他数据都是10进制的表现形式。2:数据每个8位的字节被拆分为两个ASCII字符进行发送,所以收到数据后两......
  • leetcode面试经典150题- 189. 轮转数组
     https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150  gopackageleetcode150import"testing"funcTestRotate(t*testing.T){nums:=[]int{1,2}rotate2(nums,3)for_,num......
  • Oracle数据库US7ASCII字符集中文乱码
    最近遇到一家客户的Oracle数据库,版本是11g,字符集是US7ASCII,当使用PL/SQLDeveloper工具插入和查询中文时都没问题,但是Java程序使用JDBC插入和查询中文时,中文乱码。比如'a中文b'通过JDBC查询出来的乱码是这样的'aᅱ￐ᅫᅣb'查询了一些资料,看到有网友通过这种方式解决了(只列出关键代码)。......
  • 【iis】URL重写可以引用原地址内容
    URL重写可以引用原地址内容如果iis没有URL重写模块,启用或关闭windows功能里也没有,在微软下载安装https://www.iis.net/downloads/microsoft/url-rewriteiisUrl重写入站空白规则,在匹配URL中“模式”输入域名"/"后的路径Path。匹配所有Path在匹配URL的“模式”中输入(.*),匹配一部分P......
  • 代码随想录day28 || 122 买卖最佳时机2,55 跳跃游戏,45 跳跃游戏2,1005 k次取反最大数组
    122买卖股票最佳时机2funcmaxProfit(prices[]int)int{ //思路,因为支持同一天买入卖出,所以利润最大应该是所有正利润的加总结果 varsumint fori:=1;i<len(prices);i++{ ifprices[i]-prices[i-1]>0{ sum+=prices[i]-prices[i-1] } } returns......
  • leetcode递归(LCR 141. 训练计划 III)
    前言经过前期的基础训练以及部分实战练习,粗略掌握了各种题型的解题思路。现阶段开始专项练习。递归大部分题解可以使用迭代方式求解,使用递归是为了熟悉递归的解题思路。描述给定一个头节点为 head 的单链表用于记录一系列核心肌群训练编号,请将该系列训练编号 倒序 记录......
  • leetcode面试经典150-26. 删除有序数组中的重复项
    https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150 packageleetcode150import"testing"funcTestRemoveDuplicates(t*testing.T){nums:=[]int{0,0,1,1,1,......
  • 厦门国际银行一个月不到被罚1500万:屡屡违规之下,业绩也堪忧
    《港湾商业观察》施子夫 李镭近期屡屡被罚的厦门国际银行似乎走到了风口浪尖。一个月不到,厦门国际银行就因数十项违规被罚超过1500万,其合规层面的挑战可见相当严重。​一个月不到合计被罚1500万8月9日,国家金融监督管理总局泉州监管分局行政处罚信息公开表显示,厦门国际......