LeetCode, GeeksForGeeks Problem of the day solution
الذهاب إلى القناة على Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
إظهار المزيد1 250
المشتركون
+224 ساعات
+147 أيام
+2930 أيام
أرشيف المشاركات
class Solution {
public:
int integerBreak(int n) {
if(n==2 || n==3) return n-1;
int res =1;
while(n>4){
n-=3;
res*=3;
}
return res * n;
}
};
class Solution
{
public:
void rearrange(struct Node *odd)
{
Node*i = odd;
Node*j = odd->next;
if(!j || j->next==NULL){return;}
Node*prevJ = NULL;
while(i!=NULL and j!=NULL){
i->next = j->next;
i = j->next;
j->next = prevJ;
prevJ = j;
if(i!=NULL){
if(i->next){j=i->next;}
else{i->next = j;break;}
}
else{
break;
}
if(j->next==NULL){
j->next = prevJ;
break;
}
}
}
};
class Solution {
public:
vector majorityElement(vector& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
if(n==1){
return nums;
}
else if(n==2 && nums[0] != nums[1]){
return nums;
}
vector ans;
int cnt = 0;
int temp = n/3;
for(int i=0;i0 && nums[i] == nums[i-1]){
cnt++;
if(cnt > temp && find(ans.begin(), ans.end(), nums[i]) == ans.end()){
ans.push_back(nums[i]);
}
}
else{
cnt = 1;
}
}
return ans;
}
};
class Solution
{
public:
long long int solve(string s, int k)
{
int n = s.size();
long long int c = 0;
int j = 0, sz = 0;
vectormp(26, 0);
for(int i=0; i k)
{
mp[s[j]-'a']--;
if(!mp[s[j]-'a']) sz--;
j++;
}
if(j <= i) c += (i-j+1);
}
return c;
}
long long int substrCount (string s, int k) {
//code here.
return solve(s, k) - solve(s, k-1);
}
};
class MyHashMap {
public:
unordered_map mp;
MyHashMap() {
mp.clear();
}
void put(int key, int value) {
mp[key] = value;
}
int get(int key) {
if(mp.find(key) != mp.end()){
return mp[key];
}
return -1;
}
void remove(int key) {
mp.erase(key);
}
};
class Solution {
public:
int romanToDecimal(string &str) {
unordered_mapmp;
mp['I']=1;
mp['V']=5;
mp['X']=10;
mp['L']=50;
mp['C']=100;
mp['D']=500;
mp['M']=1000;
int sum=0;
int pre=0;
for(int i=str.length()-1;i>=0;i--)
{
if(mp[str[i]] < pre) sum-=mp[str[i]];
else sum+=mp[str[i]];
pre=mp[str[i]];
}
return sum;
}
};
class Solution {
public:
int numIdenticalPairs(vector& nums) {
int ans = 0;
unordered_map f;
for (int& x : nums)
ans += f[x]++;
return ans;
}
};
class Solution{
public:
string colName (long long int n)
{
string s="";
while(n!=0){
n=n-1;
long long int first=n/26;
long long int second=(n+1)-(26*first);
s +='A'+second-1;
n=first;
}
reverse(s.begin() , s.end());
return s;
}
};
class Solution {
public:
bool winnerOfGame(string colors) {
int n = colors.size();
int a = 0,b = 0;
for(int i=0;ib)return true;
return false;
}
};
class Solution{
public:
int distinctSubsequences(string s)
{
int n=s.size();
long long mod=1e9+7;
long long arr[26]={0},pre=1,cur=1;
for(int i=0;i
