-
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
26210c8
commit 2f1d3fe
Showing
12 changed files
with
182 additions
and
254 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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); | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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."); | ||
} | ||
} | ||
} | ||
|
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,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)); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.