首页 > 其他分享 >leetcode-643. 子数组最大平均数 I

leetcode-643. 子数组最大平均数 I

时间:2023-01-03 23:34:46浏览次数:70  
标签:maxAvg begin end nums 平均数 643 leetcode

643. 子数组最大平均数 I - 力扣(Leetcode)

滑动窗口,判断好边界条件即可

func findMaxAverage(nums []int, k int) float64 {
    begin, end := 0, k-1
    if end >= len(nums) {
        end = len(nums) - 1
    }

    curSum := 0
    for i := begin; i <= end; i++ {
        curSum += nums[i]
    }

    maxAvg := float64(curSum) / float64(end - begin + 1)

    end++
    for ; end < len(nums); end++ {
        // fmt.Println(begin, end)
        curSum += nums[end]
        curSum -= nums[begin]

        avg := float64(curSum) / float64(end - begin)
        if avg > maxAvg {
            maxAvg = avg
        }

        begin++
    }

    return maxAvg
}

标签:maxAvg,begin,end,nums,平均数,643,leetcode
From: https://www.cnblogs.com/wudanyang/p/17023697.html

相关文章

  • leetcode-168-easy
    ExcelSheetColumnTitleGivenanintegercolumnNumber,returnitscorrespondingcolumntitleasitappearsinanExcelsheet.Forexample:A->1B->2C-......
  • leetcode-206-easy
    ReverseLinkedListGiventheheadofasinglylinkedlist,reversethelist,andreturnthereversedlist.Example1:Input:head=[1,2,3,4,5]Output:[5,......
  • leetcode-557-easy
    ReverseWordsinaStringIIIGivenastrings,reversetheorderofcharactersineachwordwithinasentencewhilestillpreservingwhitespaceandinitialwo......
  • leetcode-627-easy
    IslandPerimeterYouaregivenrowxcolgridrepresentingamapwheregrid[i][j]=1representslandandgrid[i][j]=0representswater.Gridcellsareconn......
  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • leetcode-441-easy
    ArrangingCoinsYouhavencoinsandyouwanttobuildastaircasewiththesecoins.Thestaircaseconsistsofkrowswheretheithrowhasexactlyicoins.Th......
  • leetcode-459-easy
    RepeatedSubstringPatternGivenastrings,checkifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether......
  • leetcode-492-easy
    ConstructtheRectangleAwebdeveloperneedstoknowhowtodesignawebpage'ssize.So,givenaspecificrectangularwebpage’sarea,yourjobbynowisto......
  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......