Skip to content

Commit

Permalink
dsa questions added
Browse files Browse the repository at this point in the history
  • Loading branch information
itsgaurav355 committed Apr 7, 2024
1 parent 108bff5 commit fcb2668
Show file tree
Hide file tree
Showing 37 changed files with 860 additions and 179 deletions.
2 changes: 2 additions & 0 deletions DP/FiboSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.DP;public class FiboSeries {
}
2 changes: 2 additions & 0 deletions DP/FrogJump2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.DP;public class FrogJump2 {
}
2 changes: 2 additions & 0 deletions DP/climbStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.DP;public class climbStairs {
}
2 changes: 2 additions & 0 deletions DSA/BST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.DSA;public class BST {
}
2 changes: 2 additions & 0 deletions DSA/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.DSA;public class BinaryTree {
}
2 changes: 2 additions & 0 deletions Exam/tp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.Exam;public class tp {
}
8 changes: 0 additions & 8 deletions Java_Classes/Exp3JavaLab.java

This file was deleted.

171 changes: 0 additions & 171 deletions Java_Classes/Experiment1a.java

This file was deleted.

2 changes: 2 additions & 0 deletions Java_Classes/Staff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.Java_Classes;public class Staff {
}
23 changes: 23 additions & 0 deletions Practice/ExcelSheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.Practice;

public class ExcelSheet {
public static void main(String[] args) {
System.out.println(findIndex("ZZZ"));
}
static int findIndex(String str){
return getIndex(str,0);
}

private static int getIndex(String str, int ans) {
if(str.isEmpty()){
return ans;
}
int temp = str.charAt(0);//AA A-->65
if(temp < 65 || temp > 90){
return 0;
}
temp = temp % 64 ;
ans = 26 * ans + temp;
return getIndex(str.substring(1),ans);
}
}
29 changes: 29 additions & 0 deletions Practice/GoodPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.Practice;

public class GoodPosition {
public static void main(String[] args) {
int [] arr = {1,4,3};
System.out.println(findPosition(arr , 3 , 3));
}
static int findPosition(int [] arr , int n,int k ){
int sum = 0;
for(int i =0; i < n ; i++ ){
sum = sum + arr[i];
}
return findPos(arr,0,k,sum , 0);
}

private static int findPos(int[] arr, int i, int k, int sum,int ans) {
if(i == arr.length){
return ans;
}
if((arr[i]+k) > (sum-arr[i])){
ans++;
return findPos(arr , i+1,k,sum,ans);
}else{
return findPos(arr , i+1,k,sum,ans);
}
}


}
39 changes: 39 additions & 0 deletions Practice/LargestAreaHistogram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.Practice;

import java.util.Stack;

//leetcode 84
public class LargestAreaHistogram {
public static void main(String[] args) {
int []heights ={2,1,5,6,2,3};
System.out.println(largestRectangleArea(heights));
}

public static int largestRectangleArea(int [] heights){
Stack<Integer>stack = new Stack<>();
int max = 0;

stack.push(0);
for(int i = 1; i< heights.length ; i++){
while(!stack.isEmpty() && heights[i] < heights[stack.peek()]){
max = getMax(heights,stack,max,i);
}
stack.push(i);
}
int i = heights.length;
while(!stack.isEmpty()){
max = getMax(heights,stack,max,i);
}
return max;
}
static int getMax(int [] heights , Stack<Integer> stack , int max, int index){
int area;
int popped =stack.pop();
if(stack.isEmpty()){
area = heights[popped] * index;
}else{
area = heights[popped] * (index-1-stack.peek());
}
return Math.max(max,area);
}
}
14 changes: 14 additions & 0 deletions Practice/MaxDifference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.Practice;

import java.util.Arrays;

public class MaxDifference {
public static void main(String[] args) {
int [] arr = {1,6,3,2};
Arrays.sort(arr);
int maxDifference = arr[arr.length-1] - arr[0];
System.out.println(maxDifference);


}
}
23 changes: 23 additions & 0 deletions Practice/MergeAlternateString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.Practice;

public class MergeAlternateString {
public static void main(String[] args) {
//1768. Merge Strings Alternately

}
public static String mergeAlternately(String word1, String word2) {
StringBuilder result = new StringBuilder();
int i = 0;
while (i < word1.length() || i < word2.length()) {
if (i < word1.length()) {
result.append(word1.charAt(i));
}
if (i < word2.length()) {
result.append(word2.charAt(i));
// result.append
}
i++;
}
return result.toString();
}
}
26 changes: 26 additions & 0 deletions Practice/MinDifference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.Practice;

import java.util.Arrays;

public class MinDifference {
public static void main(String[] args) {
int [] arr = {90,80,70,75};
System.out.println(minDiff(arr,2));
}
static int minDiff(int [] arr,int num){
if(arr.length == 1 || arr.length<num){
return 0;
}
Arrays.sort(arr);
int j = num-1;
int difference = Integer.MAX_VALUE;
int temp;
for(int i =0;i< arr.length && j< arr.length;i++,j++){
temp = arr[j] - arr[i];
if(difference > temp){
difference = temp;
}
}
return difference;
}
}
Loading

0 comments on commit fcb2668

Please sign in to comment.