LeetCode, GeeksForGeeks Problem of the day solution
Ir al canal en Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Mostrar más1 250
Suscriptores
+224 horas
+147 días
+2930 días
Archivo de publicaciones
class Solution {
public:
vector jugglerSequence(int n) {
vectorans;
ans.push_back(n);
while(n >= 1){
if(ans[ans.size()-1]==1){
break;
}
double t = pow(ans[ans.size()-1],0.5);
double t1 = t*t*t;
if(ans[ans.size()-1] % 2 == 0){
ans.push_back(t);
}
else{
ans.push_back(t1);
}
if(ans[ans.size()-1]==1){
break;
}
}
return ans;
}
};
class Solution {
public:
vector kthSmallestPrimeFraction(vector& arr, int k) {
vector answer;
int n = arr.size();
double left = 0, right = 1;
while (right - left > 1e-9) {
double mid = left + (right - left) / 2;
int count = 0, p = -1, q = -1, j = 1;
for (int i = 0; i < n - 1; ++i) {
while (j < n && arr[i] > mid * arr[j]) ++j;
count += n - j;
if (j < n && (p == -1 || arr[p] * arr[j] < arr[q] * arr[i])) {
p = i;
q = j;
}
}
if (count < k) left = mid;
else right = mid;
if (count == k) answer = {arr[p], arr[q]};
}
return answer;
}
};
class Solution{
public:
vector> ans;
void solve(vector& arr, int index, vector& path ,long long sum, int k)
{
if(sum >= k)
{
if(sum == k)
ans.push_back(path);
return;
}
if(index >= arr.size())
{
return;
}
path.push_back(arr[index]);
solve(arr, index + 1, path, sum + arr[index], k);
path.pop_back();
while(index + 1 < arr.size() && arr[index] == arr[index + 1])
index++;
solve(arr, index + 1, path, sum, k);
}
vector> CombinationSum2(vector arr,int n,int k)
{
sort(arr.begin(), arr.end());
vector path;
long long sum = 0;
solve(arr, 0, path, sum, k);
return ans;
}
};
class Solution {
public:
long long maximumHappinessSum(vector& happiness, int k) {
sort(happiness.begin(),happiness.end());
int n = happiness.size();
int i = 0 ;
long long ans = 0 ;
while(i < k){
if(happiness[n-i-1]-i >= 0){
ans += happiness[n-i-1] - i ;
}
i++ ;
}
return ans ;
}
};
class Solution {
public:
bool divisorGame(int n) {
return !(n%2);
}
};
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
priority_queue<int> pq;
map<int,int> mp;
for (int i=0; i<score.size(); i++){
mp[score[i]]= i;
pq.push(score[i]);
}
int i=1;
vector<string> ans(score.size(),"");
while (!pq.empty()){
int curr= mp[pq.top()];
pq.pop();
if (i==1){
ans[curr]= "Gold Medal";
}else if (i==2){
ans[curr]= "Silver Medal";
}else if (i==3){
ans[curr]= "Bronze Medal";
}else{
ans[curr]= to_string(i);
}
i++;
}
return ans;
}
};
class Solution {
public:
vector> ans;
void solve(Node*root,vector&temp){
if(root==NULL) return ;
if(root->left==NULL and root->right==NULL){
temp.push_back(root->data);
ans.push_back(temp);
temp.pop_back();
return;
}
temp.push_back(root->data);
solve(root->left,temp);
solve(root->right,temp);
if(!temp.empty())
temp.pop_back();
}
vector> Paths(Node* root) {
// code here
vector temp;
solve(root,temp);
return ans;
}
};
class Solution {
public:
ListNode* doubleIt(ListNode* head) {
ListNode* curr = head;
ListNode* prev = NULL;
while(curr!=NULL){
int newVal = curr->val * 2;
curr->val = newVal<10 ? newVal : newVal % 10;;
int carry = newVal>=10 ? 1 : 0;
if(prev == NULL && carry >0){
ListNode* newHead = new ListNode(1);
newHead->next = head;
prev = head;
head = newHead;
}
else if(prev!=NULL){
prev->val+=carry;
}
prev = curr;
curr = curr->next;
}
return head;
}
};
vector reverseLevelOrder(Node *root)
{
vector ans;
queue q;
q.push(root);
while(!q.empty())
{
Node * cur = q.front();
q.pop();
ans.push_back(cur->data);
if(cur->right!=nullptr)
{
q.push(cur->right);
}
if(cur->left!=nullptr)
{
q.push(cur->left);
}
}
reverse(ans.begin(),ans.end());
return ans;
}
class Solution {
public:
ListNode* reverse(ListNode* head){
ListNode* pre=NULL;
ListNode* temp=head;
while(temp!=NULL){
head=temp->next;
temp->next=pre;
pre=temp;
temp=head;
}
return pre;
}
ListNode* removeNodes(ListNode* head) {
head=reverse(head);
ListNode* temp=head;
while(temp->next){
if(temp->next->valval){
temp->next=temp->next->next;
}else{
temp=temp->next;
}
}
head=reverse(head);
return head;
}
};
