leetcode 周赛 Weekly Contest 149

         This time, finally did not submit the WA.

         1154. Day of the Year

         Simple question. Print a few days a year to the present, we can distinguish a leap year. // Do not ask me run, ping What do you mean = =.

class Solution {
public:
    int dayOfYear(string date) {
        vector<int> days_run = {31,29,31,30,31,30,31,31,30,31,30,31};
        vector<int> days_ping = {31,28,31,30,31,30,31,31,30,31,30,31};
        int year = -1;
        int month = -1;
        int day = -1;
        date.push_back('-');
        string str;
        for(auto& ch : date)
        {
            if(ch == '-')
            {
                if(year == -1)
                {
                    year = atoi(str.c_str());
                }
                else if(month == -1)
                {
                    month = atoi(str.c_str());
                }
                else if(day == -1)
                {
                    day = atoi(str.c_str());
                }
                str.clear();
            }
            else
            {
                str.push_back(ch);
            }
        }
       
        int res = 0;
        if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 3200 != 0)) // runnian
        {
            for(int i = 0;i < month - 1;i++)
            {
                res += days_run[i];
            }
            res += day;
        }
        else
        {
            for(int i = 0;i < month - 1;i++)
            {
                res += days_ping[i];
            }
            res += day;           
        }
        return res;
    }
};

        1155. Number of Dice Rolls With Target Sum

        Moderate problem, dynamic programming. You d dice, comprising a number of 1 ~ f, is the sum of all the dice of the target number of results, to write a recursive old write not anxious to change the game recursion. They would have to try a non-recursive can not write right. The title of d, f looked really uncomfortable named.

class Solution {
public:
    vector<vector<int>> dp;
    int numRollsToTarget(int d, int f, int target) { // d : number f : maxnum
        if(dp.size() == 0)
        {
            dp.resize(d + 1, vector<int>(target + 1, -1));
        }
        
        if(d == 0)
        {
            if(target == 0) return 1;
            else return 0;
        }
        if(target <= 0) return 0;
        if(dp[d][target] != -1) return dp[d][target];
        int res = 0;
        for(int k = 1; k <= f; k++)
        {
            res = (res + numRollsToTarget(d - 1, f, target - k)) % 1000000007;
        }
        return  dp[d][target] = res;
    }
};

        Just fill up a non-recursive, really is not difficult, do not know Editor's Note game.

class Solution {
public:
    int numRollsToTarget(int d, int f, int target) { // d : number f : maxnum
        int res = 0;
        vector<vector<int>> dp(d + 1, vector<int>(target + 1, 0));
        dp[0][0] = 1;
        for(int k = 1; k <= d; k++) // times
        {
            for(int sum = 1;sum <= target; sum++) // 
            {
                for(int t = 1; t <= f && t <= sum; t++)
                {                
                    dp[k][sum] = (dp[k - 1][sum - t] + dp[k][sum]) % 1000000007;                  
                }
            }
        }
        return dp[d][target];
    }
};

 

        1156. Swap For Longest Repeated Character Substring

         Medium title, the sliding window. Found longest substrings is not included in duplicate numbers, up to two characters in the string can be exchanged once.

         I maintain the window is at most only contain an additional character window. Record the maximum window size, the start and end positions. Finally, there is no way to find not find the window of a character to replace the extra characters. If you can find, you return to the window size; otherwise the window size minus one (can not find other alternatives can only find a replacement from the window). While thinking pretty simple, but I feel that I wrote very elegant.

class Solution {
public:
    int maxRepOpt1(string text) {
        int n = text.size();
        int begin = 0;
        int res = 0;
        unordered_map<char, int> count;
        int res_begin;
        int res_end;
        for(int i = 0;i < n;i++) // end
        {
            count[text[i]]++;
            while(count.size() >= 3)
            {
                count[text[begin]]--;
                if(count[text[begin]] == 0) count.erase(text[begin]);
                begin++;
            }
            if(count.size() == 2)
            {
                while(true)
                {
                    int x = count.begin()->second;
                    int y = next(count.begin())->second;
                    if(x > 1 && y > 1) 
                    {
                        count[text[begin]]--;
                        if(count[text[begin]] == 0) 
                        {
                            count.erase(begin++);
                            break;
                        }
                        begin++;
                    }
                    else break;
                }
            }
            int len = i - begin + 1;
            if(len > res)
            {
                res_begin = begin;
                res_end = i;
                res = len;
            }
        }
      
        if(res == 2)
        {
            char a = text[res_begin];
            char b = text[res_end];
            for(int i = 0;i < n;i++)
            {
                if(i < res_begin || i > res_end)
                {
                    if(text[i] == a || text[i] == b)
                    {
                        return 2;
                    }
                }                
            }
            return 1;
        }
        else
        {
            char single;
            char repeat;
            unordered_map<char, int> number;
            for(int i = res_begin;i <= res_end;i++)
            {
                number[text[i]]++;
            }
            if(number.size() == 1) return res;
            int x = number.begin()->second;
            int y = next(number.begin())->second;
            
            if(x > y) 
            {
                repeat = number.begin()->first;
                single = next(number.begin())->first;
            }
            else
            {
                single = number.begin()->first;
                repeat = next(number.begin())->first;                
            }
            
            for(int i = 0;i < n;i++)
            {
                if(i < res_begin || i > res_end)
                {
                    if(text[i] == single)
                    {
                        return res;
                    }
                }
            }
            return res - 1;
        }
        return res;
    }
};

         1157. Online Majority Element In Subarray

        Difficult questions, binary search and hash. To an array, digital frequency greater than a threshold appears query interval [left, right] within.

        As if the subject did not say there may be more than the threshold value?

        My idea is to record all indices, each corresponding to the frequency and numbers of each number appears. If you want to find a number greater than a certain frequency interval, then only find the overall number of digital frequency is greater than this threshold. Thereafter, since each number appears subscripts sequenced good, it is possible to use binary search to find the fall [left, right] Several digital section, and see if more than a threshold, larger than the direct return.

class MajorityChecker {
public:
    unordered_map<int,vector<int>> index;
    map<int,vector<int>> times;
    MajorityChecker(vector<int>& arr) {
        int n = arr.size();
        unordered_map<int,int> count;
        for(int i = 0; i < n;i++)
        {
            count[arr[i]]++;
            index[arr[i]].push_back(i);
        }
        for(auto& cnt : count)
        {
            int number = cnt.first;
            int time = cnt.second;
            times[time].push_back(number);
        }
    }
    
    int query(int left, int right, int threshold) {
        if(threshold > right - left + 1) return -1;
        auto it = times.lower_bound(threshold);
        for(;it != times.end();it++)
        {
            for(auto& num : it->second)
            {
                vector<int>& idx = index[num];
                auto l = lower_bound(idx.begin(), idx.end(), left);
                auto r = upper_bound(idx.begin(), idx.end(), right);
                if(r == idx.begin()) continue;
           //     cout << num << " " << *l << " " << *r << " " << prev(r) - l + 1 << endl;
                if(prev(r) - l + 1 >= threshold) return num;
            }
        }
        return -1;
    }
};

/**
 * Your MajorityChecker object will be instantiated and called as such:
 * MajorityChecker* obj = new MajorityChecker(arr);
 * int param_1 = obj->query(left,right,threshold);
 */

 

Guess you like

Origin www.cnblogs.com/fish1996/p/11334802.html