Skip to content

Commit

Permalink
added good questions based on heap data structure from leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Muskaanchaturvedi committed Oct 12, 2023
1 parent e259d25 commit 4fb53a6
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 108 deletions.
23 changes: 23 additions & 0 deletions Heaps/Priority_Queues/1642. Furthest Building You Can Reach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int furthestBuilding(vector<int>& h, int bricks, int ladders) {
int ans=0,sum=0,n=h.size();
priority_queue<int,vector<int>,greater<int>>pq;
for(int i=1;i<n;i++){
int diff=h[i]-h[i-1];
if(diff<=0){
ans=i;
continue;
}
pq.push(diff);
if(pq.size()>ladders){
sum+=pq.top();
pq.pop();
if(sum>bricks)break;
}
ans=i;
}
return ans;
}
};
//link->https://leetcode.com/problems/furthest-building-you-can-reach/
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<int> mostCompetitive(vector<int>& nums, int k) {
stack<int>st;
vector<int>v(k);
int n=nums.size();
int nums_to_be_removed=n-k;
for(int i=0;i<n;i++){
while(!st.empty() && nums_to_be_removed>0 && st.top() > nums[i]){
st.pop();
nums_to_be_removed--;
}
st.push(nums[i]);
}
while(st.size()>k)st.pop();
for(int i=k-1;i>=0;i--){
v[i]=st.top();
st.pop();
}
return v;
}
};
//link->https://leetcode.com/problems/find-the-most-competitive-subsequence/
20 changes: 20 additions & 0 deletions Heaps/Priority_Queues/347. Top K Frequent Elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int>v;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
unordered_map<int,int>mp;
for(auto it:nums)mp[it]++;
for(auto itr:mp){
pq.push({itr.second,itr.first});
if(pq.size()>k)pq.pop();
}
while(!pq.empty()){
v.push_back(pq.top().second);
pq.pop();
}
return v;

}
};
//link->https://leetcode.com/problems/top-k-frequent-elements/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
#define pi pair<int,int>
int kthSmallest(vector<vector<int>>& mat, int k) {
priority_queue<pair<int,pi>,vector<pair<int,pi>>,greater<pair<int,pi>> >pq;
int n=mat.size(), m=mat[0].size();
pq.push({mat[0][0],{0,0}});
set<pi>vis;
vis.insert({0,0});
while(k>1){
int row=pq.top().second.first;
int col=pq.top().second.second;
pq.pop();
if(row+1<n && vis.find({row+1,col})==vis.end()){
pq.push({mat[row+1][col],{row+1,col}});
vis.insert({row+1,col});
}
if(col+1<m && vis.find({row,col+1})==vis.end()){
pq.push({mat[row][col+1],{row,col+1}});
vis.insert({row,col+1});
}
k--;
}
return pq.top().first;
}
};
//link->https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
25 changes: 25 additions & 0 deletions Heaps/Priority_Queues/767. Reorganize String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
string reorganizeString(string s) {
unordered_map<char,int>mp;
for(auto c:s)mp[c]++;
priority_queue<pair<int,char>>pq;
for(auto itr:mp)pq.push({itr.second,itr.first});
auto temp1=pq.top();
pq.pop();
string ans="";
ans+=temp1.second;
temp1.first--;
while(!pq.empty()){
auto temp2=pq.top();
pq.pop();
ans+=temp2.second;
temp2.first--;
if(temp1.first>0)pq.push(temp1);
temp1=temp2;
}
if(temp1.first>0)return "";
return ans;
}
};
//link->https://leetcode.com/problems/reorganize-string/
216 changes: 108 additions & 108 deletions bs.cpp
Original file line number Diff line number Diff line change
@@ -1,108 +1,108 @@
// #include <bits/stdc++.h>
// using namespace std;
// int* bubbleSort(int arr[], int n) {


// for (int i = 0; i< n; i++) {


// for (int j = 0; j < n -1- i; j++) {


// if (arr[j] > arr[j + 1]) {
// int temp = arr[j];
// arr[j] = arr[j+ 1];
// arr[j + 1] = temp;
// }
// }
// }
// return arr;
// }

// int binarySearch(int arr[], int x, int low, int high) {


// while (low <= high) {
// int mid = low + high / 2;

// if (arr[mid] == x)
// return mid;

// if (arr[mid] < x)
// low = mid + 1;

// else
// high = mid - 1;
// }

// return -1;
// }

// int main() {
// int n,x;
// cout<<"Enter the size of the array"<<endl;
// cin>>n;
// int arr[n];
// cout<<"Enter the elements of the array"<<endl;
// for(int i=0;i<n;i++)
// cin>>arr[i];
// cout<<"Enter the element you want to search in the array"<<endl;
// cin>>x;
// bubbleSort(arr,n);
// int result = binarySearch(arr, x, 0, n-1 );
// if (result == -1)
// printf("Not found");
// else
// printf("Element is found at index %d", result);
// }
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n) {


for (int i = 0; i< n; i++) {
for (int j = 0; j < n -1- i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j+ 1];
arr[j + 1] = temp;
}
}
}
}

int binarySearch(int arr[], int x, int low, int high) {
while (low <= high) {
int mid = low + high / 2;

if (arr[mid] == x)
return mid;

if (arr[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}

int main() {
int n,x;
cout<<"Enter the size of the array"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements of the array"<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the element you want to search in the array"<<endl;
cin>>x;

bubbleSort(arr,n);
int result = binarySearch(arr, x, 0, n-1 );
if (result == -1)
printf("Element is not present in the array");
else
printf("Element is found at position %d", result +1);
}
// #include <bits/stdc++.h>
// using namespace std;
// int* bubbleSort(int arr[], int n) {


// for (int i = 0; i< n; i++) {


// for (int j = 0; j < n -1- i; j++) {


// if (arr[j] > arr[j + 1]) {
// int temp = arr[j];
// arr[j] = arr[j+ 1];
// arr[j + 1] = temp;
// }
// }
// }
// return arr;
// }

// int binarySearch(int arr[], int x, int low, int high) {


// while (low <= high) {
// int mid = low + high / 2;

// if (arr[mid] == x)
// return mid;

// if (arr[mid] < x)
// low = mid + 1;

// else
// high = mid - 1;
// }

// return -1;
// }

// int main() {
// int n,x;
// cout<<"Enter the size of the array"<<endl;
// cin>>n;
// int arr[n];
// cout<<"Enter the elements of the array"<<endl;
// for(int i=0;i<n;i++)
// cin>>arr[i];
// cout<<"Enter the element you want to search in the array"<<endl;
// cin>>x;
// bubbleSort(arr,n);
// int result = binarySearch(arr, x, 0, n-1 );
// if (result == -1)
// printf("Not found");
// else
// printf("Element is found at index %d", result);
// }
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n) {


for (int i = 0; i< n; i++) {
for (int j = 0; j < n -1- i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j+ 1];
arr[j + 1] = temp;
}
}
}
}

int binarySearch(int arr[], int x, int low, int high) {
while (low <= high) {
int mid = low + high / 2;

if (arr[mid] == x)
return mid;

if (arr[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}

int main() {
int n,x;
cout<<"Enter the size of the array"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements of the array"<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the element you want to search in the array"<<endl;
cin>>x;

bubbleSort(arr,n);
int result = binarySearch(arr, x, 0, n-1 );
if (result == -1)
printf("Element is not present in the array");
else
printf("Element is found at position %d", result +1);
}

0 comments on commit 4fb53a6

Please sign in to comment.