当前位置:网站首页>[leetcode weekly race record] record of the 79th biweekly race + the 295th weekly race

[leetcode weekly race record] record of the 79th biweekly race + the 295th weekly race

2022-06-09 13:25:00 One, two, three o-0-o

Personal ranking after the game

leetcode The personal data
 Insert picture description here

Game analysis summary

The first 79 A fortnight game

6083. Determine whether the number count of a number is equal to the value of the digit

class Solution {
    
public:
    bool digitCount(string num) {
    
        unordered_map<int,int> unMap;

        for(char c : num) unMap[c - '0']++;

        int n = num.size();
        for(int i{
    };i<n;++i){
    
            if(unMap[i] == (num[i] - '0')){
    
                continue;
            }else return false;
        }
        
        return true;
    }
};

6084. The sender with the maximum number of words

class Solution {
    
public:
	//  Simulation solution 
    string largestWordCount(vector<string>& messages, vector<string>& senders) {
    
        int n = messages.size();
        unordered_map<string,int> unMap;

        for(int i{
    };i<n;++i){
    
            //  Get the number of words 
            vector<string> res;
            string result;
            stringstream input(messages[i]);
            while(input>>result)
                res.push_back(result);
            unMap[senders[i]] += res.size();
        }
        vector<pair<int,string>> vv;
        for(auto [key,value] : unMap){
    
            vv.push_back({
    value,key});
        }

        sort(vv.begin(),vv.end(),[](auto &&a,auto &&b){
    
            if(a.first == b.first){
    
                return a.second < b.second;
            }

            return a.first < b.first;
        });
        string result =  vv[vv.size()-1].second;
        return result;
    }
};

6085. The greatest overall importance of the road

//  greedy : Greedy for the degree of each point , Then the point with the most degrees must be assigned a larger weight and better 
class Solution {
    
public:
    long long maximumImportance(int n, vector<vector<int>>& roads) {
    
    	//  Count the degree of each point 
        vector<int> node(n,0);
        for(auto &v : roads){
    
            node[v[0]]++;
            node[v[1]]++;
        }

        sort(node.begin(),node.end());
		//  The point with the largest degree assigns the largest weight 
        long long result{
    };
        for(int i{
    };i<n;++i){
    
            result += 1LL * node[i] * (i+1);
        }
        
        return result;
    }
};

10011. Book tickets for the concert in groups

Ignore

The first 295 Weekly match

2287. Rearrange the characters to form the target string

class Solution {
    
public:
    int rearrangeCharacters(string s, string target) {
    
        unordered_map<int,int> unMap;

        for(char c : target) unMap[c-'a']++;
        int result{
    INT_MAX};

        vector<int> need(26);

        for(char c : s){
    
            if(unMap[c-'a'] > 0)
                need[c-'a']++;
        }

        for(auto [key,value] : unMap){
    
            if(value > 0){
    
                if(need[key] <= 0) return 0;
            }
        }

        for(int i{
    };i<26;++i){
    
            if(need[i] > 0 && unMap[i] > 0){
    
                result = min(need[i]/unMap[i],result);
            }
        }
        
        return result;
    }
};

2288. Price reduction

//  Personal version 
class Solution {
    
public:
    bool isLeagal(string &s){
    
        if(s == "$") return false;
        int n = s.size();

        for(int i{
    };i<n;++i){
    
            if(s[0] != '$') return false;
            if(i > 0 ){
    
                if(( s[i] >= '0' && s[i] <= '9')){
    

                }else return false;
            }
        }
        return true;
    }
    string discountPrices(string sentence, int discount) {
    
        //  Get the number of words 
        vector<string> res;
        string result;
        stringstream input(sentence);
        while(input>>result)
            res.push_back(result);

        int n = res.size();

        for(int i{
    };i<n;++i){
    
            string s = res[i];

            if(isLeagal(s)){
    
                double prices = stod(s.substr(1,s.size()-1));

                prices *= (100-discount) * 0.01;

                ostringstream oss;
                oss << fixed << setprecision(2) << prices;

                string ans1 = "$";
                ans1+=oss.str();
                res[i] = ans1;
            }

        }
        string aa;
        for(int i{
    };i<n;++i){
    
            aa += res[i];

            if(i != n-1) aa+=" ";
        }       

        return aa;

    }
};
//  Easy version 
class Solution {
    
public:
    string discountPrices(string sentence, int discount) {
    
        string res;
        istringstream iss(sentence);
        string t;
        // c++ Standard library functions getline
        while (getline(iss, t, ' ')) {
     // while (iss >> t)
            bool isPrice = (t.size() > 1 && t[0] == '$');
            //  Check whether it is a price word 
            if (isPrice) {
    
                for (int i = 1; i < t.size(); ++i) {
    
                    if (t[i] == '$' || t[i] < '0' || t[i] > '9') {
    
                        isPrice = 0; break;
                    }
                }
            }
            if (isPrice) {
    
                string p = t.substr(1);
                double price = stod(p);
                price *= (100 - discount) * 0.01;
                //  Only two decimal places are truncated 
                ostringstream oss;
                // setprecision Control the number after the decimal point 
                oss << fixed << setprecision(2) << price;
                
                res += '$' + oss.str() + ' ';
            } else {
    
                res += t + ' ';
            }
        }
        res.pop_back();
        return res;
    }
};

2289. Make the array in non decreasing order

//  Application of monotone stack 
//  According to the meaning of the question, it can be transformed into : Converted to the maximum value of the time when all elements are deleted 
class Solution {
    
public:
    int totalSteps(vector<int> &nums) {
    
        int ans = 0;
        //  Monotonically decreasing stack stores elements and their deletion time 
        stack<pair<int, int>> st;
        for (int num : nums) {
    
            int maxT = 0;
            //  Get it to delete num And delete num Between all numbers less than num The maximum moment of the value of 
            while (!st.empty() && st.top().first <= num) {
    
                maxT = max(maxT, st.top().second);
                st.pop();
            }
            maxT = st.empty() ? 0 : maxT + 1;
            ans = max(ans, maxT);
            st.emplace(num, maxT);
        }
        return ans;
    }
};

2290. The minimum number of obstacles that need to be removed to reach the corner

Question 4 will not be considered for the time being

Reflection summary

Personal situation

The first 29、30 sub-participation leetcode competition ;

In total, I got 5 Time 12 branch ,15 Time 7 branch ,10 Time 3 branch ;

Follow up improvement

  1. greedy + Special review of graph theory 、 Training and summary system training , summary
  2. Special review of monotonous stack 、 Training and summary system training , summary
原网站

版权声明
本文为[One, two, three o-0-o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091208006650.html