forked from MAYANK25402/Hactober-2023-1
-
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.
added good questions based on heap data structure from leetcode
- Loading branch information
1 parent
e259d25
commit 4fb53a6
Showing
6 changed files
with
226 additions
and
108 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Heaps/Priority_Queues/1642. Furthest Building You Can Reach.cpp
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,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/ |
23 changes: 23 additions & 0 deletions
23
Heaps/Priority_Queues/1673. Find the Most Competitive Subsequence.cpp
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,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/ |
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,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/ |
27 changes: 27 additions & 0 deletions
27
Heaps/Priority_Queues/378. Kth Smallest Element in a Sorted Matrix.cpp
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 @@ | ||
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/ |
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 @@ | ||
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/ |
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 |
---|---|---|
@@ -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); | ||
} |