LeetCode Weekly Solutions
7.27K subscribers
34 photos
11 files
101 links
Latest Jobs and Internships are regularly uploaded here : https://t.me/placementlelo

If you want any other exam answers for free : @exam_cheating_bot

Collaborations: @growth_admin
Download Telegram
Lost In Orange Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main()
{
int N, M;
cin >> N >> M;
https://telegram.me/PLACEMENTLELO
vector<vector<int>> grid(N, vector<int>(M));
for (int i = 0; i < N; ++i){
for (int j = 0; j < M; ++j){
cin >> grid[i][j];
}
}

vector<vector<long long>> dp(N, vector<long long>(M, 0));
vector<vector<long long>> placementlelo(N, vector<long long>(M, LLONG_MAX));

if (grid[0][0] == 0) placementlelo[0][0] = 0;
https://telegram.me/PLACEMENTLELO
dp[0][0] = grid[0][0];
for (int i = 0; i < N; ++i){
for (int j = 0; j < M; ++j){
if (i == 0 && j == 0)
continue;

if (i > 0){
dp[i][j] = max(dp[i][j], dp[i - 1][j] + grid[i][j] - placementlelo[i - 1][j]);
placementlelo[i][j] = min(placementlelo[i][j], placementlelo[i - 1][j] + 2);
}

if (j > 0){
dp[i][j] = max(dp[i][j], dp[i][j - 1] + grid[i][j] - placementlelo[i][j - 1]);
placementlelo[i][j] = min(placementlelo[i][j], placementlelo[i][j - 1] + 2);
}
https://telegram.me/PLACEMENTLELO
if (grid[i][j] == 0) placementlelo[i][j] = 0;
}
}
https://telegram.me/PLACEMENTLELO
cout << dp[N - 1][M - 1] << endl;

return 0;
}

Lost In Orange Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Intervals Maximization Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

#include <bits/stdc++.h>
using namespace std;

int mb(vector<int>& A, vector<pair<int, int>>& it) {
int placementlelo = A.size();
sort(it.begin(), it.end(), [](pair<int, int>& a, pair<int, int>& b) { return a.second < b.second; });
https://telegram.me/PLACEMENTLELO

vector<int> dp(placementlelo + 1, 0);
vector<int> ln(placementlelo + 1, 0);

for (auto& p : it) {
int st = p.first;
int en = p.second;
set<int> ds(A.begin() + st - 1, A.begin() + en);
int bt = accumulate(ds.begin(), ds.end(), 0);
int pe = ln[st - 1];
https://telegram.me/PLACEMENTLELO

dp[en] = max(dp[en], dp[pe] + bt);

for (int i = en; i <= placementlelo; i++) {
ln[i] = max(ln[i], en);
}
}
https://telegram.me/PLACEMENTLELO

return *max_element(dp.begin(), dp.end());
}

Intervals Maximization Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9
Count Subtree Factor Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> tr;
vector<int> sz;
vector<int> pr;
https://telegram.me/PLACEMENTLELO
void f(int nd, int pa) {
sz[nd] = 1;
pr[nd] = pa;
for (int ch : tr[nd]) {
if (ch == pa) continue;
f(ch, nd);
sz[nd] += sz[ch];
}
}
https://telegram.me/PLACEMENTLELO
int u(int nd) {
int uc = 0;
for (int v = 1; v <= sz[nd]; ++v) {
if (nd % v == 0) {
uc++;
}
}
return uc;
}
https://telegram.me/PLACEMENTLELO
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

int n, m, cl;
cin >> n >> m >> cl;
tr.resize(n + 1);
sz.resize(n + 1);
pr.resize(n + 1, -1);
https://telegram.me/PLACEMENTLELO
for (int i = 0; i < m; ++i) {
int u1, u2;
cin >> u1 >> u2;
tr[u1].push_back(u2);
tr[u2].push_back(u1);
}
https://telegram.me/PLACEMENTLELO
f(1, -1);

int tu = 0;
for (int nd = 1; nd <= n; ++nd) {
tu += u(nd);
}
https://telegram.me/PLACEMENTLELO
cout << tu << endl;
return 0;
}

Count Subtree Factor Code
Infosys SP Exam
C++

https://telegram.me/+_hn3cBQVbGliYTI9