From 845d582944dd44bff40a2d17cfdb63200b5c751a 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, 4 Sep 2022 11:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=A8=E8=B5=9B309?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/contest/y2022/m9/Week309.java | 87 +++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/main/java/contest/y2022/m9/Week309.java diff --git a/src/main/java/contest/y2022/m9/Week309.java b/src/main/java/contest/y2022/m9/Week309.java new file mode 100644 index 0000000..6fc1eac --- /dev/null +++ b/src/main/java/contest/y2022/m9/Week309.java @@ -0,0 +1,87 @@ +package contest.y2022.m9; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * @author hyy + * @Classname Week309 + * @Description TODO + * @Date 2022/9/4 10:28 + */ +public class Week309 { + public static void main(String[] args) { + Week309 solution = new Week309(); + solution.numberOfWays(989, 1000, 99); + } + + public int longestNiceSubarray(int[] nums) { + int left = 0; + int num = 0; + int res = 1; + for (int i = 0; i < nums.length; i++) { + while (left < i && (num & nums[i]) > 0) { + num -= nums[left]; + left += 1; + } + num |= nums[i]; + res = Math.max(res, i - left + 1); + } + return res; + } + + public int numberOfWays(int startPos, int endPos, int k) { + int num = k - Math.abs(endPos - startPos); + if (num < 0 || num % 2 == 1) { + return 0; + } + if (num == 0) { + return 1; + } + long ret = 0; + if (map.containsKey(endPos - 1)) { + Map tmp = map.get(endPos - 1); + if (tmp.containsKey(k - 1)) { + ret += tmp.get(k - 1); + } else { + ret += numberOfWays(startPos, endPos - 1, k - 1); + } + } else { + ret += numberOfWays(startPos, endPos - 1, k - 1); + } + if (map.containsKey(endPos + 1)) { + Map tmp = map.get(endPos + 1); + if (tmp.containsKey(k - 1)) { + ret += tmp.get(k - 1); + } else { + ret += numberOfWays(startPos, endPos + 1, k - 1); + } + } else { + ret += numberOfWays(startPos, endPos + 1, k - 1); + } + Map tmp = map.getOrDefault(endPos, new HashMap<>()); + tmp.put(k, (int) ret); + map.put(endPos, tmp); + return map.get(endPos).get(k); + } + + Map> map = new HashMap<>(); + + public boolean checkDistances(String s, int[] distance) { + int[] arr = new int[26]; + Arrays.fill(arr, -1); + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + int index = ch - 'a'; + if (arr[index] > -1) { + if (distance[index] != i - arr[index] - 1) { + return false; + } + } else { + arr[index] = i; + } + } + return true; + } +}