From a895c989cce91e5cf24d153d0e169213394972f4 Mon Sep 17 00:00:00 2001 From: gbabyX Date: Tue, 20 Jul 2021 10:17:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=BB=91?= =?UTF-8?q?=E4=B8=8D=E6=9D=A5=E5=95=8A=EF=BC=8C=E6=9C=89=E7=82=B9=E4=B8=9C?= =?UTF-8?q?=E8=A5=BF=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\347\232\204\351\242\221\346\225\260.go" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "1838.\346\234\200\351\253\230\351\242\221\345\205\203\347\264\240\347\232\204\351\242\221\346\225\260.go" diff --git "a/1838.\346\234\200\351\253\230\351\242\221\345\205\203\347\264\240\347\232\204\351\242\221\346\225\260.go" "b/1838.\346\234\200\351\253\230\351\242\221\345\205\203\347\264\240\347\232\204\351\242\221\346\225\260.go" new file mode 100644 index 0000000..17a26fb --- /dev/null +++ "b/1838.\346\234\200\351\253\230\351\242\221\345\205\203\347\264\240\347\232\204\351\242\221\346\225\260.go" @@ -0,0 +1,29 @@ +/* + * @lc app=leetcode.cn id=1838 lang=golang + * + * [1838] 最高频元素的频数 + */ + +// @lc code=start +func maxFrequency(nums []int, k int) int { + sort.Ints(nums) + + l, r, total := 0, 1, 0 + max := 1 + + for ; r < len(nums); r++ { + total += (nums[r] - nums[r-1]) * (r - l) + for total > k { + total -= nums[r] - nums[l] + l++ + } + m := r - l + 1 + if m > max { + max = m + } + } + return max +} + +// @lc code=end +