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
#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
heights = [int(i) for i in input().split()]
freq_dict = {}
for h in heights:
if h in freq_dict:
freq_dict[h] += 1
else:
freq_dict[h] = 1
for h in sorted(freq_dict):
print(h, freq_dict[h])
Accenture exam
Telegram:- @allcoding1
freq_dict = {}
for h in heights:
if h in freq_dict:
freq_dict[h] += 1
else:
freq_dict[h] = 1
for h in sorted(freq_dict):
print(h, freq_dict[h])
Accenture exam
Telegram:- @allcoding1
#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
#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
class Message(object):
def init(self, message: str, sender: int, receiver: int) -> None:
self.message = message
self.sender = sender
self.receiver = receiver
def str(self) -> str:
return self.message
def eq(self, other: object) -> bool:
if not isinstance(other, Message):
return False
return self.message == other.message
Message Objects
Telegram:- @allcoding1
def init(self, message: str, sender: int, receiver: int) -> None:
self.message = message
self.sender = sender
self.receiver = receiver
def str(self) -> str:
return self.message
def eq(self, other: object) -> bool:
if not isinstance(other, Message):
return False
return self.message == other.message
Message Objects
Telegram:- @allcoding1
🎯Infosys Recruitment Drive 2024 | Hiring For Process Executive , Salary Up to ₹3.5 LPA
Degree:- Any Bachelor's
Batch:- 2021, 2022, 2023 & 2024
Apply now:- www.allcoding1.com
Degree:- Any Bachelor's
Batch:- 2021, 2022, 2023 & 2024
Apply now:- www.allcoding1.com
👍2
#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
#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
int solve(vector<int> tc) {
int n = tc.size();
vector<int> p(n), s(n);
p[0] = tc[0];
for (int i = 1; i < n; ++i) {
p[i] = p[i - 1] + tc[i];
}
s[n - 1] = tc[n - 1];
for (int i = n - 2; i >= 0; --i) {
s[i] = s[i + 1] + tc[i];
}
int m = max(p[0], s[0]);
for (int i = 1; i < n; ++i) {
m = max(m, max(p[i], s[i]));
}
return m;
}
Amazon
int n = tc.size();
vector<int> p(n), s(n);
p[0] = tc[0];
for (int i = 1; i < n; ++i) {
p[i] = p[i - 1] + tc[i];
}
s[n - 1] = tc[n - 1];
for (int i = n - 2; i >= 0; --i) {
s[i] = s[i + 1] + tc[i];
}
int m = max(p[0], s[0]);
for (int i = 1; i < n; ++i) {
m = max(m, max(p[i], s[i]));
}
return m;
}
Amazon
👍3
#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 <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
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
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
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