LeetCode, GeeksForGeeks Problem of the day solution
Open in Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Show more1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution {
public:
int maxSumAfterPartitioning(vector& arr, int k) {
int n = arr.size();
vector dp(n+1, 0);
for(int i=n-1;i>=0;i--){
int maxParti = INT_MIN;
int ans = INT_MIN;
int len = 0;
for(int j=i;j
class Solution
{
public:
// Should return decimal equivalent modulo 1000000007 of binary linked list
long long unsigned int decimalValue(Node *head)
{
// Your Code Here
long long unsigned int ans=0;
Node *temp=head;
while(temp!=NULL)
{
ans=ans<<1;
ans=ans|temp->data;
ans=ans%1000000007;
temp=temp->next;
}
return ans;
}
};
class Solution {
public:
void update_next_num(int &num, int &base_num, int &digit_mask) {
num += digit_mask;
if (num % 10 == 0) {
digit_mask = digit_mask * 10 + 1;
base_num = base_num * 10 + base_num % 10 + 1;
num = base_num;
}
}
vector sequentialDigits(int low, int high) {
vector result;
int len = 1;
while (low / len > 0) {
len *= 10;
}
// Get smallest base
int base_num = 1;
int digit_mask = 1;
while (base_num <= len / 10) {
base_num = base_num * 10 + base_num % 10 + 1;
digit_mask = digit_mask * 10 + 1;
}
// Get smallest number greater than low
int num = base_num;
while (num < low) {
update_next_num(num, base_num, digit_mask);
}
while (num <= high) {
result.push_back(num);
update_next_num(num, base_num, digit_mask);
}
return result;
}
};
class Solution{
public:
/*You are required to complete this method */
int atoi(string s) {
bool neg=0;
int n=s.length();
int ans=0;
for(int i=0; i='0' && s[i]<='9'){
ans=(ans*10)+(s[i]-'0');
}else return -1;
}
return neg?(-1)*ans:ans;
}
};
class Solution {
public:
vector> divideArray(vector& nums, int k) {
sort(nums.begin(),nums.end());
vector> ans;
for(int i=0;i
class Solution
{
public:
//Function to check if a string is Pangram or not.
bool checkPangram (string s) {
// your code here
vector alphabet(26,false);
for(int i = 0; i < s.size(); i++) {
if(s[i] >= 'A' && s[i] <= 'Z') {
char lowercase = (s[i]^(1 << 5));
int index = lowercase - 'a';
alphabet[index] = true;
} else if(s[i] >= 'a' && s[i] <= 'z') {
int index = s[i]-'a';
alphabet[index] = true;
}
}
for(int i = 0; i < 26; i++) {
if(!alphabet[i])
return false;
}
return true;
}
};
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temp) {
int n=temp.size();
stack<int>st;
vector<int>arr(n,0);
for(int i=0;i<n;i++){
while(!st.empty() and temp[st.top()]<temp[i] ){;
int j=st.top();
st.pop();
arr[j]=i-j;
}
st.push(i);
}
// reverse(arr.begin(),arr.end());
return arr;
}
};
class Solution
{
public:
//Function to insert string into TRIE.
void insertWord(struct TrieNode *root, string key){
if(key.size() == 0){
root->isLeaf = true;
return;
}
int index = key[0] - 'a';
struct TrieNode * child;
if(root->children[index] != NULL){
child = root->children[index];
}
else{
child = getNode();
root->children[index] = child;
}
insertWord(child, key.substr(1));
}
void insert(struct TrieNode *root, string key)
{
insertWord(root, key);
}
//Function to use TRIE data structure and search the given string.
bool searchKey(struct TrieNode *root, string key){
if(key.size()==0){
return root->isLeaf;
}
int index = key[0] - 'a';
struct TrieNode * child;
if(root->children[index] !=NULL){
child = root->children[index];
}
else{
return false;
}
return searchKey(child, key.substr(1));
}
bool search(struct TrieNode *root, string key)
{
return searchKey(root, key);
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long>s;
for(string a: tokens){
if(a=="+"){
long val1,val2;
val1=s.top();
s.pop();
val2=s.top();
s.pop();
s.push(val1+val2);
}else if(a=="-"){
int val1,val2;
val1=s.top();
s.pop();
val2=s.top();
s.pop();
s.push(val2-val1);
}else if(a=="*"){
long val1=s.top();
s.pop();
long val2=s.top();
s.pop();
s.push(val1*val2);
}else if(a=="/"){
long val1,val2;
val1=s.top();
s.pop();
val2=s.top();
s.pop();
s.push(val2/val1);
}else{
s.push(stoi(a));
}
}
return (int)s.top();
}
};
class Solution
{
public:
int LCSof3 (string A, string B, string C, int n1, int n2, int n3)
{
int dp[n1+1][n2+1][n3+1];
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n1; i++)
{
for(int j=1; j<=n2; j++)
{
for(int k=1; k<=n3; k++)
{
if (A[i-1] == B[j-1] && B[j-1] == C[k-1])
{
dp[i][j][k]=dp[i-1][j-1][k-1]+1;
}
else
{
dp[i][j][k]=max(max(dp[i-1][j][k], dp[i][j-1][k]),dp[i][j][k-1]);
}
}
}
}
return dp[n1][n2][n3];
}
};
