allcoding1
26.7K subscribers
2.2K photos
2 videos
77 files
865 links
Download Telegram
allcoding1
Photo
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int numDecodings(string msg) {
    int MOD = 1000000007;
    int n = msg.size();
   
    vector<long long> dp(n + 1, 0);
    dp[0] = 1;
   
    if (msg[0] == '0')
        dp[1] = 0;
    else if (msg[0] == '*')
        dp[1] = 9;
    else
        dp[1] = 1;
   
    for (int i = 2; i <= n; ++i) {
        if (msg[i - 1] == '0') {
           
            if (msg[i - 2] == '1' || msg[i - 2] == '2')
                dp[i] += dp[i - 2];
            else if (msg[i - 2] == '*')
                dp[i] += 2 * dp[i - 2];
        } else if (msg[i - 1] >= '1' && msg[i - 1] <= '9') {
     
            dp[i] += dp[i - 1];
           
            if (msg[i - 2] == '1' || (msg[i - 2] == '2' && msg[i - 1] <= '6'))
                dp[i] += dp[i - 2];
            else if (msg[i - 2] == '*') {
               
                if (msg[i - 1] <= '6')
                    dp[i] += 2 * dp[i - 2];
                else
                    dp[i] += dp[i - 2];
            }
        } else if (msg[i - 1] == '*') {
            dp[i] += 9 * dp[i - 1];
           
            if (msg[i - 2] == '1')
                dp[i] += 9 * dp[i - 2];
            else if (msg[i - 2] == '2')
                dp[i] += 6 * dp[i - 2];
            else if (msg[i - 2] == '*')
                dp[i] += 15 * dp[i - 2];
        }
       
        dp[i] %= MOD;
    }
   
    return dp[n];
}

Number of ways decode


Accenture exam

Telegram:- @allcoding1
👍1
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::string stemmer(const std::string& text) {
    std::stringstream ss(text);
    std::string word;
    std::vector<std::string> stemmed_words;

    while (ss >> word) {
        if (word.size() > 2 && (word.substr(word.size() - 2) == "ed" word.substr(word.size() - 2) == "ly" word.substr(word.size() - 3) == "ing")) {
            word = word.substr(0, word.size() - 2);
        }
        if (word.size() > 8) {
            word = word.substr(0, 8);
        }
        stemmed_words.push_back(word);
    }

    std::string result;
    for (const std::string& stemmed_word : stemmed_words) {
        result += stemmed_word + " ";
    }


    result.pop_back();
    return result;
}

int main() {
    std::string text = "an extremely dangerous dog is barking";
    std::cout << stemmer(text) << std::endl;  // Output: "an extreme dangerou dog is bark"

    return 0;
}

Suffix stripping stemmer

Telegram:- @allcoding1
👍1
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

long getkRepValue(string user_history, long k) {
    long n = user_history.size();
    unordered_map<char, long> count;
    long left = 0, right = 0, ans = 0;

    while (right < n) {
        count[user_history[right]]++;
        while (count[user_history[right]] >= k && left <= right) {
            ans += n - right;
            count[user_history[left]]--;
            left++;
        }
        right++;
    }

    return ans;


Machine learning
Amazon
👍2
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

int getMinTransactions(int n, vector<vector<int>>& debt) {
    unordered_map<int, int> balance
    for (const auto& d : debt) {
        balance[d[0]] -= d[2];
        balance[d[1]] += d[2];
    }
   
    vector<int> transactions;
    for (const auto& entry : balance) {
        if (entry.second != 0) {
            transactions.push_back(entry.second);
        }
    }

    int count = 0;
    int i = 0, j = transactions.size() - 1;
    while (i < j) {
        if (transactions[i] + transactions[j] == 0) {
            count++;
            i++;
            j--;
        } else if (transactions[i] + transactions[j] > 0) {
            transactions[i] += transactions[j];
            j--;
            count++;
        } else {
            transactions[j] += transactions[i];
            i++;
            count++;
        }
    }
    return count;
}

Transaction Simplification
#include<bits/stdc++.h>
using namespace std;
string str, bad_string;
struct node{
    bool end_mark;
    node *next[10];
    node()
    {
        end_mark = false;
        for(int i = 0; i<10; i++)
            next[i] = NULL;
    }
}*root;
bool add(string s)
{
    node *current = root;
    for(int i = 0; i<s.size(); i++)
    {
        int nw = s[i] - 'a';
        if(i == (s.size()-1) && current->next[nw] != NULL)
            return false;
        if(current->next[nw] == NULL)
            current->next[nw] = new node();
        current = current->next[nw];
        if(current->end_mark)
            return false;
    }
    current->end_mark = true;
    return true;
}

int main()
{
    int i, N;
    bool ok = true;
    cin >> N;
    root = new node();
    for(i = 1; i<=N; i++)
    {
        cin >> str;
        if(!ok)
            continue;
        ok = add(str);
        if(!ok)
            bad_string = str;
    }
    if(ok)
        printf("GOOD SET\n");
    else
    {
        printf("BAD SET\n");
        cout << bad_string << endl;
    }
}

Good Bad String
Uber
👍4
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

long long solution(int n, vector<long long>& a, vector<long long>& b) {
    long long ans = 0;

    int minP = min_element(a.begin(), a.end()) - a.begin();
    int minQ = min_element(b.begin(), b.end()) - b.begin();

    for (int i = 0; i < n; i++) {
        if (i == minP || i == minQ)
            continue;

        ans += min(a[minP] * b[i], b[minQ] * a[i]);
        ans %= MOD;
    }

    if (minP != minQ) {
        ans += a[minP] * b[minQ];
        ans %= MOD;
    }

    return ans;
}.

professor code
Uber
👍2
#include<bits/stdc++.h>
using namespace std;

int main() {
    int n,c,d ;
    cin>>n>>c>>d ;
    int b[n],p[n],t[n] ;
    for(int i=0;i<n;i++)
        cin>>b[i]>>p[i]>>t[i] ;
    vector<pair<int,int>>type_0,type_1 ;
    for(int i=0;i<n;i++)
    {
        if(t[i]==0)
        {
            type_0.push_back({p[i],b[i]}) ;
        }
        else
        {
            type_1.push_back({p[i],b[i]}) ;
        }
    }
    sort(type_0.begin(),type_0.end()) ;
    sort(type_1.begin(),type_1.end()) ;
    // One using coins and one using diamonds
    int max_0=0,max_1=0 ;
    int x=type_0.size() ;
    int y=type_1.size() ;
    for(int i=0;i<x;i++)
    {
        if(type_0[i].first<=c)
            max_0=max(max_0,type_0[i].second) ;
    }
    for(int i=0;i<y;i++)
    {
        if(type_1[i].first<=d)
            max_1=max(max_1,type_1[i].second) ;
    }
    int ans=0 ;
    if(max_0&&max_1)
        ans=max(ans,max_0+max_1) ;  
    // Both using coins
    multiset<int>m;                        
    for(int i=0;i<x;i++)
    {
       m.insert(type_0[i].second) ;
    }
    int j=type_0.size()-1 ;
    for(int i=0;i<x;i++)
    {
        if(j<=i)
            break ;
        auto it=m.find(type_0[i].second) ;
        m.erase(it) ;
        int flag=0 ;   
        while(j>i)
        {
            if(type_0[j].first+type_0[i].first<=c)
            {
                flag=1 ;
                break ;
            }
            auto it=m.find(type_0[j].second) ;
            m.erase(it) ;
            j-- ;
        }
        if(flag==0)
            break ;
        if(m.size())
        ans=max(ans,type_0[i].second+*m.rbegin()) ;  
    }
    // Both using diamonds
    m.clear() ;
    for(int i=0;i<y;i++)
    {
       m.insert(type_1[i].second) ;
    }
     j=type_1.size()-1 ;
    for(int i=0;i<y;i++)
    {
        if(j<=i)
            break ;
        auto it=m.find(type_1[i].second) ;
        m.erase(it) ;
        int flag=0 ;
        while(j>i)
        {
            if(type_1[j].first+type_1[i].first<=d)
            {
                flag=1 ;
                break ;
            }
            auto it=m.find(type_1[j].second) ;
            m.erase(it) ;
            j-- ;
        }
        if(flag==0)
            break ;
        if(m.size())
        ans=max(ans,type_1[i].second+*m.rbegin()) ;
    }
    cout<<ans<<"\n" ;
    return 0;
}

Uber
👍2
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

int minimum_energy_cost_recursive(int index,int idxy, int left_consecutive, int right_consecutive, int N, int X, int Y, int El, int Er, vector<int>& weights) {
    // Base case: if we have reached the end of bags
    if (index > idxy) {
        return 0;
    }
   
    // Calculate the cost of picking from the left and the right
    int cost_left = weights[index] * X + (left_consecutive ? El : 0) +
                    minimum_energy_cost_recursive(index + 1,idxy, 1, 0, N, X, Y, El, Er, weights);
    int cost_right = weights[idxy] * Y + (right_consecutive ? Er : 0) +
                     minimum_energy_cost_recursive(index ,idxy-1, 0, 1, N, X, Y, El, Er, weights);
   
    // Return the minimum of both choices
    return min(cost_left, cost_right);
}

int main() {
    int N, X, Y, El, Er;
    cin >> N >> X >> Y >> El >> Er;
   
    vector<int> weights(N);
    for (int i = 0; i < N; ++i) {
        cin >> weights[i];
    }
   
    cout << minimum_energy_cost_recursive(0,N-1, 0, 0, N, X, Y, El, Er, weights) << endl;
   
    return 0;
}

Amazon Hackon exam
👍6🤯1
//Equilibrium path


#include <bits/stdc++.h>

using namespace std;

string trim(string str) {
str.erase(0, str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ') + 1);
return str;
}

int solve(int N, vector<int> A) {
int total_sum = accumulate(A.begin(), A.end(), 0);
int left_sum = 0;
int equilibrium_count = 0;

for (int i = 0; i < N; ++i) {
int right_sum = total_sum - left_sum - A[i];
if (left_sum == right_sum) {
equilibrium_count++;
}
left_sum += A[i];
}

return equilibrium_count;
}

int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

string inputline;
getline(cin, inputline);
int N = stoi(trim(inputline));

vector<int> A(N);
for (int j = 0; j < N; j++) {
getline(cin, inputline);
A[j] = stoi(trim(inputline));
}

int result = solve(N, A);
cout << result << endl;

return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void dfs(int node, int parent, const vector<vector<int>> &adj, const vector<int> &A, int length, int &maxLength) {
maxLength = max(maxLength, length);
for (int neighbor : adj[node]) {
if (neighbor == parent) continue;
if ((A[node] ^ A[neighbor]) < min(A[node], A[neighbor])) {
dfs(neighbor, node, adj, A, length + 1, maxLength);
}
}
}

int main() {
int N;
cin >> N;
vector<int> A(N), P(N);
for (int i = 0; i < N; ++i) cin >> A[i];
for (int i = 1; i < N; ++i) cin >> P[i];

vector<vector<int>> adj(N);
for (int i = 1; i < N; ++i) {
int parent = P[i];
adj[parent].push_back(i);
adj[i].push_back(parent);
}

int maxLength = 0;
dfs(0, -1, adj, A, 1, maxLength);

cout << maxLength << endl;
return 0;
}
Nodes
👍1