upvote for fun lol
Vordin
@Vordin
Posts made by Vordin

RE: Another explanation and solution
@XuhuiWang No, all the pigs are testing simultaneously，once a pig dies it actually help to locate one dimension of the matrix and it won't be used in further test. Try do run the algorithm in a small scenario like 2 pigs, 2 tests with 8 buckets and you will find it works.

Java Weighted UnionFind with Compression Path
Same idea of using unionfind. Only add one more sz[] array to record the size of each subtree. When merge two tree we can use it to pick a smaller size tree to merge into the big one, which help to reduce the time cost for finding the root.
public class Solution { /** * @param n an integer * @param edges a list of undirected edges * @return true if it's a valid tree, or false */ public boolean validTree(int n, int[][] edges) { // Write your code here int[] graph=new int[n]; int[] sz=new int[n]; for(int i=0;i<n;i++){ graph[i]=i; sz[i]=1; } for(int[] tuple:edges){ int x=findroot(tuple[0],graph); int y=findroot(tuple[1],graph); if(x==y) return false; if(sz[x]<sz[y]){ graph[x]=y; sz[y]+=sz[x]; } else{ graph[y]=x; sz[x]+=sz[y]; } } return edges.length==n1; } private int findroot(int i, int[] graph) { // TODO Autogenerated method stub while(i!=graph[i]){ graph[i]=graph[graph[i]]; i=graph[i]; } return i; } }

question about java.lang.NullPointerException
Hi all,
When I run my code on the OJ it points out that for the following code I made a null pointer exception.int res=head.val;
but isn't the input are guaranteed that the head is not null?
Can anybody figure out what is happening? I am not familiar with java and appreciate for any help.
Here attaches all the code.ListNode head; Random random; /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */ public Solution(ListNode head) { this.head = head; random = new Random(); } /** Returns a random node's value. */ public int getRandom() { int res=head.val; for(int count=1;head!=null;count++){ if(random.nextInt(count)==0) res=head.val; head=head.next; } return res; }

RE: Java clear O(n logn) solution based on TreeMap
@ivtoskov
@xietao0221
Correct me if I am wrong.
Given that, you are using a treemap, which derived from Map. if you put the same key with a different value it will replace the original one with the newer one. In such case, if you have case like
[[4,6],[1,2],[4,8]]
the correct answer should be
[1,0,1]However, the answer you provide will give
[1,2,1]Because in the treeMap the [4,8] will store as 4>2 ,which replace the
previously inserted [4,6] stored as 4>0 for the Map property.Anyway, you guys show me how to use treemap, still be a brilliant solution.
Updated:
Here attached my edited code which can fix this problem while passing all the test case.public int[] findRightInterval(Interval[] intervals) { TreeMap<Integer, PriorityQueue<Integer>> map = new TreeMap<>(); int[] res = new int[intervals.length]; for(int i=0;i<intervals.length;i++){ if(map.get(intervals[i].start)==null){ PriorityQueue<Integer> pq=new PriorityQueue<>(); pq.add(i); map.put(intervals[i].start, pq); } else{ map.get(intervals[i].start).add(i); } } for(int i=0;i<intervals.length;i++) { Integer key = map.ceilingKey(intervals[i].end); res[i] = key!=null ?map.get(key).peek() : 1; } return res; }

RE: Math solusion based on Euler's theorem, power called only ONCE, C++/Java/1linePython
though my undergraduate majored in mathematics and learned abstract algebra it still took me 3 hours to understand how it works. Genius.
(zhong wen ban: gei da shen ni gui xia le ....) 
RE: Step by step tackling of the problem
really awesome and easy to understand