I am wondering this too. Why adding 7 makes 7~13 covered? How to prove it for all general case? Especially during interview.
guschen802
@guschen802
Posts made by guschen802

RE: Why does a greedy solution work for this problem?

RE: Share my simple Java code
Amazing solution! Code is easy to understand, adding the missing number.
but I think the key is to explain and prove this works..not just passing the OJ ,but come up a math explanation during the interview. 
RE: Share my solution with Java 8 Lambdas
Using Lambdas is slow. like over 100ms for this question..Is this JDK's problem or OJ's problem?

RE: Easy Java Solution using Priority Queue and Comparator (18ms)
You can just use the normal order. and keep the queue's size under k by polling out the min when exceed, then the front of the queue will always be the kth largest element;
And just for short code, your comparator can use Collections.reverseOrder() instead.
public class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> minHeap = new PriorityQueue(k+1); for (int i : nums) { minHeap.offer(i); if (minHeap.size() > k) minHeap.poll(); } return minHeap.poll(); } }

RE: Why I get wrong answer (I am so confused!)
The answer required that each column be in top down order. You can fix the code by changing DFS to BFS.

RE: Simple to understand solution with Lists
The code has a problem where every column cannot follow uptodown order, like [5,1,6,null,3,null,null,2,4].
I think this can be fixed by using BFS. Here is the code fixed base on this post.class NodeWrapper{ TreeNode node; int column; NodeWrapper(TreeNode n, int c) { node = n; column = c; } } public class Solution { public List<List<Integer>> verticalOrder(TreeNode root) { if (root == null) return new ArrayList<>(); List<List<Integer>> positive = new ArrayList<>(); List<List<Integer>> negative = new ArrayList<>(); negative.add(new ArrayList<>());//dummy for 0 which already in positive Queue<NodeWrapper> queue = new LinkedList<>(); queue.add(new NodeWrapper(root, 0)); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0 ; i < size; i++) { NodeWrapper cur = queue.poll(); List<List<Integer>> list = cur.column >= 0? positive:negative; int pos = Math.abs(cur.column); List<Integer> column; if (pos >= list.size()) { column = new ArrayList<>(); list.add(column); } else { column = list.get(pos); } column.add(cur.node.val); if (cur.node.left != null) queue.add(new NodeWrapper(cur.node.left, cur.column  1)); if (cur.node.right != null) queue.add(new NodeWrapper(cur.node.right, cur.column + 1)); } } Collections.reverse(negative); negative.remove(negative.size()1); negative.addAll(positive); return negative; } }

RE: My 6ms Java solution beating 85%, with explanation
Great solution!, the idea is no track last occurrence index, there for no need of two pointers.

RE: Help,why TLE(java)?how to opt?
Thanks! Did not think of that either.

RE: Simple java solution with explanation
Is this really necessary? since it use "return isUgly(num/2);", the recursive will only go 233 for 18, and then finish, it wont go like 323 or 332, am I right?