[NO.1 Variable value after operation]

Their thinking

Sign in.

The code shown

class Solution { public int finalValueAfterOperations(String[] operations) { int v = 0; for (String op : operations) { if (op.contains("++")) { v++; } else { v--; } } return v; }}Copy the code

NO.2 array beauty sum

Their thinking

The beauty value of the intermediate element can be obtained from the maximum prefix value and the minimum suffix value, so the array of the maximum prefix value and the minimum suffix value can be preprocessed.

The code shown

class Solution { public int sumOfBeauties(int[] nums) { int[] preMax = new int[nums.length]; preMax[0] = nums[0]; for (int i = 1; i < nums.length; i++) { preMax[i] = Math.max(preMax[i - 1], nums[i]); } int[] sufMin = new int[nums.length]; sufMin[nums.length - 1] = nums[nums.length - 1]; for (int i = nums.length - 2; i >= 0; i--) { sufMin[i] = Math.min(sufMin[i + 1], nums[i]); } int res = 0; for (int i = 1; i < nums.length - 1; ++i) { if (preMax[i - 1] < nums[i] && nums[i] < sufMin[i + 1]) { res += 2; } else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) { res += 1; } } return res; }}Copy the code

【 No.3 test square 】

Their thinking

Use Map to store all vertices and then enumerate diagonals during count queries.

The code shown

class DetectSquares {

   Map<Integer, Integer> count;

   public DetectSquares() {
       count = new HashMap<>();
  }

   public void add(int[] point) {
       int c = comp(point[0], point[1]);
       count.put(c, count.getOrDefault(c, 0) + 1);
  }

   public int count(int[] point) {
       int res = 0;
       for (var kv : count.entrySet()) {
           int x = X(kv.getKey());
           int y = Y(kv.getKey());
           if (Math.abs(x - point[0]) == Math.abs(y - point[1]) && x != point[0]) {
               res += kv.getValue() *
                       count.getOrDefault(comp(x, point[1]), 0) *
                       count.getOrDefault(comp(point[0], y), 0);
          }
      }
       return res;
  }

   private int comp(int x, int y) {
       return x * 10000 + y;
  }

   private int X(int c) {
       return c / 10000;
  }

   private int Y(int c) {
       return c % 10000;
  }
}
Copy the code

NO.4 Sequence of the oldest child repeated K times

Their thinking

Note that 2 <= n < k * 8, and if a subsequence is to be repeated k times, then every character in the subsequence must occur at least K times, so the answer must be less than or equal to 7.

We first find all the characters that occur at least k times, and then enumerate the permutations and combinations of these characters to determine in turn whether each permutation and combination occurs k times.

The code shown

class Solution { public String longestSubsequenceRepeatedK(String s, int k) { Map<Character, Integer> count = new HashMap<>(); for (char c : s.toCharArray()) { count.put(c, count.getOrDefault(c, 0) + 1); } StringBuilder s2 = new StringBuilder(); for (char c : s.toCharArray()) { if (count.get(c) >= k) { s2.append(c); } } count.clear(); for (char c : s2.toString().toCharArray()) { count.put(c, count.getOrDefault(c, 0) + 1); } return solve(new StringBuilder(), count, s2.toString().toCharArray(), k); } private String solve(StringBuilder cur, Map<Character, Integer> count, char[] s, int k) { String res = ""; var keys = new HashSet<Character>(count.keySet()); for (var c : keys) { cur.append(c); if (comp(cur.toString(), res)) { int cnt = 0, idx = 0; for (char cc : s) { if (cc == cur.charAt(idx) && ++idx == cur.length()) { idx = 0; if (++cnt == k) { res = cur.toString(); break; } } } } int bak = count.get(c); if (bak - k < k) { count.remove(c); } else { count.put(c, bak - k); } String r = solve(cur, count, s, k); if (comp(r, res)) { res = r; } cur.deleteCharAt(cur.length() - 1); count.put(c, bak); } return res; } private boolean comp(String a, String b) { return a.length() > b.length() || (a.length() == b.length() && a.compareTo(b) > 0); }}Copy the code