Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekghoshh committed Dec 22, 2024
1 parent 02625ab commit 4cd18dc
Show file tree
Hide file tree
Showing 21 changed files with 623 additions and 62 deletions.
7 changes: 1 addition & 6 deletions README-old.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@
- bit-manipulation
-

- [3 Sum : Find triplets that add up to a zero](/src/com/problems/array/ThreeSum.java)
- two-pointer
- hashing
- [4 Sum / Find Quads that add up to a target value](/src/com/problems/array/FourSum.java)
- two-pointer
- hashing



- [Insert Delete GetRandom O(1)](/src/com/problems/array/RandomizedSetProblem.java)
Expand Down
11 changes: 10 additions & 1 deletion resources/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@


### General
- [Two Sum : Check if a pair with given sum exists in Array](/src/com/problems/array/TwoSum.java)
- [3 Sum : Find triplets that add up to a zero](/src/com/problems/array/ThreeSum.java)
- [4 Sum / Find Quads that add up to a target value](/src/com/problems/array/FourSum.java)

- [Contains Duplicate](/src/com/problems/hashing/ContainsDuplicate.java)
- [Contains Duplicate II](/src/com/problems/hashing/ContainsDuplicate2.java)

- [Replace Elements with Greatest Element on Right Side](/src/com/problems/array/ReplaceElementsWithGreatestElementOnRightSide.java)
- [Two Sum : Check if a pair with given sum exists in Array](/src/com/problems/array/TwoSum.java)

- [Program to generate Pascal’s Triangle](/src/com/problems/array/PascalTriangle.java)
- [Program to generate Pascal’s Triangle one row](/src/com/problems/array/PascalTriangleOneRow.java)
- [Program to generate Pascal’s Triangle one element](/src/com/problems/array/PascalTriangleOneElement.java)

- [Remove Element](/src/com/problems/array/RemoveElement.java)
- [Majority Element I | Find the Majority Element that occurs more than N/2 times](/src/com/problems/array/MajorityElements1.java)
- [Majority Element II | Find the elements that appear more than N/3 times in the array](/src/com/problems/array/MajorityElements2.java)
Expand All @@ -34,7 +40,10 @@
- [Valid Sudoku](/src/com/problems/array/ValidSudoku.java)
- [Longest Consecutive Sequence in an Array](/src/com/problems/array/LongestConsecutiveSequence.java)
- [Sort an array of 0's 1's and 2's](/src/com/problems/array/SortZeroOneTwo.java)
- [Minimum Number of Operations to Make Elements in Array Distinct](/src/com/problems/array/MinimumNumberOfOperationsToMakeElementsInArrayDistinct.java)

- [Minimum Number of Swaps to Make The String Balanced](/src/com/problems/array/MinimumNumberOfSwapsToMakeTheStringBalanced.java)
- [Number of Pairs of Interchangeable Rectangles](/src/com/problems/array/NumberOfPairsOfInterchangeableRectangles.java)


### Swap sort
Expand Down
1 change: 1 addition & 0 deletions resources/dynamic-programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@
- [Maximize Total Cost of Alternating Subarrays](/src/com/problems/dp/MaximizeTotalCostOfAlternatingSubarrays.java)
- [Minimum Cost for Cutting Cake I](/src/com/problems/dp/MinimumCostForCuttingCakeI.java)
- [Sum of Good Subsequences](/src/com/problems/dp/SumOfGoodSubsequences.java)
- [Count Paths With the Given XOR Value](/src/com/problems/dp/CountPathsWithTheGivenXORValue.java)

3 changes: 2 additions & 1 deletion resources/greedy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
- [Non-overlapping Intervals](/src/com/problems/greedy/NonOverlappingIntervals.java)
- [Increasing Triplet Subsequence](/src/com/problems/greedy/IncreasingTripletSubsequence.java)
- [Minimum Number of Pushes to Type Word I](/src/com/problems/greedy/MinimumNumberOfPushesToTypeWord1.java)
- [Minimum Number of Pushes to Type Word II](/src/com/problems/greedy/MinimumNumberOfPushesToTypeWord2.java)
- [Minimum Number of Pushes to Type Word II](/src/com/problems/greedy/MinimumNumberOfPushesToTypeWord2.java)
- [Maximum Number of Distinct Elements After Operations](/src/com/problems/greedy/MaximumNumberOfDistinctElementsAfterOperations.java)
1 change: 1 addition & 0 deletions resources/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Encode and Decode TinyURL](/src/com/problems/array/EncodeAndDecodeTinyURL.java)
- [Brick Wall](/src/com/problems/array/BrickWall.java)
- [Unique Length 3 Palindromic Subsequences](/src/com/problems/array/UniqueLength3PalindromicSubsequences.java)
- [Minimum Number of Operations to Make Elements in Array Distinct](/src/com/problems/array/MinimumNumberOfOperationsToMakeElementsInArrayDistinct.java)



Expand Down
21 changes: 20 additions & 1 deletion resources/special-problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,23 @@
- [Jump Game 6](/src/com/problems/special/jumpgame/JumpGame6.java)
- [Jump Game 7](/src/com/problems/special/jumpgame/JumpGame7.java)
- [Frog Jump 1](/src/com/problems/special/jumpgame/FrogJump1.java)
- [Frog Jump 2](/src/com/problems/special/jumpgame/FrogJump2.java)
- [Frog Jump 2](/src/com/problems/special/jumpgame/FrogJump2.java)

### Resources

### Stone Game

- [Stone game]
- [Stone game II]
- [Stone game III]
- [Stone game IV]
- [Stone game V]
- [Stone game VI]
- [Stone game VII]
- [Stone game VIII]
- [Stone game IX]

### Resources

- [Master Game Theory / Geeksforgeeks](https://www.youtube.com/playlist?list=PLM68oyaqFM7RqV8GBCMGsmwyFxUVr36tx)
- [Game Theory - Level 3 / pepcoding](https://www.youtube.com/playlist?list=PL-Jc9J83PIiENC1X16gjxjaIbSXBr8C6e)
9 changes: 7 additions & 2 deletions resources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
- [Implement Stack using Arrays](/src/com/problems/stack/Stack.java)
- [Implement Stack using Queue](/src/com/problems/stack/StackUsingQueue.java)
- [Implement stack using Linkedlist](/src/com/problems/stack/StackUsingLinkedlist.java)
- [Implement Queue using Stack](/src/com/problems/queue/QueueUsingStack.java)
- [Implement Min Stack](/src/com/problems/stack/MinStack.java)

- [Valid Parentheses](/src/com/problems/stack/ValidParenthesis.java)
- [Longest Valid Parentheses](/src/com/problems/stack/LongestValidParenthesis.java)
- [Implement Min Stack](/src/com/problems/stack/MinStack.java)
- [Baseball Game](/src/com/problems/stack/BaseballGame.java)
- [Make The String Great](/src/com/problems/stack/MakeTheStringGreat.java)

- [Infix to Postfix](/src/com/problems/stack/InfixToPostfix.java)
- [Postfix to Infix](/src/com/problems/stack/PostfixToInfix.java)
- [Infix To Prefix](/src/com/problems/stack/InfixToPrefix.java)
- [Prefix to Infix](/src/com/problems/stack/PrefixToInfix.java)
- [Prefix to Postfix](/src/com/problems/stack/PrefixToPostfix.java)
- [Postfix to Prefix](/src/com/problems/stack/PostfixToPrefix.java)
- [Make The String Great](/src/com/problems/stack/MakeTheStringGreat.java)

- [Next Greater Element](/src/com/problems/stack/NextGreaterElement.java)
- [Next Greater Element for different array](/src/com/problems/stack/NextGreaterElementForDifferentArray.java)
- [Next Greater Element in a Circle](/src/com/problems/stack/NextGreaterElementInCircle.java)
Expand Down
17 changes: 11 additions & 6 deletions src/com/problems/array/FourSum.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.problems.array;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/*
Expand All @@ -15,6 +18,8 @@
*
* https://takeuforward.org/data-structure/4-sum-find-quads-that-add-up-to-a-target-value/
* */

// Tags: Arrays, Two Pointers
public class FourSum {

public static void main(String[] args) {
Expand Down Expand Up @@ -44,14 +49,14 @@ private static void type2() {
// we have to skip this nums[i]
if ((long) nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) continue;
for (int j = i + 1; j <= n - 3; j++) {
//if there is a series of duplicates then it will just take the first time when j==i+1
//if there is a series of duplicates, then it will just take the first time when j==i+1
//when j>i+1 then for all duplicate value will be skipped
if (j > i + 1 && nums[j] == nums[j - 1]) continue;
// in a series left most elements are the smallest
// if sum of i and j to j+2 elements are less than target then there is not point to check
// in a series left, most elements are the smallest
// if the sum of i and j to j+2 elements are less than target then there is no point to check
if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target ) break;
// if nums[i] plus nums[j] and last 2 numbers are less than target
// than means nums[j] is useless as last 2 numbers are the highest
// if nums[i] plus nums[j] and the last 2 numbers are less than target
// than means nums[j] is useless as the last 2 numbers are the highest,
// we have to skip this nums[j]
if ((long) nums[i] + nums[j] + nums[n - 2] + nums[n - 1] < target ) continue;
left = j + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.problems.array;

/*
* Problem link:
* https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/
*
* Solution link :
*
*/
public class MinimumNumberOfOperationsToMakeElementsInArrayDistinct {
public static void main(String[] args) {
type1();
type2();
type3();
}

// rather going from first if we go from the last
// if we go from the last till freq of any element is greater than 1
private static void type3() {
int[] nums = {1, 2, 3, 4, 2, 3, 3, 5, 7};
int ans = minimumOperations3(nums);
System.out.println(ans);
}

private static int minimumOperations3(int[] nums) {
int n = nums.length;
int[] freq = new int[101];
int rem = (n % 3);
int parts = (n / 3) + (rem > 0 ? 1 : 0);
int i = n - 1;
// checking for the first part individually if there is any reminder
while (rem > 0) {
rem--;
int num = nums[i--];
freq[num]++;
if (freq[num] == 2) return parts;
}
// this means n has a reminder but that part only contained the unique elements,
// so we will decrement the part needed
if (n % 3 != 0) parts--;

// checking for the remaining parts which is multiple of length 3
while (parts > 0) {
int d = 3;
while (d > 0) {
int num = nums[i--];
freq[num]++;
if (freq[num] == 2) return parts;
d--;
}
parts--;
}
return parts;
}

// hashing approach
// we will store the freq of the numbers and as we well as store a variable to keep track
// the number of non-unique elements. so that we do not need to go through the entire freq map or array
// every time, to check how many non-unique are left
private static void type2() {
int[] nums = {1, 2, 3, 4, 2, 3, 3, 5, 7};
int ans = minimumOperations2(nums);
System.out.println(ans);
}

public static int minimumOperations2(int[] nums) {
int n = nums.length;
int count = 0;
int[] freq = new int[101];
int nonUnique = 0;
for (int num : nums) {
freq[num]++;
// if freq 2, that means it is non-unique,
// we do not need to check for > 2 as once it is non-unique, it will stay as non-unique
if (freq[num] == 2) nonUnique++;
}
// now we will loop till non-unique count is 0
// (non-unique-count > 0) means there should be at least 2 elements in the array
int i = 0;
while (nonUnique > 0) {
count++;
int first = nums[i], second = nums[i + 1];
// decreasing the freq of first and checking if the freq reduced to 1
freq[first]--;
if (freq[first] == 1) nonUnique--;
// decreasing the freq of first and checking if the freq reduced to 1
freq[second]--;
if (freq[second] == 1) nonUnique--;
// checking if there is the third element or not, if it has third then only we will check for it
if (i + 2 < n) {
int third = nums[i + 2];
freq[third]--;
if (freq[third] == 1) nonUnique--;
}
i = i + 3;
}
return count;
}


// brute force approach
private static void type1() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.problems.array;

/*
*
* problem links :
* https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/description/
*
* Solution link :
* https://www.youtube.com/watch?v=3YDBT9ZrfaU
*
* https://neetcode.io/solutions/minimum-number-of-swaps-to-make-the-string-balanced
* */


// Tags: Arrays, Two Pointers, String, Stack, Greedy
public class MinimumNumberOfSwapsToMakeTheStringBalanced {
public static void main(String[] args) {
type1();
}

// todo check this solution again
// We will be a little greedy here
// The total number of unmatched opening brackets at the end will tell us how many swaps are necessary to balance the string
// so, we keep track the number of unmatched closing brackets.
// Each swap can fix two unmatched brackets, so the minimum number of swaps required is (unmatched+1)/2
private static void type1() {
String s = "]][][[";
int ans = minSwaps3(s);
System.out.println(ans);
}

public static int minSwaps3(String s) {
int imbalance = 0; // This variable tracks the number of unmatched closing brackets
int openBrackets = 0; // This variable tracks the number of unmatched opening brackets
// Iterate through each character in the string
for (char c : s.toCharArray()) {
if (c == '[') {
openBrackets++; // Increment for an opening bracket
} else { // c == ']'
if (openBrackets > 0) {
openBrackets--; // Match with an opening bracket
} else {
imbalance++; // Unmatched closing bracket
}
}
}
// Each swap can fix two imbalances, hence divide by 2
return (imbalance + 1) / 2;
}


}
Loading

0 comments on commit 4cd18dc

Please sign in to comment.