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:
double findMedianSortedArrays(vector& nums1, vector& nums2) {
// Combine nums1 and nums2 into a single sorted array
vector combined;
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) {
combined.push_back(nums1[i]);
i++;
} else {
combined.push_back(nums2[j]);
j++;
}
}
// Add any remaining elements from nums1 and nums2
while (i < nums1.size()) {
combined.push_back(nums1[i]);
i++;
}
while (j < nums2.size()) {
combined.push_back(nums2[j]);
j++;
}
// Compute the median based on the size of the combined array
int n = combined.size();
if (n % 2 == 0) {
// If the combined array has an even number of elements, average the middle two elements
return (combined[n / 2 - 1] + combined[n / 2]) / 2.0;
} else {
// If the combined array has an odd number of elements, return the middle element
return combined[n / 2];
}
}
};
class Solution
{
public:
//Function to find the maximum money the thief can get.
int solve(int index, int nums[], vector& dp){
if(index<0){
return 0;
}
if(index==0){
return nums[0];
}
if(dp[index] != -1){
return dp[index];
}
int notSteal = 0 + solve(index-1, nums, dp);
int steal = nums[index] + solve(index-2, nums, dp);
return dp[index] = max(steal, notSteal);
}
int FindMaxSum(int arr[], int n)
{
vector dp(n, -1);
return solve(n-1, arr, dp);
}
};
class Solution {
public:
int minOperations(vector& nums, int x) {
int n = nums.size();
int totalSum=0;
for(auto i:nums)
{
totalSum+=i;
}
if(totalSumrequiredSum)
{
currSum-=nums[i++];
}
if(currSum==requiredSum)
{
maxi=max(j-i+1,maxi);
}
j++;
}
return maxi>0? n-maxi:-1;
}
};
class Solution
{
public:
vector rotate (int n, int d)
{
d=d%16;
int mask=(1<<16)-1;
int x=(n<>(16-d))&mask;
int y=(n>>d|n<<(16-d))&mask;
return {x,y};
}
};
class Solution
{
public:
//Function to find position of first set bit in the given number.
unsigned int getFirstSetBit(int n)
{
unsigned int cnt=1;
while(n>0){
if(n&1){
return cnt;
}
n = n>>1;
cnt++;
}
return 0;
}
};
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
vector<int>ans;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
for(int i = 0; i<mat.size(); i++)
{
pair<int, int>p = {0, 0};
for(int j = 0; j<mat[0].size(); j++)
{
if(mat[i][j] == 1)
{
p.first++;
}
p.second = i;
}
pq.push(p);
}
while(pq.size() && k>0)
{
ans.push_back(pq.top().second);
pq.pop();
k--;
}
return ans;
}
};
class Solution{
public:
// Function to check if given number n is a power of two.
bool isPowerofTwo(long long n){
return (n==0 || n&(n-1)) == 0;
}
};
class Solution {
public:
int shortestPathLength(vector>& adj) {
int n = adj.size();
int end = (1<> vis(1<(n, false));
queue> q;
int ans = 0;
for (int i = 0; i < n; ++i) {
int m = 0;
m |= 1 << i;
q.push({m, i});
vis[m][i] = true;
}
while(!q.empty()) {
int k = q.size();
while(k--) {
auto[set, node] = q.front();
q.pop();
if (set == end) {
return ans;
}
for (int i = 0; i < adj[node].size(); ++i) {
int m = set;
m |= (1 << adj[node][i]);
if (!vis[m][adj[node][i]]) {
q.push({m, adj[node][i]});
vis[m][adj[node][i]] = true;
}
}
}
++ans;
}
return ans;
}
};
class Solution
{
public:
//Function to return list containing first n fibonacci numbers.
vector printFibb(int n)
{
vector ans(n);
ans[0]=1;
if(n>1)ans[1]=1;
for(int i=2;i
