LeetCode-Contest record (biweekly 23)

Write in front

Yesterday night's biweekly competition was filled with water. I actually made it all. Summing up yesterday's question, it should be this expression:
Brute force search

Biweekly 23

5360. Count the number of largest groups

Gives you an integer n. Please first find the sum of digits in the decimal representation of each integer from 1 to n (add the digits on each digit), and then put the digits and the equivalent digits in the same group.

Please count the number of numbers in each group, and return the number of groups with the largest number of numbers in parallel.

Example 1:

Input: n = 13

Output: 4

Explanation: There are a total of 9 groups. After summing the numbers from 1 to 13, the groups are:

[1,10], [2,11], [3,12] , [4,13], [5], [6], [7], [8], [9]. In total, there are 4 groups with the most numbers in parallel.

Solution:

Traverse the sum, then use the hash table to count the number of sums, and then count the maximum number of occurrences.

Code:

class Solution {
public:
    int countLargestGroup(int n) {
        vector<int> a(10001,0);
        int mm = 0;
        for(int i=1; i<=n; i++){
            int t = i;
            int s = 0;
            while(t>0){
                s += t % 10;
                t/=10;
            }
            mm = max(s,mm);//最大和就是哈希表的范围
            ++a[s]; 
        }
        int m = 0;
        for(int i=1; i<=mm; ++i){//寻找出现最多的次数
            m = max(m,a[i]);
        }
        int res = 0;
        for(int i=1; i<=mm; ++i)//统计该最大次数的出现次数
            if(a[i]==m) ++res;
        return res;
    }
};

5362. Construct K palindrome strings

Gives you a string s and an integer k. Please use all the characters in the s string to construct k non-empty palindrome strings.

If you can construct k palindrome strings with all characters in s, then please return True, otherwise return False.

Example 1:

Input: s = "annabelle", k = 2

Output: true

Explanation: You can construct 2 palindrome strings with all characters in s.

Some feasible construction schemes include: "anna" + "elble", "anbna" + "elle", "anellena" + "b"

Solution:

Count the number of occurrences of each letter, and then count the number of letters with odd numbers. According to the characteristics of palindrome strings, a palindrome string can have at most one letter with an odd number of occurrences, so if If the number of odd letters is greater than k, then it must not be constructed.

Code:

class Solution {
public:
    bool canConstruct(string s, int k) {
        vector<int> a(30,0);
        if(k>s.length()) return false;
        for(int i=0; i<s.length(); ++i){
            ++a[int(s[i]-'a')];
        }
        int c = 0;
        for(int i=0; i<30; ++i){
            if(a[i]%2==1)
                ++c;
        }
        if(c>k) return false;
        return true;
    }
};

5361. Do circles and rectangles overlap?

Gives you a circle represented by (radius, x_center, y_center) and a rectangle parallel to the coordinate axis (x1, y1, x2, y2), where (x1, y1) is the coordinate of the bottom left corner of the rectangle, (x2, y2) Is the coordinates of the upper right corner.

If the circle and rectangle overlap, please return True, otherwise return False.

In other words, please check whether there is a point (xi, yi), which is both on the circle and the rectangle (both include the point where the point falls on the boundary).

solution:

Boundary judgment

I didn't think about the mathematical method during the game. I directly draw a picture to find the positional relationship between the rectangle and the circle.

Code:

class Solution {
public:
    bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
        int circleleft = x_center - radius;
        int circleright = x_center + radius;
        int circletop = y_center + radius;
        int circlebot = y_center - radius;
        if(circletop<y1) return false;
        if(circlebot>y2) return false;
        if(circleleft>x2) return false;
        if(circleright<x1) return false;
        
        if(circletop<=y2 && circlebot>=y1 && circleleft>=x1 && circleright<=x2) return true;
        if(circletop>=y2 && circlebot<=y1 && circleleft<=x1 && circleright>=x2) return true;
        
        if(circleright==x1 || circleright==x2) return true;
        if(circleleft==x2 || circleleft==x1) return true;
        if(circlebot==y2 || circlebot==y1) return true;
        if(circletop==y1 || circletop==y2) return true;
        
        if(circleleft>x1 && circleright<x2) return true;
        if(circlebot>y1 && circletop<y2) return true;
        
        
        int d1 = (x1-x_center)*(x1-x_center) + (y1-y_center)*(y1-y_center);
        int d2 = (x1-x_center)*(x1-x_center) + (y2-y_center)*(y2-y_center);
        int d3 = (x2-x_center)*(x2-x_center) + (y1-y_center)*(y1-y_center);
        int d4 = (x2-x_center)*(x2-x_center) + (y2-y_center)*(y2-y_center);
        int r = radius * radius;
        if(r<d1 && r<d2 && r<d3 && r<d4) return false;
        if(r>d1 && r>d2 && r>d3 && r>d4) return false;
        
        
        return true;
    }
};

Mathematics

Take the distance from the center of the circle to the center of the matrix minus the distance from the center of the matrix to the corner of the matrix and compare it with the radius.

Code:

class Solution {
public:
    bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
        int x=(x1+x2)/2,y=(y1+y2)/2;
        double s=distance(x,y,x_center,y_center);
        double s1=distance(x,y,x1,y1);
        if(radius>=int(s-s1)) return true;
        return false;
    }
    double distance(int a,int b,int c,int d){
        double s;
        s=sqrt((a-c)*(a-c)+(b-d)*(b-d));
        return s;
    }
};

5363. Order of cooking

A chef collected his satisfaction with n dishes. The chef's time to make each dish is 1 unit time.

The "favorite time" coefficient of a dish is defined as the time it takes to cook the dish and each previous dish multiplied by the degree of satisfaction of the dish, which is time [i] * satisfaction [i].

Please return the maximum value of the total "love time" of all dishes.

You can arrange the order of cooking in any order, or you can choose to give up cooking to get a larger sum.

Example 1:

Input: satisfaction = [-1, -8,0,5, -9]

Output: 14

Explanation: Remove the second and last dish, the maximum favorite time coefficient sum is (-1 1 + 0 2 + 5 * 3 = 14). Each dish takes 1 unit time to complete.

prompt:

  1. n == satisfaction.length
  2. 1 <= n <= 500
  3. -10^3 <= satisfaction[i] <= 10^3

Solution: The

data range is scary, this question is greedy.

Sort first, according to the definition of favorite time, we want to put the dishes with high satisfaction behind, and then start cooking from the first point, and then move back from the beginning, because the data range is small, and there is no need to optimize , Every time you find the favorite time to find the maximum value.

Code:

class Solution {
public:
    int maxSatisfaction(vector<int>& a) {
        if(a.empty()) return 0;
        sort(a.begin(),a.end());//排序
        int p = 0;//起点位置
        int res = 0;
        while(p<a.size()){
            int sat = 0;
            for(int i=p; i<a.size(); ++i){
                sat += (i-p+1) * a[i];
            } 
            res = max(sat,res);
            ++p;
        }
        return res;
    }
};
Published 13 original articles · won 27 · views 2649

Guess you like

Origin blog.csdn.net/u011708337/article/details/105322437