@mo10 @pinkfloyda @StefanPochmann
Please correct me guys if I am wrong,
Many a people are talking about PQ's but if we have an array[1,2,3] and K=3, so here it will be an O(nlogn) which is what we see when talking about the "WORST" case. Thus, PQs cannot be used if we have to do better than nlgn
G
Gobu07
@Gobu07
2
Reputation
24
Posts
104
Profile views
0
Followers
0
Following
Posts made by Gobu07

RE: Java O(n) Solution  Bucket Sort

RE: Accepted O(n) solution
A good Intuitive and practical solution... better than above ones

Array Iteration with Stacks . Java
class Solution { public int calPoints(String[] ops) { Stack<Integer>stack = new Stack<Integer>(); for(String i: ops){ if(i.equals("D")){ if(!stack.isEmpty()){ stack.push(stack.peek()*2); } } else if(i.equals("C")){ if(!stack.isEmpty()){ stack.pop(); } } else if(i.equals("+")){ if(!stack.isEmpty()){ int temp = stack.pop(); int temp2 = temp+stack.peek(); stack.push(temp); stack.push(temp2); } } else{ stack.push(Integer.parseInt(i)); } } int sum=0; while(!stack.isEmpty()){ System.out.println(stack.peek()); sum+=stack.pop(); } return sum; } }

RE: Super simple and clean Java, binary search.
@oneexy
The reason of using mid = start+(endstart)/2 is:
because sometimes when you want to find mid of x and y such that x and y are very large numbers so it goes out of the range of int. 
RE: Concise Java solutions O(log(n)^2)
A very legible Solution for beginers
class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; int left = getHeightLeft(root); int right = getHeightRight(root); //If left and right are equal it means that the tree is complete and hence go for 2^h 1. if(left == right) return ((2<<(left)) 1); //else recursively calculate the number of nodes in left and right and add 1 for root. else return countNodes(root.left)+ countNodes(root.right)+1; } public int getHeightLeft(TreeNode root){ int count=0; while(root.left!=null){ count++; root = root.left; } return count; } public int getHeightRight(TreeNode root){ int count=0; while(root.right!=null){ count++; root = root.right; } return count; } }

When U have a good legible Solution (JAVA)
class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; int left = getHeightLeft(root); int right = getHeightRight(root); //If left and right are equal it means that the tree is complete and hence go for 2^h 1. if(left == right) return ((2<<(left)) 1); //else recursively calculate the number of nodes in left and right and add 1 for root. else return countNodes(root.left)+ countNodes(root.right)+1; } public int getHeightLeft(TreeNode root){ int count=0; while(root.left!=null){ count++; root = root.left; } return count; } public int getHeightRight(TreeNode root){ int count=0; while(root.right!=null){ count++; root = root.right; } return count; } }

RE: [C++] [Java] Clean Code  6 liner Without Modifying Input
@messi14 Preety Similar or same approach

if (rand.nextInt(++count) == count1) result = i;
The most important confusion for this question is
"What if the count is 2 or 10 (when x is repeated 10 times) " and we are only checking for
rand.nextInt(++count) == 0 ??So, this is for an understanding of the above idea..
public class Solution { int[] nums; Random rand; public Solution(int[] nums) { this.nums = nums; this.rand = new Random(); } public int pick(int target) { int result = 1; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != target) continue; if (rand.nextInt(++count) == count1) result = i; } return result; } }