Skip to content

Commit

Permalink
added leetcode-solution folder
Browse files Browse the repository at this point in the history
in this folder i've added a binary search folder that consists of some awesome question, check that out and lemme know
  • Loading branch information
tamannasgh committed Oct 4, 2022
1 parent 08b5b75 commit c34d4f7
Show file tree
Hide file tree
Showing 28 changed files with 1,116 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Leetcode-solutions/Binary-search/ArrangingCoins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class ArrangingCoins {
public static void main(String[] args) {
int n = 1;
System.out.println(arrangeCoins2(n));
}

static int arrangeCoins(int n) {
long s = 1;
long e = n;

while(s <= e){
long m = s + (e-s)/2;
long coinsNeeded = (m * (m+1)) /2;

if(coinsNeeded <= n){
s = m+1;
} else{
e= m-1;
}
}
return (int)e;
}

static int arrangeCoins2(int n){
long s = 1;
long e = n;

while(s < e){
long m = s + (e-s)/2;
long coinsNeeded = (m * (m+1)) /2;

if(coinsNeeded <= n){
s = m+1;
} else{
e = m;
}
}
return (s * (s+1)) /2 <= n ? (int)s : (int)s-1;
}

}
21 changes: 21 additions & 0 deletions Leetcode-solutions/Binary-search/CountNegGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class CountNegGrid {
public static void main(String[] args) {

}

static int countNegatives(int[][] grid) {
int s = 0;
int e = grid[0].length-1;
int count = 0;

while(s < grid.length && e >= 0){
if(grid[s][e] < 0){
count += grid.length - s;
e--;
} else{
s++;
}
}
return count;
}
}
47 changes: 47 additions & 0 deletions Leetcode-solutions/Binary-search/FirstLastPos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Arrays;

public class FirstLastPos {
public static void main(String[] args) {
int[] nums = {5,7,7,8,8,10};
int target = 8;
int[] ans = firstLastPosition(nums, target);
System.out.println( Arrays.toString(ans) );
}

static int[] firstLastPosition(int[] nums, int target){
int[] ans = {-1, -1};

ans[0] = serach(nums, target, true);
if(ans[0] == -1){
return ans;
}
ans[1] = serach(nums, target, false);
return ans;
}

static int serach(int[] nums, int target, boolean findingFirst){
int s = 0;
int e = nums.length-1;
int ans = -1;

while(s <= e){
int m = s + (e-s)/2;

if(nums[m] == target){
ans = m;

if( findingFirst ){
e = m-1;
} else{
s = m+1;
}
} else if(nums[m] < target){
s = m+1;
} else{
e = m-1;
}
}

return ans;
}
}
43 changes: 43 additions & 0 deletions Leetcode-solutions/Binary-search/FirstbadVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class FirstbadVersion {
public static void main(String[] args) {
int n = 1;
System.out.println( firstBadVersion2(n));
}

static int firstBadVersion(int n) {
int s = 1;
int e = n;

while(s <= e){
int m = s + (e-s)/2;

if( isBadVersion(m) ){
e = m-1;
} else{
s = m+1;
}
}

return s;
}

static int firstBadVersion2(int n){
int s = 1;
int e = n;

while(s < e){
int m = s + (e-s)/2;

if(isBadVersion(m)){
e = m;
} else{
s = m+1;
}
}
return isBadVersion(s) ? s : s+1;
}

static boolean isBadVersion(int n){
return 1 == n;
}
}
24 changes: 24 additions & 0 deletions Leetcode-solutions/Binary-search/HIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class HIndex {
public static void main(String[] args) {
int[] citations = {1,2,100};
System.out.println(hIndex(citations));
}

static int hIndex(int[] citations) {
int s = 0;
int e = citations.length-1;

while(s <= e){
int m = s + (e-s)/2;

if(citations[m] >= (citations.length - m) ){
e = m-1;
}
else{
s = m+1;
}
}
return citations.length - s;
}

}
52 changes: 52 additions & 0 deletions Leetcode-solutions/Binary-search/IntersectionOfTwoArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.ArrayList;
import java.util.Arrays;

public class IntersectionOfTwoArrays {
public static void main(String[] args) {

}

static int[] intersection(int[] nums1, int[] nums2) {
if(nums1.length > nums2.length){
return intersection(nums2, nums1);
}
ArrayList<Integer> list = new ArrayList<>();
Arrays.sort(nums1);

for(int num : nums2){

if(bS(nums1, num)){
if(!list.contains(num)){
list.add(num);
}
}
}

int[] ans = new int[list.size()];

for(int i = 0 ; i < list.size() ; i++){
ans[i] = list.get(i);
}
return ans;
}

static boolean bS(int[] arr, int target){
int s = 0;
int e = arr.length-1;

while(s <= e){
int m = s + (e-s)/2;

if(arr[m] == target){
return true;
}
if(arr[m] < target){
s = m+1;
} else{
e = m-1;
}
}
return false;
}

}
35 changes: 35 additions & 0 deletions Leetcode-solutions/Binary-search/KClosestElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.List;

public class KClosestElement {
public static void main(String[] args) {
int[] arr = {1,1,2,2,2,2,2,3,3};
System.out.println( findClosestElements(arr, 3, 3) );
}

static List<Integer> findClosestElements(int[] arr, int k, int x) {
int s = 0;
int e = arr.length-k;

while(s < e){
int m = s + (e-s)/2;

int indexJustAfterWindow = m+k;
while(indexJustAfterWindow <= e && arr[m] == arr[indexJustAfterWindow] ){
indexJustAfterWindow++;
}

if( Math.abs( arr[m] - x ) > Math.abs( x - arr[indexJustAfterWindow] ) ){
s = m+1;
} else{
e = m;
}
}

List<Integer> list = new ArrayList<>();
for(int i = s; i < s +k ; i++){
list.add( arr[i] );
}
return list;
}
}
42 changes: 42 additions & 0 deletions Leetcode-solutions/Binary-search/KoKoEatingBanana.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class KoKoEatingBanana {
public static void main(String[] args) {
int[] piles = {3,6,7,11};
System.out.println(minEatingSpeed(piles, 8));
}

static int minEatingSpeed(int[] piles, int h) {
int s = 1;
int e = piles[0];
for(int pile : piles){
e = Math.max(e, pile);
}

while(s < e){
int m = s + (e-s)/2;

if( canEatAllWithSpeed(piles, m, h) ){
e = m;
} else{
s = m+1;
}
}
return s;
}

static boolean canEatAllWithSpeed(int[] piles, int allowedBananas, int h) {
int hoursNeeded = 0;

for(int pile : piles){

// dekh agr mne allow kre h 4 banana in 1 hour or is pile main 10 h to 4 + 4 kha paegi or do bchange to 3 ghnte ab ek cheez dekh simple ye nh kr skti 10 /4 to vo seedha mtlb dedega ki 10 main kitne proper 4 ke piece bnenge baat smjh soch thoda thik h or fir ek check lga liyo ki agr modulo krke 0 nhi aaara to mtlb kya h iska ki abhi kuch bche h allowed se km mtlb 4 se km h to bss hour main 1 plus krde bss ab bss shant dimaag se soch aa jaega smjh or agr allowed to 4 h pr piles h 3 to 3 /4 0 aaega or aage bss ek plus ho jaega and that is right!!
hoursNeeded += pile/allowedBananas;
if(pile % allowedBananas != 0){
hoursNeeded++;
}
}

return hoursNeeded <= h;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class KthSmallestMultiplicationTable {
public static void main(String[] args) {
int m = 3;
int n = 3;
int k = 5;
System.out.println(findKthNumber(m, n, k));
}

static int findKthNumber(int m, int n, int k) {
int s = 1;
int e = m * n;

while(s < e){
int mid = s + (e-s)/2;

if( isPotentialK(mid, m, n, k) ){
e = mid;
} else{
s = mid + 1;
}
}
return s; // or e as they both are pointing to the same element
}

static boolean isPotentialK(int potential, int m, int n, int k) {
int count = 0;
for(int i = 1; i <= m ; i++){
count += Math.min( (potential / i) , n ); //sun ye isliye kyunki ye hr row simply table h 1 se m tk ka, ab mujhe nikalna h hr row mai se 5 ya kisi num se kitne chhote to ye simploe nikl jaega, kse aise ki maan tujhe nikalna h ki 5 se chhote kitne num h 2 ke table mai to nikaal ki 5 2 ke table mai kahan aata h ya uska floor nikaal to ab 5 nhi aata to floor ya fr agr decial mai aaega na 5/2 = 2.5 to point to ht hi jaega int mai to 2 or haan yhi to h 2 ke table mai 5 se chhote 2 num h thike to ab ye potential/i waali baat to smjh aa gyi pr ye min kyu n se, isliye kyunki soch 1 ke table mai 5 se chhote ya baraabar kitne h 5/1 = 5 pr que mai given table to 3 tk jaari hna to mai ye thodi likh skti ki is row mai jisme 3 elements h usme 5 se chhote 5 elements h ab smjh gyi to kya hoga ki agr jada honge to fr wo min 3 ko lega or agr km honge to fr to whi h ans. huh!!!
}
return count >= k;
}


}
43 changes: 43 additions & 0 deletions Leetcode-solutions/Binary-search/LongestIncrSubSeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.ArrayList;

public class LongestIncrSubSeq {
public static void main(String[] args) {
int[] nums = {1,1,1,1,1,1};
System.out.println(lengthOfLIS(nums));
}

static int lengthOfLIS(int[] nums) {
ArrayList<Integer> list = new ArrayList<>();
list.add(nums[0]);

for(int i = 1 ; i < nums.length ; i++){

if( list.get( list.size() -1 ) < nums[i] ){
list.add(nums[i]);
} else{
int index = ceil(list, nums[i]);
list.set(index, nums[i]);
}

}

return list.size();
}

static int ceil(ArrayList<Integer> list, int target){
int s = 0;
int e = list.size() -1;

while(s <= e){
int m = s + (e-s)/2;

if(list.get(m) >= target){
e = m-1;
} else{
s = m+1;
}
}
return s;
}

}
Loading

0 comments on commit c34d4f7

Please sign in to comment.