-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcb2668
commit 94a567d
Showing
87 changed files
with
571 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
package com.DP;public class FiboSeries { | ||
package com.DP; | ||
|
||
import java.util.Arrays; | ||
|
||
public class FiboSeries { | ||
public static void main(String[] args) { | ||
int n =5; | ||
int []dp = new int[n+1]; | ||
Arrays.fill(dp, -1); | ||
System.out.println(fibo(dp,n)); | ||
} | ||
static int fibo(int [] dp , int n){ | ||
if(n <= 1){ | ||
return 1; | ||
} | ||
if(dp[n] != -1){ | ||
return dp[n]; | ||
}else{ | ||
return dp[n] = fibo(dp, n-1) + fibo(dp , n-2); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
package com.DP;public class FrogJump2 { | ||
package com.DP; | ||
|
||
import java.util.ArrayList; | ||
//coding ninja frog jump | ||
public class FrogJump2 { | ||
public static void main(String[] args) { | ||
int [] heights = {0,2,5,6,7}; | ||
System.out.println(frogJumpRecursion(heights.length-1,heights)); | ||
} | ||
public static int frogJumpRecursion(int index , int [] heights){ | ||
if(index == 0) return 0; | ||
int left = frogJumpRecursion(index-1,heights) + Math.abs(heights[index] - heights[index-1]); | ||
int right = Integer.MAX_VALUE; | ||
if(index > 1) right = frogJumpRecursion(index-2,heights) + Math.abs(heights[index] - heights[index-2]); | ||
return Math.min(left,right); | ||
} | ||
|
||
public static int frogjump(int index, int [] heights , int [] dp){ | ||
if(index == 0) return 0; | ||
if(dp[index] != -1) return dp[index]; | ||
int left = frogjump(index-1,heights,dp) + Math.abs(heights[index] - heights[index-1]); | ||
int right = Integer.MAX_VALUE; | ||
if(index > 1) right = frogjump(index-2,heights,dp) + Math.abs(heights[index] - heights[index-2]); | ||
return Math.min(left,right); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,32 @@ | ||
package com.DP;public class climbStairs { | ||
package com.DP; | ||
|
||
public class climbStairs { | ||
public static void main(String[] args) { | ||
|
||
} | ||
static int iterative(int n){ | ||
if(n == 0) return 0; | ||
if(n == 1) return 1; | ||
if(n == 2) return 2; | ||
int oneStepBefore = 2; | ||
int twoStepBefore = 1; | ||
int res = 0 ; | ||
for(int i = 2 ; i < n ; i++ ){ | ||
res = oneStepBefore + twoStepBefore ; | ||
twoStepBefore = oneStepBefore; | ||
oneStepBefore = res; | ||
} | ||
return res; | ||
} | ||
|
||
static int recursive(int n){ | ||
if(n<=1){ | ||
return 1; | ||
}else{ | ||
int left = recursive(n-1); | ||
int right = recursive(n-2); | ||
return left + right; | ||
} | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
package com.DSA;public class BST { | ||
package com.DSA; | ||
|
||
public class BST { | ||
public class Node{ | ||
private int value; | ||
private int height; | ||
private Node left; | ||
private Node right; | ||
|
||
public Node(int value){ | ||
this.value = value; | ||
} | ||
|
||
public int getValue(){ | ||
return value; | ||
} | ||
} | ||
private Node root; | ||
public BST(){ | ||
|
||
} | ||
public int height(Node node){ | ||
if(node == null){ | ||
return -1; | ||
} | ||
return node.height; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return root == null; | ||
} | ||
|
||
public void display(){ | ||
display(root,"Root Node : "); | ||
} | ||
private void display(Node node, String details){ | ||
if(node == null){ | ||
return ; | ||
} | ||
System.out.println(details + node.getValue()); | ||
display(node.left, "Left Child of "+node.getValue() + " : "); | ||
display(node.right, "Right Child of "+node.getValue() + " : "); | ||
} | ||
|
||
|
||
|
||
public static void main(String[] args) { | ||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,84 @@ | ||
package com.DSA;public class BinaryTree { | ||
package com.DSA; | ||
|
||
import java.util.Scanner; | ||
|
||
public class BinaryTree { | ||
private static class Node{ | ||
int value; | ||
Node left; | ||
Node right; | ||
|
||
public Node(int value){ | ||
this.value = value; | ||
} | ||
} | ||
private Node root; | ||
//insertion | ||
public void populate(Scanner scanner){ | ||
System.out.println("Enter the root node: "); | ||
int value = scanner.nextInt(); | ||
root = new Node(value); | ||
populate(scanner,root); | ||
} | ||
|
||
private void populate(Scanner scanner,Node node){ | ||
System.out.println("Do you want to enter left of "+ node.value); | ||
boolean left = scanner.nextBoolean(); | ||
if(left){ | ||
System.out.println("Enter the value of the left of "+ node.value); | ||
int value = scanner.nextInt(); | ||
node.left = new Node(value); | ||
populate(scanner,node.left); | ||
} | ||
System.out.println("Do you want to enter right of "+ node.value); | ||
boolean right = scanner.nextBoolean(); | ||
if(right){ | ||
System.out.println("Enter the value of the right of "+ node.value); | ||
int value = scanner.nextInt(); | ||
node.right = new Node(value); | ||
populate(scanner,node.right); | ||
} | ||
} | ||
public void display(){ | ||
display(root,""); | ||
} | ||
private void display(Node node,String indent){ | ||
if(node == null){ | ||
return ; | ||
} | ||
System.out.println(indent + node.value); | ||
display(node.left , indent + "\t"); | ||
display(node.right , indent + "\t"); | ||
} | ||
public void prettyDisplay(){ | ||
prettyDisplay(root,0); | ||
|
||
} | ||
private void prettyDisplay(Node node, int level){ | ||
if(node == null){ | ||
return ; | ||
} | ||
prettyDisplay(node.right , level+1); | ||
|
||
if(level != 0 ){ | ||
for(int i =0; i <level -1 ; i++){ | ||
System.out.print("|\t\t"); | ||
} | ||
System.out.println("|------->"+ node.value); | ||
}else{ | ||
System.out.println(node.value); | ||
} | ||
prettyDisplay(node.left,level+1); | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc =new Scanner(System.in); | ||
BinaryTree tree = new BinaryTree(); | ||
tree.populate(sc); | ||
// tree.display(); | ||
tree.prettyDisplay(); | ||
|
||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.DSA; | ||
|
||
public class FIndBadVersion { | ||
public static void main(String[] args) { | ||
|
||
} | ||
//First Bad Version | ||
public static int firstBadVersion(int n) { | ||
if(n == 0 || n == 1){ | ||
return 1; | ||
} | ||
|
||
int start = 0; | ||
int end = n; | ||
int ans = 0; | ||
while(start <= end){ | ||
int mid = start+(end-start)/2; | ||
// predefined api | ||
// boolean x = isBadVersion(mid); | ||
// if(x==true){ | ||
// ans = mid; | ||
// end = mid-1; | ||
// } | ||
// else{ | ||
// start = mid+1; | ||
// } | ||
} | ||
return ans; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.DSA; | ||
|
||
public class FindPeakElement { | ||
public static void main(String[] args) { | ||
|
||
} | ||
// Find Peak Element | ||
public static int findPeakElement(int[] nums) { | ||
|
||
if(nums.length == 1) return 0; // single element | ||
|
||
int n = nums.length; | ||
|
||
// check if 0th/n-1th index is the peak element | ||
if(nums[0] > nums[1]) return 0; | ||
if(nums[n-1] > nums[n-2]) return n-1; | ||
|
||
// search in the remaining array | ||
int start = 1; | ||
int end = n-2; | ||
|
||
while(start <= end) { | ||
int mid = start + (end - start)/2; | ||
if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]) return mid; | ||
else if(nums[mid] < nums[mid-1]) end = mid - 1; | ||
else if(nums[mid] < nums[mid+1]) start = mid + 1; | ||
} | ||
return -1; // dummy return statement | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.DSA; | ||
|
||
public class NdoubleExist { | ||
public static void main(String[] args) { | ||
|
||
} | ||
//Check If N and Its Double Exist | ||
public static boolean checkIfExist(int[] arr) { | ||
for(int i = 0; i < arr.length ; i++){ | ||
for(int j = 0; j < arr.length ; j++){ | ||
if(i==j){ | ||
continue; | ||
} | ||
if(arr[i] == 2 * arr[j]){ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.DSA; | ||
|
||
public class Search2DMatrix { | ||
//74. Search a 2D Matrix | ||
public static void main(String[] args) { | ||
int [][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}}; | ||
System.out.println(searchMatrix(matrix,3)); | ||
} | ||
public static boolean searchMatrix(int[][] matrix, int target) { | ||
for(int i = 0; i < matrix.length ; i++){ | ||
for(int j = 0; j < matrix[i].length ; j++){ | ||
int temp = matrix[i].length - 1 ; | ||
if(target > matrix[i][temp]){ | ||
break; | ||
}else{ | ||
return helper(matrix , i , target); | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean helper(int [][] matrix , int row , int target){ | ||
int start = 0; | ||
int end = matrix[row].length; | ||
while(start <= end){ | ||
int mid = start + (end - start)/2; | ||
if(matrix[row][mid] == target){ | ||
return true; | ||
}else if(matrix[row][mid] < target){ | ||
start = mid+1; | ||
}else{ | ||
end = mid - 1; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.DSA; | ||
|
||
public class SingleNonDuplicate { | ||
public static void main(String[] args) { | ||
|
||
} | ||
//Single Element in a Sorted Array | ||
|
||
public static int singleNonDuplicate(int[] nums) { | ||
int left = 0, right = nums.length - 1; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (mid % 2 == 1) { | ||
mid--; | ||
} | ||
if (nums[mid] != nums[mid + 1]) { | ||
right = mid; | ||
} else { | ||
left = mid + 2; | ||
} | ||
} | ||
return nums[left]; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.DSA; | ||
|
||
public class Sqrt { | ||
public static void main(String[] args) { | ||
|
||
} | ||
//Sqrt(x) | ||
public static int singleNonDuplicate(int[] nums) { | ||
int left = 0, right = nums.length - 1; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (mid % 2 == 1) { | ||
mid--; | ||
} | ||
if (nums[mid] != nums[mid + 1]) { | ||
right = mid; | ||
} else { | ||
left = mid + 2; | ||
} | ||
} | ||
return nums[left]; | ||
} | ||
} |
Oops, something went wrong.