Forwarded from Goldman Sachs Exam Solutions
Desert Queen
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool isValid(int x, int y, int n, const vector<vector<char>>& grid) {
return x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'M';
}
// @PLACEMENTLELO
int fMW(int n, const vector<vector<char>>& grid, pair<int, int> start, pair<int, int> end) {
vector<vector<int>> placementlelo(n, vector<int>(n, INT_MAX));
queue<pair<int, int>> q;
https://telegram.me/+_hn3cBQVbGliYTI9
q.push(start);
placementlelo[start.first][start.second] = 0;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
https://telegram.me/+_hn3cBQVbGliYTI9
for (int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
if (isValid(nx, ny, n, grid)) {
int cost = (grid[x][y] == 'T' && grid[nx][ny] == 'T') ? 0 : 1;
if (placementlelo[x][y] + cost < placementlelo[nx][ny]) {
placementlelo[nx][ny] = placementlelo[x][y] + cost;
q.push({nx, ny});
}
}
}
}
https://telegram.me/+_hn3cBQVbGliYTI9
return placementlelo[end.first][end.second];
}
@PLACEMENTLELO
int main() {
int n;
cin >> n;
https://telegram.me/PLACEMENTLELO
vector<vector<char>> grid(n, vector<char>(n));
pair<int, int> start, end;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'S') start = {i, j};
if (grid[i][j] == 'E') end = {i, j};
}
}
int result = fMW(n, grid, start, end);
cout << result << endl;
return 0;
}
Desert Queen
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool isValid(int x, int y, int n, const vector<vector<char>>& grid) {
return x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'M';
}
// @PLACEMENTLELO
int fMW(int n, const vector<vector<char>>& grid, pair<int, int> start, pair<int, int> end) {
vector<vector<int>> placementlelo(n, vector<int>(n, INT_MAX));
queue<pair<int, int>> q;
https://telegram.me/+_hn3cBQVbGliYTI9
q.push(start);
placementlelo[start.first][start.second] = 0;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
https://telegram.me/+_hn3cBQVbGliYTI9
for (int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
if (isValid(nx, ny, n, grid)) {
int cost = (grid[x][y] == 'T' && grid[nx][ny] == 'T') ? 0 : 1;
if (placementlelo[x][y] + cost < placementlelo[nx][ny]) {
placementlelo[nx][ny] = placementlelo[x][y] + cost;
q.push({nx, ny});
}
}
}
}
https://telegram.me/+_hn3cBQVbGliYTI9
return placementlelo[end.first][end.second];
}
@PLACEMENTLELO
int main() {
int n;
cin >> n;
https://telegram.me/PLACEMENTLELO
vector<vector<char>> grid(n, vector<char>(n));
pair<int, int> start, end;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'S') start = {i, j};
if (grid[i][j] == 'E') end = {i, j};
}
}
int result = fMW(n, grid, start, end);
cout << result << endl;
return 0;
}
Desert Queen
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
Forwarded from Goldman Sachs Exam Solutions
Buzz Sale
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
https://telegram.me/PLACEMENTLELO
int main() {
int n;
cin >> n;
// @PLACEMENTLELO
vector<int> ids(n), costs(n);
for (int i = 0; i < n; i++) cin >> ids[i];
for (int i = 0; i < n; i++) cin >> costs[i];
int budget;
cin >> budget;
// @PLACEMENTLELO
int mfi = 0, mfw = 0;
for (int i = 0; i < n; i++) {
int buyCost = costs[i];
int maxQty = budget / buyCost;
// @PLACEMENTLELO
if (maxQty > 0) {
int cfi = 0;
int cfw = 0;
https://telegram.me/PLACEMENTLELO
for (int j = 0; j < n; j++) {
if (i != j && ids[i] % ids[j] == 0) {
cfi += maxQty;
cfw += costs[j] * maxQty;
}
}
// @PLACEMENTLELO
if (cfi > mfi ||
(cfi == mfi && cfw > mfw)) {
mfi = cfi;
mfw = cfw;
}
}
}
// @PLACEMENTLELO
cout << mfi << " " << mfw << endl;
return 0;
}
Buzz Sale
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
https://telegram.me/PLACEMENTLELO
int main() {
int n;
cin >> n;
// @PLACEMENTLELO
vector<int> ids(n), costs(n);
for (int i = 0; i < n; i++) cin >> ids[i];
for (int i = 0; i < n; i++) cin >> costs[i];
int budget;
cin >> budget;
// @PLACEMENTLELO
int mfi = 0, mfw = 0;
for (int i = 0; i < n; i++) {
int buyCost = costs[i];
int maxQty = budget / buyCost;
// @PLACEMENTLELO
if (maxQty > 0) {
int cfi = 0;
int cfw = 0;
https://telegram.me/PLACEMENTLELO
for (int j = 0; j < n; j++) {
if (i != j && ids[i] % ids[j] == 0) {
cfi += maxQty;
cfw += costs[j] * maxQty;
}
}
// @PLACEMENTLELO
if (cfi > mfi ||
(cfi == mfi && cfw > mfw)) {
mfi = cfi;
mfw = cfw;
}
}
}
// @PLACEMENTLELO
cout << mfi << " " << mfw << endl;
return 0;
}
Buzz Sale
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9