Skip to content

Commit 43c5379

Browse files
add new implementations for ordered map, k-sorted array sorting, and string removal; delete unused files
1 parent cb9aa65 commit 43c5379

File tree

11 files changed

+129
-1
lines changed

11 files changed

+129
-1
lines changed

09_String/01removepart.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
class Solution {
4+
public:
5+
string removeOccurrences(string s, string part) {
6+
//sliding window of size same as part
7+
int n = part.size();
8+
string win = "";
9+
int idx = 0;
10+
while(idx<s.size()){
11+
if(win.size()<=n){
12+
win.append(s.substr(idx,n));
13+
}
14+
cout<<win<<endl;
15+
if(win==part){
16+
win = "";
17+
s.erase(idx,n);
18+
cout<<s<<endl;
19+
idx=0;
20+
}
21+
else{
22+
win = "";
23+
idx++;
24+
}
25+
}
26+
return s;
27+
}
28+
};
29+
int main(){
30+
string s = "ccctltctlltlb";
31+
string part = "ctl";
32+
Solution *sol1 = new Solution();
33+
string ans = sol1->removeOccurrences(s,part);
34+
cout<<ans;
35+
}
36+
/*
37+
LC1910. Remove All Occurrences of a Substring
38+
*/
File renamed without changes.
Binary file not shown.

26Heap/04sortKSorted.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
//sort a k sorted array (mostly sorted array)
6+
7+
// void ksorted(vector<int>& v, int k, vector<int> &ans){
8+
// //O(nlogk)
9+
// priority_queue<int, vector<int>, greater<int>> pq;//min heap
10+
// for(int i=0;i<v.size();i++){
11+
// pq.push(v[i]);
12+
// if(pq.size()>k) {
13+
// ans.push_back(pq.top());
14+
// pq.pop();
15+
// }
16+
// }
17+
// while(!pq.empty()){
18+
// ans.push_back(pq.top());
19+
// pq.pop();
20+
// }
21+
// return;
22+
// }
23+
24+
//without extra space
25+
void ksorted(vector<int>& v, int k){
26+
//O(nlogk)
27+
priority_queue<int, vector<int>, greater<int>> pq;//min heap
28+
int idx = 0;
29+
for(int i=0;i<v.size();i++){
30+
pq.push(v[i]);
31+
if(pq.size()>k) {
32+
v[idx++] = pq.top();
33+
pq.pop();
34+
}
35+
}
36+
while(!pq.empty()){
37+
v[idx++] = pq.top();
38+
pq.pop();
39+
}
40+
return;
41+
}
42+
int main(){
43+
vector<int> v = {6,5,3,2,8,10,9};
44+
int k = 3;
45+
ksorted(v,k);
46+
for(int ele:v){
47+
cout<<ele<<" ";
48+
}
49+
}

26Heap/04sortKSorted.exe

171 KB
Binary file not shown.

27Set/implementation.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include<iostream>
22
#include<unordered_set>
3+
#include<set>
34
using namespace std;
45
int main(){
56
unordered_set<int> us;
@@ -17,7 +18,17 @@ int main(){
1718
*/
1819
cout<<"exists"<<endl;
1920
}
20-
for(int ele : us){//prints in random order and only unique elements
21+
// for(int ele : us){//prints in random order and only unique elements
22+
// cout<<ele<<" ";
23+
// }
24+
25+
set<int> st;
26+
st.insert(10);
27+
st.insert(20);
28+
st.insert(23);
29+
st.insert(32);
30+
st.insert(32);
31+
for(int ele : st){
2132
cout<<ele<<" ";
2233
}
2334
}

27Set/implementation.exe

27.7 KB
Binary file not shown.

28Map/02orderedmap.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
//ordered set arranges on basis of key
5+
map<int,string> mp;
6+
map<string,int> mp2;
7+
mp.emplace(1,"a");
8+
mp.emplace(2,"we");
9+
mp.emplace(3,"abd");
10+
mp.emplace(4,"z");
11+
mp.emplace(5,"m");
12+
13+
mp2.emplace("a",1);
14+
mp2.emplace("we",2);
15+
mp2.emplace("abd",5);
16+
mp2.emplace("z",0);
17+
mp2.emplace("m",9);
18+
19+
20+
for(auto ele: mp){
21+
cout<<ele.first<<":"<<ele.second<<endl;
22+
}
23+
cout<<endl;
24+
for(auto ele: mp2){
25+
cout<<ele.first<<":"<<ele.second<<endl;
26+
}
27+
28+
}

28Map/02orderedmap.exe

285 KB
Binary file not shown.

sample.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
Simple file for doing a local run for lC questions*/

sample.exe

144 KB
Binary file not shown.

0 commit comments

Comments
 (0)