Problem solution · Java

Subsequences with a Unique Middle Mode I

Subsequences with a Unique Middle Mode I: a Java solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
76 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Subsequences with a Unique Middle Mode I, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 76 lines of Java from the credited upstream file 3395-2.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 3 loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeSubsequences with a Unique Middle Mode I · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int subsequencesWithMiddleMode(int[] nums) {    int ans = 0;    Map<Integer, Integer> p = new HashMap<>(); // prefix counter    Map<Integer, Integer> s = new HashMap<>(); // suffix counter     for (final int num : nums)      s.merge(num, 1, Integer::sum);     for (int i = 0; i < nums.length; ++i) {      final int a = nums[i];      s.merge(a, -1, Integer::sum);       final int l = i;      final int r = nums.length - i - 1;       final int pa = p.getOrDefault(a, 0);      final int sa = s.get(a);       // Start with all possible subsequences with `a` as the middle number.      ans = (int) ((ans + (long) nC2(l) * nC2(r)) % MOD);       // Minus cases where frequency of 'a' is 1, so it's not a mode.      ans = (int) ((ans - (long) nC2(l - pa) * nC2(r - sa)) % MOD);       for (final int b : getUniqueNums(p, s)) {        if (b == a)          continue;         final int pb = p.getOrDefault(b, 0);        final int sb = s.get(b);         // Minus cases where the middle number is not a "unique" mode        int subtract = 0;         // [a b] a [b c]        subtract = (int) ((subtract + (long) pa * pb * sb * (r - sa - sb)) % MOD);         // [b c] a [a b]        subtract = (int) ((subtract + (long) sa * sb * pb * (l - pa - pb)) % MOD);         // [b b] a [a c]        subtract = (int) ((subtract + (long) nC2(pb) * sa * (r - sa - sb)) % MOD);         // [a c] a [b b]        subtract = (int) ((subtract + (long) nC2(sb) * pa * (l - pa - pb)) % MOD);         // [b b] a [a b]        subtract = (int) ((subtract + (long) nC2(pb) * sa * sb) % MOD);         // [a b] a [b b]        subtract = (int) ((subtract + (long) nC2(sb) * pa * pb) % MOD);         ans = (int) ((ans - subtract + MOD) % MOD);      }       p.merge(a, 1, Integer::sum);    }     return (ans + MOD) % MOD;  }   private static final int MOD = 1_000_000_007;   private int nC2(long n) {    return (int) (n * (n - 1) / 2 % MOD);  }   private Set<Integer> getUniqueNums(final Map<Integer, Integer> p, final Map<Integer, Integer> s) {    final Set<Integer> uniqueNums = new HashSet<>();    uniqueNums.addAll(p.keySet());    uniqueNums.addAll(s.keySet());    return uniqueNums;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗