funcbeautifulBouquet(flowers []int, cnt int)int { h := map[int]int{} left := 0 ans := 0 for right,x := range flowers{ h[x]++ for h[x] > cnt{ y := flowers[left] h[y]-- if h[y] == 0{ delete(h,y) } left++ } ans = (ans + (right - left + 1) ) % MOD } return ans }
funcmax(a,b int)int{ if a > b{ return a } return b }
functotalFruit(fruits []int)int { h := map[int]int{} left := 0 ans := 0 for right,x := range fruits{ h[x]++ forlen(h) > 2{ y := fruits[left] h[y]-- if h[y] == 0{ delete(h,y) } left++ } ans = max(ans,right - left + 1) } return ans }
funcmax(a,b int)int{ if a > b { return a } return b }
funcmin(a,b int)int{ if a < b{ return a } return b }
funccountSubarrays(nums []int, minK int, maxK int)int64 { i0,left,right := -1,-1,-1 var ans int64 for i,v := range nums{ if v == minK{ left = i } if v == maxK{ right = i }
if v < minK || v > maxK{ i0 = i } ans += int64( max(min(left,right) - i0,0)) } return ans }