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:
int getWinner(vector& arr, int k) {
int first=0; int second=1; int count=0;
while(first < arr.size() && second< arr.size()){
if(count==k) return arr[first];
if(arr[first] > arr[second]){
count++;
second++;
}
else{
count=1;
while(first!=second){
first++;
}
first++;
int temp=second;
second= first;
first= temp;
}
}
return arr[first];
}
};
class Solution {
public:
vector<int> topK(vector<int>& nums, int k) {
priority_queue <pair<int,int>> pq;
unordered_map <int,int> map;
vector<int> ans;
for (auto i : nums) {
map[i]++;
}
for (auto i : map) {
pq.push({i.second,i.first});
}
while (k--) {
ans.push_back(pq.top().second);
pq.pop();
}
return ans;
}
};
class Solution {
public:
int getLastMoment(int n, vector& left, vector& right) {
if(right.size()==NULL){
sort(left.begin(),left.end());
return left[left.size()-1];
}
if(left.size()==NULL){
sort(right.begin(),right.end());
return n-right[0];
}
sort(left.begin(),left.end());
sort(right.begin(),right.end());
int mini=right[0];
int maxi=left[left.size()-1];
if(maxi>=abs(mini-n))return maxi;
else return abs(mini-n);
}
};
class Solution
{
public:
int transitionPoint(int arr[], int n) {
for(int i=0;i
class Solution {
public:
vector buildArray(vector& target, int n) {
vectors;
int x=1;
vectorv;
int j=0;
for(int i=1;i<=n;i++){
v.push_back(i);
s.push_back("Push");
if(i==target[j]){
j++;
}
else{
v.pop_back();
s.push_back("Pop");
}
if(v==target) break;
}
return s;
}
};
class Solution{
public:
// Function to check if the
// Pythagorean triplet exists or not
bool checkTriplet(int arr[], int n) {
vectorv(1001,0);
for(int i=0;i
class Solution {
public:
int result = 0;
int traverse(TreeNode* node, int ¤tSum){
if(node == nullptr){
return 0;
}
int temp = currentSum;
currentSum = currentSum + node->val;
int left = 0, right = 0;
if(node->left!=nullptr)
left = 1 + traverse(node->left, currentSum);
if(node->right!=nullptr)
right = 1 + traverse(node->right, currentSum);
int n = left + right;
int avg = int(floor(((currentSum - temp) / ((n+1) * 1.0))));
if(avg == node->val){
result++;
}
return n;
}
int averageOfSubtree(TreeNode* root) {
int sum = 0;
traverse(root, sum);
return result;
}
};
class Solution{
public:
int minDist(int a[], int n, int x, int y) {
int ans=INT_MAX;
int x1=-1;
int y1=-1;
for(int i=0;i
class Solution {
public:
vectorans;
int mx_freq=0;
unordered_mapmp;
void dfs(TreeNode* root){
if(root==NULL) return;
int f= mp[root->val]++;
if(f>mx_freq)
{mx_freq=f;
ans={root->val};
}
else if(f==mx_freq) ans.push_back(root->val);
dfs(root->left);
dfs(root->right);
}
vector findMode(TreeNode* root) {
dfs(root);
return ans;
}
};
class Solution{
public:
//Function to count the frequency of all elements from 1 to N in the array.
void frequencyCount(vector& arr,int N, int P)
{
unordered_mapmp(P);
for(int i=0;i
