Leetcode One Question of the Day 2/1 ~ 2/5 Summary

I have begun to accumulate my own question volume. I have to say that LeetCode, a daily question specifically for a category of questions, is really suitable for cuteness. After finishing this month, I hope that the sliding window questions can be confidently teared.
2/1:
Insert picture description here
Idea: We can respectively calculate the total size of Alice and Bob’s candy bars, so that for each candy bar of Bob/Alice, the size of the one that needs to be exchanged by Alice/Bob is fixed . Then it becomes a question of finding numbers. Using hash is the fastest way to find.

class Solution {
    
    
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
    
    
        int sumA = accumulate(A.begin(), A.end(), 0);
        int sumB = accumulate(B.begin(), B.end(), 0);
        int tmp = (sumA - sumB) / 2;
        unordered_set<int> s(A.begin(), A.end());
        vector<int> ans;
        for (int y : B) {
    
    
            int x = y + tmp;
            if (s.find(x) != s.end()) {
    
    
                ans.push_back(x);
                ans.push_back(y);
                break;
            }
        }
        return ans;
    }
};

Time complexity O(n)
Space complexity O(1)
2/2 see link solution
2/3 see link solution
2/4:
Insert picture description here
Idea: For each element: store the sum of the elements so far in the small standard for later Quickly calculate the sum of the elements in the sliding window.
In the subsequent sequence, just slide the fixed window size to the right and update the maximum value each time.

class Solution {
    
    
public:
    double findMaxAverage(vector<int>& nums, int k) {
    
    
        int left = 0, right = k - 1;
        for(int i = 0; i < nums.size() - 1; i++) {
    
    
            nums[i + 1] += nums[i];
        }
        double ans = nums[k - 1] * 1.0 / k;
        for(int i = k ; i < nums.size(); i++) {
    
    
            double tmp = (nums[i] - nums[i - k]) * 1.0 / k;
            ans = max(tmp, ans);
        }
        return ans;
    }
};

Time complexity of O (n)
space complexity O (. 1)
2/5:
Insert picture description here
Thinking: and longest substring after the replacement is repeated , the longest substring of characters no repeating almost right boundary expanding until found When not satisfied, the left boundary changes again, and when the right boundary exceeds the array, it stops. A very classic sliding window template.

class Solution {
    
    
public:
    int equalSubstring(string s, string t, int maxCost) {
    
    
        if(s == t) return s.size();
        int len = s.size();
        int left = 0, right = 0,cost = 0;
        while(right < len) {
    
    
            cost += abs(s[right] - t[right]);
            if(cost > maxCost) {
    
    
                cost -= abs(s[left] - t[left]);
                left++;
            }
            right++;
        }
        return right - left;
    }
};

Time complexity O(n)
Space complexity O(1)

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/113678016