The requirement says "Note: Do not use class member/global/static variables to store states."
So you may pass the variable as function argument.
lfzh123
@lfzh123
Posts made by lfzh123

RE: 16 lines in Java, pretty simple solution in PreOrder for any BT

Java concise solution  beat 96%
This problem is very similar to 435. The difference is that in this problem, we can shoot two balloon with one shot by shooting at edge of two intervals, like for [1,2], [2,3], we can shoot at 2. And also to find the minimum arrows means we need to find out maximum non overlapping intervals.
Although in this problem, LC uses int[] to represent interval, let's talk it as interval. Firstly sort the interval by end in ascending order. Then scan the array to find out how many nonoverlapping intervals. The time complexity is O(N*logN), space complexity is O(1).public int findMinArrowShots(int[][] points) { if(points.length == 0  points[0].length == 0) return 0; Arrays.sort(points, new Comparator<int[]>(){ public int compare(int[] a, int[] b){ return a[1]  b[1]; } }); int count = 1, end = points[0][1]; for(int i = 1;i < points.length;i++){ if(points[i][0] > end){//here is >, not >=, since if they are equal, it indicates they are overlapped. end = points[i][1]; count++; }//we don't need to update end if they are overlapped. } return count; }

RE: 1 ms Easy understand Java Solution
By assigning the node to null, the original structure of tree is destroyed, although it could work.

RE: Java Dynamic Programming Solution (21ms, with explanation)
@fabrizio3 Yes, thanks for pointing out. I realized it by running an example. :)

Java Accept Solution
The running time is not that fast, since I just use string concatenate.
The method is simple, every time we scan from the beginning to find the first peak element and remove it. Loop it for K times.public String removeKdigits(String num, int k) { if(k >= num.length()) return "0"; int i = 0; while(k > 0){ i = 0; for(; i < num.length()1 && num.charAt(i) <= num.charAt(i+1);i++); num = num.substring(0, i) + (i < num.length()1? num.substring(i+1):""); k; } //get rid of heading '0' for(i = 0; i < num.length() && num.charAt(i) == '0';i++); return i == num.length()? "0":num.substring(i); }

RE: Java Dynamic Programming Solution (21ms, with explanation)
Why don't use boolean type for the DP array? Since the value only has two possibilities.

RE: JAVA solution very very ez!
I searched several web, and seems the time complexity of Math.pow is nearly O(1). So it is O(1) solution.
http://stackoverflow.com/questions/32418731/javamathpowabtimecomplexity 
RE: Simple Python+Java
@wzrthhj It's not O(N*N), instead it is O(1). The nested loop is 12 * 60 regardless of value of N.

RE: Java O(n) easy to understand solution, easily extended to any times of occurance
public int singleNumber(int[] nums) { int res = 0; for(int i = 0; i < 32; i++){ int sum = 0; for(int n: nums) if((n >> i & 1) == 1) sum++; sum %= 3; res = sum<<i; } return res; }
Brilliant idea! I have two suggestions:
 move sum %= 3 out of the inner loop to reduce the module operation.
 remove the condition check ( sum != 0).

Concise Java Accepted Solution
public int read(char[] buf, int n) { char[] tempBuf = new char[4]; int index = 0; while(n > 0){ int count = Math.min(read4(tempBuf), n); for(int i = 0; i < count;i++) buf[index++] = tempBuf[i]; //if count is less than 4, it means reading reaches the end, either end of file or already read N char. if(count < 4) return index; n = 4; } return index; }