From de3468cfba5a462c0480a3e3ffb57590c9c6a5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Sun, 3 Sep 2023 10:26:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E5=91=A8=E8=B5=9B112?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/contest/y2023/BW112.java | 96 ++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/main/java/contest/y2023/BW112.java diff --git a/src/main/java/contest/y2023/BW112.java b/src/main/java/contest/y2023/BW112.java new file mode 100644 index 0000000..5e20742 --- /dev/null +++ b/src/main/java/contest/y2023/BW112.java @@ -0,0 +1,96 @@ +package contest.y2023; + +import java.util.*; + +public class BW112 { + public static void main(String[] args) { + BW112 so = new BW112(); +// String s1 = "abe"; +// String s2 = "bea"; +// so.checkStrings(s1, s2); + List nums = Arrays.asList(2, 6, 7, 3, 1, 7); + int m = 3; + int k = 4; + so.maxSum(nums, m, k); + } + + public long maxSum(List nums, int m, int k) { + long max = 0; + Set set = new HashSet<>(); + List list = new ArrayList<>(); + long sum = 0; + for (int i = 0; i < nums.size(); i++) { + sum += nums.get(i); + set.add(nums.get(i)); + list.add(nums.get(i)); + if (i < k - 1) { + continue; + } else if (i == k - 1) { + if (set.size() >= m) { + max = Math.max(max, sum); + } + } else { + list.remove(nums.get(i - k)); + if (!list.contains(nums.get(i - k))) { + set.remove(nums.get(i - k)); + } + sum -= nums.get(i - k); + if (set.size() >= m) { + max = Math.max(max, sum); + } + } + } + return max; + } + + public boolean checkStrings(String s1, String s2) { + char[] ch1 = s1.toCharArray(); + char[] ch2 = s2.toCharArray(); + int size = s1.length(); + char[] ch10 = new char[size / 2 + (size % 2)]; + char[] ch20 = new char[size / 2 + (size % 2)]; + char[] ch11 = new char[size / 2]; + char[] ch21 = new char[size / 2]; + for (int i = 0; i < size / 2 + (size % 2); i++) { + ch10[i] = ch1[i * 2]; + ch20[i] = ch2[i * 2]; + if (i < size / 2) { + ch11[i] = ch1[i * 2 + 1]; + ch21[i] = ch2[i * 2 + 1]; + } + } + Arrays.sort(ch10); + Arrays.sort(ch11); + Arrays.sort(ch20); + Arrays.sort(ch21); + for (int i = 0; i < size / 2 + (size % 2); i++) { + if (ch10[i] != ch20[i]) { + return false; + } + if (i < size / 2) { + if (ch11[i] != ch21[i]) { + return false; + } + } + } + return true; + } + + public boolean canBeEqual(String s1, String s2) { + char[] ch1 = s1.toCharArray(); + char[] ch2 = s2.toCharArray(); + for (int i = 0; i < 4; i++) { + if (ch1[i] != ch2[i]) { + if (i > 1) { + return false; + } else { + if (ch1[i + 2] != ch2[i]) { + return false; + } + ch1[i + 2] = ch1[i]; + } + } + } + return true; + } +}