Actually it doesn't have to have a squareSum == a previous added element. This algorithm strictly reflects the process. Try some test cases and you will get it.
mo10
@mo10
Posts made by mo10

RE: Beat 90% Fast Easy Understand Java Solution with Brief Explanation

Java TreeMap O(1) add and O(n) get Solution with Explanation
The basic idea is to use two map to store intervals. One map stores the start of each interval and its length; the other map stores the end of each interval and its length. When a new value is added, check whether (val+1) or (val 1) is the start or end of one interval, if so, merge and update both maps.
public class SummaryRanges { TreeMap<Integer, Integer> fromLeft; TreeMap<Integer, Integer> fromRight; /** Initialize your data structure here. */ public SummaryRanges() { fromLeft = new TreeMap<>(); fromRight = new TreeMap<>(); } public void addNum(int val) { if (fromRight.containsKey(val)  fromLeft.containsKey(val)) { return; } int leftLen = fromLeft.getOrDefault(val + 1, 0) + 1; int rightLen = fromRight.getOrDefault(val  1, 0) + 1; fromLeft.put(val, leftLen + rightLen  1); fromRight.put(val + leftLen  1, leftLen + rightLen  1); fromRight.put(val, leftLen + rightLen  1); fromLeft.put(val  rightLen + 1, leftLen + rightLen  1); } public List<Interval> getIntervals() { List<Interval> ans = new ArrayList<>(); int limit = Integer.MIN_VALUE; for (int key : fromLeft.keySet()) { if (key > limit) { limit = key + fromLeft.get(key)  1; ans.add(new Interval(key, limit)); } } return ans; }
}

Straightforward standard very clean binary search.
The idea is simple, if you want to have x as hindex, your citation[Nx] must have at least x citations. This is simplified like finding the largest number that satisfy citation[Nx]>=x. So like Find the First Bad Version, code is simple BS:
public int hIndex(int[] c) { int N = c.length; int lo = 0, hi = N; while (lo < hi) { // only different, +1 to avoid infinity loop when mid == lo int mid = (hi + lo + 1) / 2; if (c[N  mid] >= mid) { lo = mid; } else { hi = mid  1; } } return lo; }

RE: Can someone please tell me why my solution does not work/
The way you update count is wrong. You need to update all count(index) of primes that reach the min value. Sometimes there may not be just one prime.
For example, let's say primes = [2,7], when you try and locate 14 is the min, you need to update the count of both 2 and 7. That's exactly what this piece of code in nthUglyNumber is doing:
if (mn == m2) { i++; } if (mn == m3) { j++; } if (mn == m5) { k++; }

RE: Java O(n) Solution  Bucket Sort
I think the problem is not clear about how to deal with even. Technically, all the 10 numbers equally have 2nd max frequency, I have to return them all.
Generally speaking, the fact is: top ten scores of an exam does not indicate there have to be exactly ten students.

RE: 14ms java solution
I recommend you the famous book : Cracking the coding interview. It stats that the opt point should be median

Java O(n) Solution  Bucket Sort
Idea is simple. Build a array of list to be buckets with length 1 to sort.
public List<Integer> topKFrequent(int[] nums, int k) { List<Integer>[] bucket = new List[nums.length + 1]; Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>(); for (int n : nums) { frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1); } for (int key : frequencyMap.keySet()) { int frequency = frequencyMap.get(key); if (bucket[frequency] == null) { bucket[frequency] = new ArrayList<>(); } bucket[frequency].add(key); } List<Integer> res = new ArrayList<>(); for (int pos = bucket.length  1; pos >= 0 && res.size() < k; pos) { if (bucket[pos] != null) { res.addAll(bucket[pos]); } } return res; }

RE: Revised Binary Search
If you change your while condition to lo<=hi, then you will simply need to return just 1 in the end.

RE: Short Java solution using DP O(n log n)
Great Solution!! The way you construct dp is brilliant ~