๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.52K subscribers
5.56K photos
3 videos
95 files
9.7K 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
from collections import defaultdict 
import sys

def parse_recipes(n, recipes):
    graph = defaultdict(list)
    for recipe in recipes:
        potion, ingredients = recipe.split('=')
        graph[potion].append(ingredients.split('+'))
    return graph

def min_orbs(target, graph, cache):
    if target in cache:
        return cache[target]
    if target not in graph:
        cache[target] = 0
        return 0

    min_cost = sys.maxsize
    for recipe in graph[target]:
        cost = len(recipe) - 1
        for ingredient in recipe:
            cost += min_orbs(ingredient, graph, cache)
        min_cost = min(min_cost, cost)

    cache[target] = min_cost
    return min_cost

n = int(input())
recipes = [input().strip() for _ in range(n)]
target_potion = input().strip()

graph = parse_recipes(n, recipes)
cache = {}
result = min_orbs(target_potion, graph, cache)

print(result, end="")

Frenkitsen
TCS Codevita โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((int)a.size())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long

typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;


struct Flight {
    string src;
    string to;
    int dt; 
    int at;   
    int cost;
};

int parse_time(const string& time_str) {
    int hour = stoi(time_str.substr(0, 2));
    int minute = stoi(time_str.substr(3, 2));
    string am_pm = time_str.substr(5, 2);

    if (am_pm == "Am") {
        if (hour == 12) hour = 0;
    } else if (am_pm == "Pm") {
        if (hour != 12) hour += 12;
    }

    return hour * 60 + minute;
}

void solve()
{
    int N;
    cin >> N;
    vector<Flight> fff(N);
    unordered_map<string, vector<Flight>> mp;

    for (int i = 0; i < N; ++i) {
        string src, to, des, arrival_str;
        int cost;
        cin >> src >> to >> des >> arrival_str >> cost;
        int dt = parse_time(des);
        int at = parse_time(arrival_str);
        Flight ff = {src, to, dt, at, cost};
        fff[i] = ff;
        mp[src].push_back(ff);
    }

    string src, des;
    cin >> src >> des;
    string edes, last;
    cin >> edes >> last;
    int earliest_dt = parse_time(edes);
    int latest_at = parse_time(last);

    typedef tuple<int, int, string> State;
    priority_queue<State, vector<State>, greater<State>> pq;

    for (const auto& ff : mp[src]) {
        if (ff.dt >= earliest_dt) {
            if (ff.at <= latest_at) {
                pq.emplace(ff.cost, ff.at, ff.to);
            }
        }
    }

    unordered_map<string, int> ans; 
    unordered_map<string, int> at; 

    while (!pq.empty()) {
        int cost_so_far, current_at;
        string current_city;
        tie(cost_so_far, current_at, current_city) = pq.top();
        pq.pop();

        if (ans.find(current_city) != ans.end()) {
            if (cost_so_far > ans[current_city]) continue;
            if (cost_so_far == ans[current_city] && current_at >= at[current_city]) continue;
        }

        ans[current_city] = cost_so_far;
        at[current_city] = current_at;

        if (current_city == des) {
            cout << cost_so_far;
            return ;
        }

        for (const auto& ff : mp[current_city]) {
            if (ff.dt >= current_at) {
                if (ff.dt >= earliest_dt && ff.at <= latest_at) {
                    int new_cost = cost_so_far + ff.cost;
                    pq.emplace(new_cost, ff.at, ff.to);
                }
            }
        }
    }

    cout << "Impossible";
    return ;
}


int32_t main()
{

    int tc = 1;
    // cin >> tc;
    while (tc--)
    {
        solve();
    }
    return 0;
}

Flight optimizationโœ…
def solve(skills, p):
    n = len(skills)
    if n < 2 or p < 1 or p > (n * (n-1)) // 2:
        return []
    skills.sort()
    from heapq import heappush, heappop
    inefficiencies = []
    for i in range(n-1):
        for j in range(i+1, n):
            inefficiency = abs(skills[i] - skills[j])
            heappush(inefficiencies, inefficiency)
    result = []
    for _ in range(p):
        if inefficiencies:
            result.append(heappop(inefficiencies))
    return result

Amazon โœ…
long getMaxRequests(vector<int>& sc, vector<int>& ir, int k) {
    int n = sc.size();
    long total = 0;
    vector<int> extra(n, 0);

    for (int i = 0; i < n; i++) {
        int h = min(sc[i], ir[i]);
        total += h;

        if (sc[i] < ir[i]) {
            int dch = min(2 * sc[i], ir[i]);
            extra[i] = dch - h;
        }
    }

    sort(extra.rbegin(), extra.rend());

    for (int i = 0; i < k; i++) {
        total += extra[i];
    }

    return total;
}

Server Management โœ…
def numPaths(warehouse):
    paths = [[0]*len(warehouse[0]) for i in warehouse]
    if warehouse[0][0] == 1:
        paths[0][0] = 1
    for i in range(1, len(warehouse)):
        if warehouse[i][0] == 1:
            paths[i][0] = paths[i-1][0]
    for j in range(1, len(warehouse[0])):
        if warehouse[0][j] == 1:
            paths[0][j] = paths[0][j-1]
          
    for i in range(1, len(warehouse)):
        for j in range(1, len(warehouse[0])):
            if warehouse[i][j] == 1:
                paths[i][j] = paths[i-1][j] + paths[i][j-1]
    return paths[-1][-1]%(10**9+7)


path in warehouse โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
  using namespace std;
  long long dfs(long long src,long long par,vector<long long> adj[],vector<long long> &sum,vector<long long> &depth,vector<long long> &a,vector<long long> &c)
  {
    if(a[src]!=0)
    {
      depth[src]=0;
      sum[src]=a[src];
      return a[src];
    }
    long long maxi=0;
    for(auto x:adj[src])
    {
      if(x!=par)
      {
        long long temp=dfs(x,src,adj,sum,depth,a,c);
        sum[src]+=(temp-depth[x]);
        depth[src]=depth[x]+1;
        maxi=max(maxi,temp);
      }
    }
    return (maxi + min(c[src],sum[src]-(maxi-depth[src])));
  }
  int main() {

    long long n;
    cin>>n;
    vector<long long> a(n+1);
    vector<long long> c(n+1);
   
    for(long long i=0;i<n;i++)
    cin>>a[i+1];
    for(long long i=0;i<n;i++)
    cin>>c[i+1];
   
    vector<long long> adj[n+1];
    for(long long i=1;i<n;i++)
    {
      long long u,v;
      cin>>u>>v;
     
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    vector<long long> sum(n+1,0);
    vector<long long> depth(n+1);
    cout<<dfs(1,0,adj,sum,depth,a,c);

    return 0;

  }

fruits on treeโœ…
Media. Net
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll M = 1e9+7 ;

int main() {

    ll n;cin>>n ;
    vector<ll> v(31) ;
    for(ll i = 1;i<=n;i++){
      ll x;cin>>x ;
      v[x]++ ;
    }
    vector<ll> p ;
    for(ll i = 2;i<=30;i++){
      bool ch = 1 ;
      for(ll j = 2;j*j<=i;j++){
        if(i%(j*j)==0)ch = 0 ;
      }
      if(ch)p.push_back(i) ;
    }
    ll one = 1 ;
    while(v[1]){
      one = one*2%M ;
      v[1]-- ;
    }
    n = p.size() ;
    ll ans = 0 ;
    for(ll i = 1;i<(1ll<<n);i++){
      ll pro = 1 ;
      ll tot = 1 ;
      bool ch = 1 ;
     
      for(ll j =0;j<n;j++){
        if((1ll<<j)&i){
          if(__gcd(pro,p[j])!=1){
            ch = 0 ;
            break;
          }
          pro = pro*p[j] ;
          tot = tot*v[p[j]]%M ;
        }
      }
     
      tot = tot*(one)%M ;
      if(ch){
        ans += tot ;
        ans %= M ;
      }
    }
    cout<<(ans+M)%M<<endl;
   

    return 0;

  }

  }
omega prime
media .netโœ…
from collections import deque
def timeOfBuffering(arrivalRate, packets):
    buffer = deque()
    current = 1
    time = 0

    for i in range(0, len(packets), arrivalRate):
        time += 1

        for j in range(i, min(i + arrivalRate, len(packets))):
            packet = packets[j]
            if packet != current:
                buffer.append(packet)

        if current in buffer:
            buffer.remove(current)
        elif current == packets[i]:
            pass
        else:
            return time

        current += 1

    return -1


Video Buffering โœ…
def maxProfit(cost, x):
    ans = 0
    mod = 10 ** 9 + 7
    for i in range(len(cost) - 1, -1, -1):
        if cost[i] <= x:
            x -= cost[i]
            ans = (ans + pow(2, i, mod)) % mod
    return ans