From faeab03adb2dfb9f1375f12fadc1b48fb4de72f8 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Sun, 27 Oct 2024 22:58:18 +0800 Subject: [PATCH] =?UTF-8?q?2024/10/27=20=E7=81=B5=E8=8C=B6=E9=A2=98?= =?UTF-8?q?=E5=8D=95=20=E4=B8=8D=E5=AE=9A=E9=95=BF=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sliding_windows/Num1838.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/com/cool/ling_cha_mount/sliding_windows/Num1838.java diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num1838.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num1838.java new file mode 100644 index 0000000..9ee47cf --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num1838.java @@ -0,0 +1,42 @@ +package com.cool.ling_cha_mount.sliding_windows; + +import java.util.Arrays; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/10/27/22:48 + * @Description: + */ +public class Num1838 { + public int maxFrequency(int[] nums, int k) { + Arrays.sort(nums); + long l= k; + long left = 0; + long res = 1; + for (int i = 1; i < nums.length; i++) { + l -=(nums[i] - nums[i - 1]) *(i - left); + while (l < 0) { + l += nums[i] - nums[(int)left++]; + } + res = Math.max(res, i - left + 1); + } + return (int)res; + } + + public int maxFrequency1(int[] nums, int k) { + Arrays.sort(nums); + int left = 0, res = 1; + long total = 0; + for (int i = 0; i < nums.length; i++) { + total += nums[i]; + while ((long) nums[i] * (i - left + 1) - total > k) { + total -= nums[left]; + left++; + } + res = Math.max(res, i - left + 1); + } + return res; + } +}