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:
//Function to find the smallest positive number missing from the array.
int missingNumber(int arr[], int n)
{
vectora(n+1,false);
for(int i=0;in){
continue;
}
a[arr[i]]=true;
}
for(int i=1;i<=n;i++)
if(a[i]==false)
return i;
return n+1;
}
};
class Solution {
public:
long long putMarbles(vector& weights, int k) {
vector < long long > sum;
for (int i = 0; i < weights.size() - 1; i++)
sum.push_back(weights[i] + weights[i + 1]);
sort(sum.begin(), sum.end());
long long maxi = 0, mini = 0;
for (int i = 0; i < k - 1; i++)
{
mini += sum[i];
maxi += sum[sum.size() - 1 - i];
}
return maxi - mini;
}
};
class Solution{
public:
//Function to find triplets with zero sum.
bool findTriplets(int arr[], int n)
{ sort(arr, arr+n); // sorting
for(int i=0;i
class Solution {
public:
int maxConsecutiveAnswers(string answerKey, int k) {
int maxf = 0, i = 0, n = answerKey.length();
vector count(26);
for (int j = 0; j < n; ++j) {
maxf = max(maxf, ++count[answerKey[j] - 'A']);
if (j - i + 1 > maxf + k)
--count[answerKey[i++] - 'A'];
}
return n - i;
}
};
class Solution{
public:
//Function to merge the arrays.
void merge(long long arr1[], long long arr2[], int n, int m)
{
int lo = n-1; int hi = 0;
while(lo>=0 && hiarr2[hi]){
swap(arr1[lo], arr2[hi]);
hi++;
}
lo--;
}
sort(arr1, arr1+n);
sort(arr2, arr2+m);
}
};
class Solution {
public:
int minSubArrayLen(int target, vector& nums) {
int mini = 1e9;
int n=nums.size();
int i=0;
int j=0;
int sum=0;
while(jtarget){
sum -= nums[i];
mini = min(mini, j-i+1);
i++;
}
if(sum
class Solution
{
public:
//Function to sort an array using quick sort algorithm.
void quickSort(int arr[], int low, int high)
{
if(lowpivot && j>=low+1){
j--;
}
if(i
class Solution {
public:
int longestSubarray(vector& A) {
int i = 0, j, k = 1;
for (j = 0; j < A.size(); ++j) {
if (A[j] == 0) k--;
if (k < 0 && A[i++] == 0) k++;
}
return j - i - 1;
}
};
class Solution {
public:
int stockBuyAndSell(int n, vector &prices) {
int ans = 0;
prices.push_back(0);
int j=0;
for(int i=0;iprices[i+1]){
ans += prices[i]-prices[j];
j = i+1;
}
}
return ans;
}
};
class Solution {
public:
int singleNumber(vector& nums) {
vectorcountBits(32,0);
for(int i=0;i
