Skip to content

Commit

Permalink
java-dsa
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Jan 23, 2024
1 parent 26210c8 commit 2f1d3fe
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 254 deletions.
9 changes: 8 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Problems/Arrays/BestTimeToBuySellStock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Java_DSA.Problems.Arrays;

import java.util.* ;
import java.io.*;
import java.util.ArrayList;

public class BestTimeToBuySellStock{
public static int maximumProfit(ArrayList<Integer> prices){
// Write your code here.
int min = prices.get(0);
int maxProfit = 0;
for(int i = 0;i < prices.size();i++){
min = Math.min(min,prices.get(i));
int profit = prices.get(i)-min;
maxProfit = Math.max(maxProfit,profit);
}
return maxProfit;
}

public static void main(String[] args) {
ArrayList<Integer>list = new ArrayList<>(Arrays.asList(17, 20, 11, 9, 12, 6));
int ans = maximumProfit(list);
System.out.println(ans);
}
}
33 changes: 33 additions & 0 deletions Problems/BitManipulation/MaxInterview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Java_DSA.Problems.BitManipulation;

public class MaxInterview {
static int calculateNumberOfDays(String s) {
// Write your code here.
int n = s.length();
int[] dp = new int[n];

// Base case: The first day's interview can always be attended
dp[0] = s.charAt(0) - '0';

for (int i = 1; i < n; i++) {
// Calculate the maximum number of interviews that can be attended
// on the current day by considering two possibilities:
// 1. Attend the interview on the current day
// 2. Skip the interview on the current day

// If we attend the interview on the current day, add 1 to the
// maximum interviews attended on the previous day (if any)
int option1 = (i >= 2 ? dp[i - 2] : 0) + (s.charAt(i) - '0');

// If we skip the interview on the current day, the maximum
// interviews attended remain the same as on the previous day
int option2 = dp[i - 1];

// Choose the maximum of the two options
dp[i] = Math.max(option1, option2);
}

// The result is the maximum interviews that can be attended on the last day
return dp[n - 1];
}
}
33 changes: 33 additions & 0 deletions Problems/Matrix/UpperLowerSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Java_DSA.Problems.Matrix;
import java.util.*;

public class UpperLowerSum {
static ArrayList<Integer> sumTriangles(int matrix[][], int n)
{
ArrayList<Integer>ans = new ArrayList<>();
int upper = 0;
int lower = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(i<=j){
upper += matrix[i][j];
}
if(j<=i){
lower += matrix[i][j];
}
}
}
ans.add(upper);
ans.add(lower);
return ans;
}

public static void main(String[] args) {
int mat[][]={{6, 5, 4},
{1, 2, 5},
{7, 9, 7}};
ArrayList<Integer>list;
list = sumTriangles(mat,3);
System.out.println(list);
}
}
27 changes: 27 additions & 0 deletions Problems/Strings/CountFrequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Java_DSA.Problems.Strings;

import java.util.HashMap;
import java.util.Map;

public class CountFrequence {
public static void main(String[] args) {
String inputString = "Java";
Map<Character, Integer> charCountMap = new HashMap<>();
inputString = inputString.toLowerCase();

for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
if (Character.isLetter(c)) {
if (charCountMap.containsKey(c)) {
charCountMap.put(c, charCountMap.get(c) + 1);
} else {
charCountMap.put(c, 1);
}
}
}
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
System.out.println("'" + entry.getKey() + "' occurs " + entry.getValue() + " times.");
}
}
}

29 changes: 29 additions & 0 deletions Problems/Strings/Same.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Java_DSA.Problems;

public class Same {
static long sameDigits(long n) {
String s = Long.toString(n);
int start = s.charAt(0)-'0';
int l = s.length();
if(l == 1){
return Long.parseLong(s);
}
else{
StringBuilder one = new StringBuilder();
for(int i = 0;i < l;i++){
one.append('1');
}
long number = Long.parseLong(one.toString());
long min = start*number;
long max = (start+1)*number;
if(n-min < max - n)
return min;
else
return max;
}
}

public static void main(String[] args) {
System.out.println(sameDigits(277));
}
}
27 changes: 27 additions & 0 deletions Problems/Strings/TossScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Java_DSA.Problems.Strings;
import java.util.Scanner;

public class TossScore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter string : ");
String str = sc.next();
int tail = 0,head = 0;
int last = str.length()-1;
for(int i = 0;i < str.length()-3;i++){
if(str.charAt(i)=='H' && str.charAt(i+1)=='H' && str.charAt(i+2)=='H'){
last = i + 2;
break;
}
}
for(int i = 0;i <= last;i++){
if(str.charAt(i)=='T')
tail++;
else
head++;
}
// System.out.println(head);
// System.out.println(tail);
System.out.println((2*head)+(-1*tail));
}
}
42 changes: 0 additions & 42 deletions Sorting/BubbleSort.java

This file was deleted.

41 changes: 0 additions & 41 deletions Sorting/InsertionSort.java

This file was deleted.

71 changes: 0 additions & 71 deletions Sorting/MergeSort.java

This file was deleted.

Loading

0 comments on commit 2f1d3fe

Please sign in to comment.