-Shaurya Awasthi
**class Solution {
public:
int dfs(int x,int y,vector<vector<int>>& grid, int m,int n)
{
if(x<0||y<0||x>=m||y>=n||grid[x][y]==0)return 0;
int curr=grid[x][y];
grid[x][y]=0;
int a[]={0,1,-1,0};
int b[]={1,0,0,-1};
int gold=curr;
for(int i=0;i<4;i++)
{
int nr=x+a[i];
int nc=y+b[i];
gold=max(gold,curr+dfs(nr,nc,grid,m,n));
}
grid[x][y]=curr;
return gold;
}
int getMaximumGold(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int maxGold=INT_MIN;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
maxGold=max(maxGold,dfs(i,j,grid,m,n));
}
}
return maxGold;
}
};**
class Solution {
public:
long long maximumHappinessSum(vector<int>& h, int k) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
priority_queue<long long >pq;
for(int i=0;i<h.size();i++)
{
pq.push(h[i]);
}
long long sum=0;
int i=0;
while(i<k)
{
int x=pq.top()-i;
sum+= x>=0?x:0;
pq.pop();
i++;
}
return sum;
}
};
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n=score.size();
vector<string>ans(n);
unordered_map<int ,int>idx;
for(int i=0;i<n;i++)idx[score[i]]=i;
sort(score.begin(),score.end());
for(int i=0;i<n;i++)
{
int rank=n-i;
if(rank==1)
{
ans[idx[score[i]]]=("Gold Medal");
}
else if(rank==2)
{
ans[idx[score[i]]]=("Silver Medal");
}
else if(rank==3)
{
ans[idx[score[i]]]=("Bronze Medal");
}
else
{
ans[idx[score[i]]]=to_string(rank);
}
}
return ans;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* doubleIt(ListNode* head) {
if(head->val>4)
{
head=new ListNode(0,head);
}
ListNode* temp=head;
while(temp)
{
temp->val=(temp->val*2)%10;
if(temp->next && temp->next->val>4)
{
temp->val++;
}
temp=temp->next;
}
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNodes(ListNode* head) {
if(head==nullptr)return nullptr;
head->next=removeNodes(head->next); // head recursion
if(head->next && head->val<head->next->val)
{
return head->next;
}
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
swap(node->val,node->next->val);
node->next=node->next->next;
}
};
class Solution {
public:
int compareVersion(string v1, string v2) {
int n1=v1.size(),n2=v2.size();
int i=0,j=0,x1=0,x2=0;
while(i<n1 || j<n2)
{
while(i<n1&&v1[i]!='.')
{
x1=x1*10+(v1[i]-'0');
i++;
}
while(j<n2&&v2[j]!='.')
{
x2=x2*10+(v2[j]-'0');
j++;
}
if(x1>x2)return 1;
if(x2>x1)return -1;
x1=0,x2=0;
i++;
j++;
}
return 0;
}
};
class Solution {
public:
int findMaxK(vector<int>& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());
int neg=0,pos=n-1;
while(neg<pos && nums[neg]<0)
{
if(abs(nums[neg])==nums[pos])return nums[pos];
else if(abs(nums[neg])>nums[pos]) neg++;
else pos--;
}
return -1;
}
};