This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Size of binary tree | |
public static int getSize(Node node) | |
{ | |
if(node == null){ | |
return 0; | |
} | |
int left = getSize(node.left); | |
int right = getSize(node.right); | |
return left+right+1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] productExceptSelf(int[] nums) { | |
int ans[] = new int[nums.length]; | |
int prod = 1; | |
for(int i=0;i<nums.length;i++){ //get product from left to right without using the index element | |
ans[i] = prod; | |
prod *= nums[i]; | |
} | |
int revprod = 1; | |
for(int i=nums.length-1;i>=0;i--){ //get product from right to left without using the index element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int rob(int[] nums) { | |
int dp[] = new int[nums.length]; | |
Arrays.fill(dp,-1); | |
return maxAmount(nums,nums.length-1, dp); | |
} | |
public int maxAmount(int[] nums, int idx, int dp[]){ | |
if(idx < 0){ | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int i=0;i<m;i++){ //row wise | |
for(int j=0;j<n;j++){ | |
// matrix[i][j]) | |
} | |
} | |
for(int i=0;i<n;i++){ //column wise | |
for(int j=0;j<m;j++){ | |
// matrix[j][i] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pair implements Comparable<Pair>{ | |
String name; | |
int height; | |
public Pair(String name, int height){ | |
this.name = name; | |
this.height = height; | |
} | |
public int compareTo(Pair other){ | |
return other.height - this.height; //descending order => this.height - other.height for ascending order | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void getAllSubsets(int[] nums, int i, int xor){ | |
if(i>=nums.length){ | |
//any terminating condition | |
} | |
getAllSubsets(nums,i+1,xor^nums[i]);//picked | |
getAllSubsets(nums,i+1,xor);//not picked | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int max1 = nums[0]; //first max | |
int max2 = 0; //second max | |
for(int i=1;i<nums.length;i++){ | |
if(nums[i]>max1){ | |
max2 = max1; | |
max1 = nums[i]; //copy the first max to second max | |
}else if(nums[i]>max2){ | |
max2 = nums[i]; //second max | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[] plusOne(int[] A) { | |
ArrayList<Integer> al = new ArrayList<>(); | |
//add 1 to last of element | |
int sum = (A[A.length-1]+1)%10; | |
int carry = (A[A.length-1]+1)/10; | |
al.add(sum); | |
//loop from second last to first | |
for(int i=A.length-2;i>=0;i--){ | |
int num = A[i]+carry; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type IGun interface { | |
setName(name string) | |
setPower(power int) | |
getName() string | |
getPower() int | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Online Go compiler to run Golang program online | |
// Print "Hello World!" message | |
package main | |
import "fmt" | |
type Client struct{ | |
} |
NewerOlder