Problem solution · Java

Subsequences with a Unique Middle Mode II

Subsequences with a Unique Middle Mode II: 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
102 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 II, 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

  • 102 lines of Java from the credited upstream file 3416.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 II · JavaJava
Use this to learn the idea, then write your own version.
// Recall from solution 1 that after counting all the subsequences with `a` as// the middle mode number, we need to subtract the cases where `a` is not a// unique mode or not a mode.//// To avoid the need of looping through all numbers that are not `a`, we can// maintain the sums that are not related to `a` in the loop.//// So, during the simplification of the formula, keep the running sums of// pss, spp, pp, ss, and ps as the first item.// (for cleaner notation, abbreviate p[b] and s[b] to just p and s)////   sum(b != a) (p[a] * p * s) * (r - s[a] - s)//             + (s[a] * s * p) * (l - p[a] - p)//             + (p, 2) * s[a] * (r - s[a])//             + (s, 2) * p[a] * (l - p[a])////   sum(b != a) (p * s) * (p[a] * (r - s[a])) + (p * s^2) * (-p[a])//             + (s * p) * (s[a] * (l - p[a])) + (s * p^2) * (-s[a])//             + (p^2 - p) * (s[a] * (r - s[a]) / 2)//             + (s^2 - s) * (p[a] * (l - p[a]) / 2) class Solution {  // Same as 3395. Subsequences with a Unique Middle Mode I  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);     long pss = 0;    long spp = 0;    long pp = 0;    long ss = 0;    long ps = 0;     for (final int freq : s.values())      ss = (ss + (long) freq * freq) % MOD;     for (int i = 0; i < nums.length; ++i) {      final int a = nums[i];      long sa = s.get(a);      final long pa = p.getOrDefault(a, 0);       // Update running sums after decrementing s[a]      pss = (pss + pa * (-sa * sa + (sa - 1) * (sa - 1))) % MOD;      spp = (spp - pa * pa) % MOD; // (-sa + (sa - 1)) * pa * pa      ss = (ss - sa * sa + (sa - 1) * (sa - 1)) % MOD;      ps = (ps - pa) % MOD; // -pa * (-sa + (sa - 1))       s.merge(a, -1, Integer::sum);      sa = s.get(a);       final int l = i;      final int r = nums.length - i - 1;       // 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);       // Minus the values where `b != a`      final long pss_ = (pss - pa * sa * sa) % MOD;      final long spp_ = (spp - sa * pa * pa) % MOD;      final long pp_ = (pp - pa * pa) % MOD;      final long ss_ = (ss - sa * sa) % MOD;      final long ps_ = (ps - pa * sa) % MOD;      final long p_ = l - pa;      final long s_ = r - sa;       // Minus cases where `a` is not a "unique" mode or not a mode      long subtract = 0;      subtract = (subtract + ps_ * (pa * (r - sa))) % MOD;      subtract = (subtract + pss_ * (-pa)) % MOD;      subtract = (subtract + ps_ * (sa * (l - pa))) % MOD;      subtract = (subtract + spp_ * (-sa)) % MOD;      subtract = (subtract + (pp_ - p_) * sa * (r - sa) / 2) % MOD;      subtract = (subtract + (ss_ - s_) * pa * (l - pa) / 2) % MOD;      ans = (int) ((ans - subtract + MOD) % MOD);       // Update running sums after incrementing p[a]      pss = (pss + sa * sa) % MOD; // (-pa + (pa + 1)) * sa * sa      spp = (spp + sa * (-pa * pa + (pa + 1) * (pa + 1))) % MOD;      pp = (pp - pa * pa + (pa + 1) * (pa + 1)) % MOD;      ps = (ps + sa) % MOD; // (-pa + (pa + 1)) * sa       p.merge(a, 1, Integer::sum);    }     return (int) ((ans + MOD) % MOD);  }   private static final int MOD = 1_000_000_007;   // Returns C(n, 2)  private long nC2(long n) {    return n * (n - 1) / 2 % MOD;  }} 

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 ↗