๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.63K subscribers
5.61K photos
3 videos
95 files
10.6K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <sstream>
#include <unordered_map>

using namespace std;

void generateResult(int N, int M, vector<string>& employeeSalaryAndSuperior, vector<string>& operations) {
    vector<int> salaries(N, 0);
    unordered_map<int, vector<int>> supervisors;

    for (int i = 0; i < N; ++i) {
        stringstream ss(employeeSalaryAndSuperior[i]);
        int salary, supervisor;
        ss >> salary >> supervisor;
        salaries[i] = salary;
        if (supervisor != -1) {
            supervisors[supervisor - 1].push_back(i);
        }
    }

    for (int i = 0; i < M; ++i) {
        stringstream ss(operations[i]);
        char type;
        ss >> type;
        if (type == 'p') {
            int A, X;
            ss >> A >> X;
            A -= 1;
            for (int subordinate : supervisors[A]) {
                salaries[subordinate] += X;
            }
        } else if (type == 'u') {
            int B;
            ss >> B;
            B -= 1;
            cout << salaries[B] << endl;
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    cin.ignore();

    vector<string> employeeSalaryAndSuperior(N);
    for (int i = 0; i < N; ++i) {
        getline(cin, employeeSalaryAndSuperior[i]);
    }

    vector<string> operations(M);
    for (int i = 0; i < M; ++i) {
        getline(cin, operations[i]);
    }

    generateResult(N, M, employeeSalaryAndSuperior, operations);
    return 0;
}

Salary fluctuations โœ…
๐Ÿ‘1
def solution(a):
    if not a:
        return []
   
    result = []
    for i in range(len(a)):
        first_char = a[i][0]
        if i == len(a) - 1:
            last_char = a[0][-1]
        else:
            last_char = a[i + 1][-1]
        result.append(first_char + last_char)
   
    return result

Databrick โœ…
def bird_nest(forest, bird):
    nest = []
    total_length = 0
    direction = 1
    current_position = bird
    visited = set()

    while total_length < 100:
        while forest[current_position] == 0 or current_position in visited:
            current_position += direction

        nest.append(current_position)
        total_length += forest[current_position]
        visited.add(current_position)

        current_position = bird
        direction *= -1

    return nest

Databrick โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
List<Integer> results = new ArrayList<>();
    int counter = 1;
    Map<Integer, Integer> allocationMap = new HashMap<>();

    for (int[] query : queries) {
        if (query[0] == 0) {
            int length = query[1];
            int result = -1;
            for (int i = 0; i <= memory.length - length; i++) {
                boolean canAllocate = true;
                for (int j = 0; j < length; j++) {
                    if (memory[i + j] != 0) {
                        canAllocate = false;
                        break;
                    }
                }
                if (canAllocate) {
                    for (int j = 0; j < length; j++) {
                        memory[i + j] = counter;
                    }
                    allocationMap.put(counter, length);
                    result = counter++;
                    break;
                }
            }
            results.add(result);
        } else if (query[0] == 1) {
            int id = query[1];
            if (!allocationMap.containsKey(id)) {
                results.add(-1);
                continue;
            }
            int length = allocationMap.get(id);
            boolean erased = false;
            for (int i = 0; i < memory.length; i++) {
                if (memory[i] == id) {
                    for (int j = 0; j < length; j++) {
                        memory[i + j] = 0;
                    }
                    allocationMap.remove(id);
                    results.add(length);
                    erased = true;
                    break;
                }
            }
            if (!erased) {
                results.add(-1);
            }
        }
    }

    int[] resultArray = new int[results.size()];
    for (int i = 0; i < results.size(); i++) {
        resultArray[i] = results.get(i);
    }
    return resultArray;
}
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
def find_local_maxima(matrix):
    def is_local_maximum(matrix, i, j):
        val = matrix[i][j]
        if val == 0:
            return False
        for di in [-1, 0, 1]:
            for dj in [-1, 0, 1]:
                if di == 0 and dj == 0:
                    continue
                ni, nj = i + di, j + dj
                if 0 <= ni < len(matrix) and 0 <= nj < len(matrix[0]):
                    if matrix[ni][nj] >= val:
                        return False
        return True

    local_maxima = []
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if is_local_maximum(matrix, i, j):
                local_maxima.append((i, j))
   
    return local_maxima[:2]
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
int solution(vector<int>& A, vector<int>& B) {
    int n = A.size();
    vector<vector<int>> graph(n + 1);
    vector<bool> visited(n + 1, false);
    int count = 0;
    unordered_map<string, int> directions;

    for (int i = 0; i < n; ++i) {
        graph[A[i]].push_back(B[i]);
        graph[B[i]].push_back(A[i]);
        string directionKey = to_string(A[i]) + "->" + to_string(B[i]);
        directions[directionKey] = 0;
    }

    function<void(int)> dfs = [&](int city) {
        visited[city] = true;
        for (int neighbor : graph[city]) {
            if (visited[neighbor]) {
                continue;
            }
            string changedDirectionKey = to_string(neighbor) + "->" + to_string(city);
            if (directions.find(changedDirectionKey) == directions.end()) {
                count++;
            }
            dfs(neighbor);
        }
    };

    dfs(0);

    return count;
}
โค1
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> row, col;
void dfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int x, int y)
{
    int rows = matrix.size();
    int cols = matrix[0].size();
    visited[x][y] = true;
    for (int k = 0; k < row.size(); ++k)
    {
        int newX = x + row[k];
        int newY = y + col[k];
  if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && matrix[newX][newY] == 1 && !visited[newX][newY])
  {
            dfs(matrix, visited, newX, newY);
        }
    }
}

int hello(vector<vector<int>>& matrix, int adj)
{
    int rows = matrix.size();
    int cols = matrix[0].size();
    vector<vector<bool>> visited(rows, vector<bool>(cols, false));
    int count = 0;
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            if (matrix[i][j] == 1 && !visited[i][j])
            {
                count++;
                dfs(matrix, visited, i, j);
            }
        }
    }
    return count;
}

int main() {
    int adj, rows, cols;
    cin >> adj;
    cin >> rows >> cols;
   
    vector<vector<int>> matrix(rows, vector<int>(cols));
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cin >> matrix[i][j];
        }
    }
    if (adj == 1) {
        row = {0, 0};
        col = {-1, 1};
    } else if (adj == 2) {
        row = {-1, 1};
        col = {0, 0};
    } else if (adj == 3) {
        row = {-1, -1, 1, 1};
        col = {-1, 1, -1, 1};
    } else if (adj == 4) {
        row = {0, 0, -1, 1};
        col = {-1, 1, 0, 0};
    } else if (adj == 5) {
        row = {-1, -1, -1, 0, 0, 1, 1, 1};
        col = {-1, 0, 1, -1, 1, -1, 0, 1};
    }

    int result = hello(matrix, adj);
    cout << result << endl;

    return 0;
}

HPC programming โœ…
s = [0]

def mapper(b):
    num_dict = {0: 1, 1: 2, 2: 0}
    return num_dict[b]

class tmap:
    def __init__(self, fun, s):
        self.fun = fun
        self.list = s
        self.cachedlen = len(self.list)

    def __getitem__(self, i):
        return self.fun(self.list[i])

    def __len__(self):
        return self.cachedlen

class concat:
    def __init__(self, s1):
        self.s1 = s1
        self.s2 = tmap(mapper, self.s1)
        self.cachedlen = len(s1) * 2

    def __getitem__(self, i):
        if i < len(self.s1):
            return self.s1[i]
        else:
            return self.s2[i - len(self.s1)]

    def __len__(self):
        return self.cachedlen

for i in range(30):
    s = concat(s)

# Use s[n] to obtain the n-th number in the sequence
print(len(s))
def solve(N, M, A):
    from functools import lru_cache
    import sys
   
    @lru_cache(None)
    def dp(mask):
        if mask == 0:
            return 0
        res = sys.maxsize
        for i in range(N):
            if mask & (1 << i):
                for j in range(M):
                    res = min(res, A[i][j] | dp(mask ^ (1 << i)))
        return res
   
    full_mask = (1 << N) - 1
    return dp(full_mask)

# Input reading
if name == "main":
    import sys
    input = sys.stdin.read
    data = input().split()
   
    N = int(data[0])
    M = int(data[1])
    A = []
    idx = 2
    for i in range(N):
        A.append(list(map(int, data[idx:idx + M])))
        idx += M
   
    print(solve(N, M, A))

Minimum possible sum โœ