Skip to content

Commit

Permalink
Day 58 by admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh971 authored Apr 27, 2023
1 parent 568a498 commit a402643
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Day 58/Bulb Switcher_Leetcode/Bulb_Switcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int bulbSwitch(int n) {
return sqrt(n);
}
};
102 changes: 102 additions & 0 deletions Day 58/Easy Task_GFG/Easy_Task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
class Solution{
public:
vector<char> easyTask(int n,string s,int q,vector<vector<string>> &queries){
vector<vector<int>> tree(4 * n, vector<int> (26));

function<void(int, int, int)> build = [&](int node, int low, int high){
if(low == high){
vector<int> f(26, 0);
++f[s[low - 1] - 'a'];
tree[node] = f;
return;
}

int mid = low + (high - low) / 2;
build(2 * node, low, mid);
build(2 * node + 1, mid + 1, high);

for(int i = 0; i < 26; i++)
tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];
};

function<void(int, int, int, int, int, int)> update = [&](int node, int low, int high, int rangelow, int rangehigh, char dx){
if(low > rangehigh or high < rangelow)
return;

if(low >= rangelow and high <= rangehigh){
vector<int> f(26, 0);
++f[dx - 'a'];
tree[node] = f;

return;
}

int mid = low + (high - low) / 2;

update(2 * node, low, mid, rangelow, rangehigh, dx);
update(2 * node + 1, mid + 1, high, rangelow, rangehigh, dx);

for(int i = 0; i < 26; i++)
tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];
};

function<vector<int>(int, int, int, int, int)> query = [&](int node, int low, int high, int rangelow, int rangehigh) -> vector<int> {
vector<int> f(26, 0);

if(low > rangehigh or high < rangelow)
return f;

if(low >= rangelow and high <= rangehigh)
return tree[node];

int mid = low + (high - low) / 2;

vector<int> x = query(2 * node, low, mid, rangelow, rangehigh);
vector<int> y = query(2 * node + 1, mid + 1, high, rangelow, rangehigh);

for(int i = 0; i < 26; i++)
f[i] = x[i] + y[i];

return f;
};

build(1, 1, n);

vector<char> ans;

for(int i = 0; i < q; i++){
int ch = stoi(queries[i][0]);

if(ch == 1){
int index = stoi(queries[i][1]);
++index;

char c = queries[i][2][0];

update(1, 1, n, index, index, c);
}
else{
int l, r, k;
l = stoi(queries[i][1]);
r = stoi(queries[i][2]);
k = stoi(queries[i][3]);

++l; ++r;

vector<int> f = query(1, 1, n, l, r);

int count = 0;
for(int i = 25; i > -1; i--){
count += f[i];

if(count >= k){
ans.push_back((char)(i + 'a'));
break;
}
}
}
}

return ans;
}
};

0 comments on commit a402643

Please sign in to comment.