LeetCode, GeeksForGeeks Problem of the day solution
Open in Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Show more1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution {
public:
int swapNibbles(int n) {
return (uint8_t)((n << 4) | (n >> 4));
}
};
class Solution {
public:
int numSteps(string s) {
int step = 0;
int carry = 0;
for (int i = s.size() - 1; i > 0; --i) {
if ((s[i] - '0') + carry == 1) {
step += 2;
carry = 1;
} else {
step += 1;
}
}
return step + carry;
}
};
class Solution {
public:
int wins(int n, int &x, int &y, vector& dp){
if(n == 0){
return 0;
}
else if(dp[n] != -1){
return dp[n];
}
int ans = 0;
ans |= (!(wins(n-1, x, y, dp)));
if(n - x >= 0){
ans |= (!(wins(n-x, x, y, dp)));
}
if(n - y >= 0){
ans |= (!(wins(n-y, x, y, dp)));
}
return dp[n] = ans;
}
int findWinner(int n, int x, int y) {
// code here
vectordp(n+1, -1);
return wins(n, x, y, dp);
}
};
class Solution {
public:
int check(vector<int>& nums, int i){
int lc=0,uc=nums.size(),mid=0;
while(lc<uc)
{
mid=lc+(uc-lc)/2;
if(nums[mid]<i)
lc=mid+1;
else
uc=mid;
}
if(nums.size()-lc==i)
return i;
return -1;
}
int specialArray(vector<int>& nums) {
sort(nums.begin(),nums.end());
for(int i=0;i<=nums.size();i++)
{
if(check(nums,i)>=i)
return i;
}
return -1;
}
};
class Solution {
public:
int longestSubseq(int n, vector &a) {
// code here
int mx = 1;
vector dp(n,1);
for(int i = 1;i
class Solution {
public:
static const int MOD = 1000000007;
int check_all_records(int cur_ind, int count_a, int count_l, int n, vector<vector<vector<int>>>& temp) {
if (cur_ind == n) {
return 1;
}
if (temp[cur_ind][count_a][count_l] != -1) {
return temp[cur_ind][count_a][count_l];
}
int with_a_next = (count_a == 0) ? check_all_records(cur_ind + 1, count_a + 1, 0, n, temp) : 0;
int with_l_next = (count_l == 2) ? 0 : check_all_records(cur_ind + 1, count_a, count_l + 1, n, temp);
int with_p_next = check_all_records(cur_ind + 1, count_a, 0, n, temp);
int total = ((with_a_next + with_l_next) % MOD + with_p_next) % MOD;
temp[cur_ind][count_a][count_l] = total;
return total;
}
int checkRecord(int n) {
vector<vector<vector<int>>> temp(n, vector<vector<int>>(2, vector<int>(3, -1)));
return check_all_records(0, 0, 0, n, temp);
}
};
class Solution {
public:
int findMinCost(string s1, string s2, int a, int b)
{
int m=s1.size(), n=s2.size();
vector> dp(m+1, vector(n+1, 0));
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (s1[i - 1] == s2[j - 1])
{
dp[i][j] = 1 + dp[i - 1][j - 1];
}
else
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return (m-dp[m][n])*a+(n-dp[m][n])*b;
}
};
class Solution {
public:
vector> largestLocal(vector>& grid) {
int n=grid.size();
vector> v(n-2);
for(int i=0; i
class Solution {
public:
int minSteps(int D) {
int sum=0;
int steps=0;
for(steps=1;sum
class Solution {
public:
double mincostToHireWorkers(vector<int>& q, vector<int>& w, int k)
{
int n=q.size();
double res=DBL_MAX;
long long sum=0;
vector<pair<double, double>> workers;
for(int i=0; i<n; i++)
workers.push_back({(double)w[i]/q[i], (double)q[i]});
sort(workers.begin(), workers.end());
priority_queue<int> pq;
for(int i=0; i<n; i++)
{
sum+=workers[i].second;
pq.push(workers[i].second);
if(pq.size()>k)
{
sum-=pq.top();
pq.pop();
}
if(pq.size()==k)
res=min(res, sum*workers[i].first);
}
return res;
}
};
