๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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
def key_generation(key_mode, roles):
    all_permissions = {
        "payments": ["read_settings", "read_intent", "write_settings", "create_intent", "confirm_intent",
                     "update_dispute", "create_dispute", "read_dispute", "create_refund", "cancel_refund", "read_refund"],
        "banking": ["read_settings", "read_intent", "write_settings", "create_intent", "confirm_intent"],
        "identity": ["read_settings", "read_intent", "write_settings", "create_intent", "confirm_intent"],
        "climate": ["read_settings", "read_intent", "write_settings", "create_intent", "confirm_intent"],
        "capital": ["read_settings", "read_intent", "write_settings", "create_intent", "confirm_intent"]
    }
   
    read_permissions = {
        "payments": ["read_settings", "read_intent", "read_dispute", "read_refund"],
        "banking": ["read_settings", "read_intent"],
        "identity": ["read_settings", "read_intent"],
        "climate": ["read_settings", "read_intent"],
        "capital": ["read_settings", "read_intent"]
    }
   
    roles_dict = {
        "admin": set(),
        "admin_readonly": set()
    }
   
    for product, perms in all_permissions.items():
        roles_dict["admin"].update([f"{product}.{perm}" for perm in perms])
   
    for product, perms in read_permissions.items():
        roles_dict["admin_readonly"].update([f"{product}.{perm}" for perm in perms])
   
    for product in all_permissions.keys():
        roles_dict[product] = set([f"{product}.{perm}" for perm in all_permissions[product]])
   
    user_permissions = set()
    for role in roles:
        if role in roles_dict:
            user_permissions.update(roles_dict[role])
   
    if key_mode == "permissions":
        for permission in sorted(user_permissions):
            print(permission)

Permissions and Roles โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void dfs(int v, const vector<vector<int>>& adjList, vector<bool>& visited) {
    visited[v] = true;

    for (int adjV : adjList[v]) {
        if (!visited[adjV]) {
            dfs(adjV, adjList, visited);
        }
    }
}
vector<int> solve(int flightNodes, const vector<int>& flightFrom, const vector<int>& flightTo, int m, const vector<int>& initiallyDelayed, int k) {
    vector<vector<int>> adjList(flightNodes);
    for (int i = 0; i < m; ++i) {
        adjList[flightFrom[i] - 1].push_back(flightTo[i] - 1);
    }
    vector<bool> visited(flightNodes, false);
    for (int i = 0; i < k; ++i) {
        if (!visited[initiallyDelayed[i] - 1]) {
            dfs(initiallyDelayed[i] - 1, adjList, visited);
        }
    }
    vector<int> delayedFlights;
    for (int i = 0; i < flightNodes; ++i) {
        if (visited[i]) {
            delayedFlights.push_back(i + 1);
        }
    }

    sort(delayedFlights.begin(), delayedFlights.end());
    return delayedFlights;
}
๐Ÿ‘1
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;

vector<int> solve(int N, int M, vector<int>& ans) {
    vector<int> res;
    res.reserve(M);

    priority_queue<pair<long long, int>,
        vector<pair<long long, int>>, 
        greater<pair<long long, int>>> pq;

    for (int i = 1; i <= N; ++i) {
        pq.push(make_pair(0LL, i));
    }

    for (int it : ans) {
        pair<long long, int> top = pq.top();
        pq.pop();

        long long load = top.first;
        int x = top.second;

        res.push_back(x);
        pq.push(make_pair(load + it, x));
    }

    return res;
}

int main(){
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n=2,m=2;
    vector<int> ans={5,5};
    vector<int> ans1=solve(n,m,ans);
    for(int i=0;i<ans1.size();i++) cout<<ans1[i]<<" ";

}

Servers โœ…
class MovieTicket {
public:
    MovieTicket() {
       
    }

    bool book(int userId, int movieId) {
        if (userBookings[userId].find(movieId) != userBookings[userId].end() || availableTickets[movieId] == 0) {
            return false;
        }
        userBookings[userId].insert(movieId);
        movieBookings[movieId].insert(userId);
        availableTickets[movieId]--;
        return true;
    }

    bool cancel(int userId, int movieId) {
        if (userBookings[userId].find(movieId) == userBookings[userId].end()) {
            return false;
        }
        userBookings[userId].erase(movieId);
        movieBookings[movieId].erase(userId);
        availableTickets[movieId]++;
        return true;
    }

    bool isBooked(int userId, int movieId) {
        return userBookings[userId].find(movieId) != userBookings[userId].end();
    }

    int availableTicketsCount(int movieId) {
        return availableTickets[movieId];
    }

private:
    unordered_map<int, unordered_set<int>> userBookings;
    unordered_map<int, unordered_set<int>> movieBookings;
    unordered_map<int, int> availableTickets = [] {
        unordered_map<int, int> m;
        for (int i = 1; i <= 1000; ++i) {
            m[i] = 100;
        }
        return m;
    }();
};

movie tickets โœ…
def getLongestSubsequence(arr):
    n = len(arr)
    max_len = 0
   
    for i in range(n):
        for j in range(i, n):
            x, y = arr[i], arr[j]
            length = 0
            expect_x = True
           
            for k in range(i, n):
                if (expect_x and arr[k] == x) or (not expect_x and arr[k] == y):
                    length += 1
                    expect_x = not expect_x
           
            max_len = max(max_len, length)
   
    return max_len

arr = list(map(int, input().split(',')))
result = getLongestSubsequence(arr)
print(result)

//Interesting sequence
string solution(string &forth){
    int xmin = 0 , xmax = 0;
    int x = 0 , y = 0;
    for(char d : forth){
        if(d == 'E') xmax = max(xmax , ++x);
        else if(d == 'W') xmin = min(xmin , --x);
        else y++;
    }
    string L = string(x + 1 - xmin , 'W') + string(y , 'S') + string(1 - xmin , 'E');
    string R = string(xmax + 1 - x , 'E') + string(y , 'S') + string(xmax + 1, 'W');
    return L.size() < R.size() ? L : R;
  
}

MS 1
โค1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int>& A, vector<int>& B, int X, int Y) {
    int N = A.size();
    vector<int> dpA(N), dpB(N);

    // Initial condition: Start at position 0 on either line
    dpA[0] = A[0];
    dpB[0] = B[0];

    // Fill the dp arrays
    for (int i = 1; i < N; ++i) {
        dpA[i] = min(dpA[i - 1] + A[i], dpB[i - 1] + Y + A[i]);
        dpB[i] = min(dpB[i - 1] + B[i], dpA[i - 1] + X + B[i]);
    }

    // The result is the minimum time to complete the car on either line at the last position
    return min(dpA[N - 1], dpB[N - 1]);
}

int main() {
    int N, X, Y;
    cin >> N >> X >> Y;
    vector<int> A(N), B(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    for (int i = 0; i < N; ++i) {
        cin >> B[i];
    }
    cout << solution(A, B, X, Y) << endl;
    return 0;
}

Ms task2
๐Ÿ‘1
int solve(vector<int>& sequence) {
    int n = sequence.size();
    vector<int> dp(n + 1, INT_MAX);
    dp[0] = 0;

    for (int i = 0; i < n; ++i) {
        if (dp[i] == INT_MAX) continue;
        int groupSize = sequence[i];
        if (i + groupSize < n) {
            dp[i + groupSize + 1] =min(dp[i + groupSize + 1], dp[i]);
        }
        dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    }

    return dp[n];
}

Uber โœ…
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll minFreq=0;
unordered_map<ll,pair<ll,ll>> keyVal;
unordered_map<ll,list<ll>> freqList;
unordered_map<ll,list<ll>::iterator> pos;
ll get(ll key)
{
    if(keyVal.find(key) == keyVal.end()) return -1;
    freqList[keyVal[key].second].erase(pos[key]);
    keyVal[key].second++;
    freqList[keyVal[key].second].push_back(key);
    pos[key] = --freqList[keyVal[key].second].end();
    if(freqList[minFreq].empty()) minFreq++;
    return keyVal[key].first;
}
void put(ll key, ll value)
{
    if(!n) return;
    if(keyVal.find(key) != keyVal.end())
    {
        keyVal[key].first = value;
        freqList[keyVal[key].second].erase(pos[key]);
        keyVal[key].second++;
        freqList[keyVal[key].second].push_back(key);
        pos[key] = --freqList[keyVal[key].second].end();
        if(freqList[minFreq].empty())
            minFreq++;
        return;
    }
    if(keyVal.size()==n)
    {
        ll delKey = freqList[minFreq].front();
        keyVal.erase(delKey);
        pos.erase(delKey);
        freqList[minFreq].pop_front();
    }
    keyVal[key] = {value,1};
    freqList[1].push_back(key);
    pos[key] = --freqList[1].end();
    minFreq = 1;
}
signed main()
{
    cin>>n;
    ll q; cin>>q;
    while(q--)
    {
        ll x,y,z; cin>>x>>y>>z;
        if(x==1) cout<<get(y)<<" ";
        else put(y,z);
    }
    return 0;
}


Catch it โœ