LeetCode-contest record (Single Weekly 182)

Write in front

To organize the questions at the weekend leetcode competition.

Weekly 181

5368. Find the lucky number in the array

In an array of integers, if the frequency of occurrence of an integer is equal to its value, we call this integer the "lucky number".

Give you an integer array arr, please find and return a lucky number from it.

If there are multiple lucky numbers in the array, just return the largest one.

If there are no lucky numbers in the array, -1 is returned.

Example 1:

Input: arr = [2,2,3,4]

Output: 2

Explanation: The only lucky number in the array is 2 because the frequency of the value 2 is also 2.

Solution: The violence is over, there is nothing to say,

Code:

class Solution {
public:
    int findLucky(vector<int>& a) {
        if(a.empty()) return -1;
        vector<int> c(501,0);
        for(int i=0; i<a.size(); i++){
            c[a[i]]++;
        }
        int res = -1;
        for(int i=1; i<=500; i++){
            if(c[i]==i)
                res = i;
        }
        return res;
    }
};

5369. Count the number of combat units

n soldiers stood in a row. Each soldier has a unique rating rating.

Every 3 soldiers can form a combat unit, the grouping rules are as follows:

select 3 soldiers with subscripts i, j, k from the team, and their ratings are rating [i], rating [j], rating [ k]

Combat units must meet: rating [i] <rating [j] <rating [k] or rating [i]> rating [j]> rating [k], where 0 <= i <j <k <n

please you Returns the number of combat units that can be formed under the above conditions. Each soldier can be part of multiple combat units.

Example:

Input: rating = [2,5,3,4,1]

Output: 3

Explanation: We can form three combat units (2,3,4), (5,4,1), (5,3, 1) .

violence

A glance at the range during the game, only 200, such a small range is to make you violent (fog), the triple loop n ^ 3 will not time out, it is a bit ugly

Code:

class Solution {
public:
    int numTeams(vector<int>& a) {
        if(a.empty()||a.size()<=2) return 0;
        int res = 0;
        for(int x1 = 0; x1<a.size(); x1++){
            for(int x2=x1+1; x2<a.size(); x2++){
                for(int x3 = x2+1; x3<a.size(); x3++){
                    if(a[x1]<a[x2] && a[x2]<a[x3]){
                        ++res;
                    }
                    if(a[x1]>a[x2] && a[x2]>a[x3]){
                        ++res;
                    }
                }
            }
        }
        return res;
    }
};

statistics

In fact, it can be simplified, because this combat unit has only three people, so you can fix the person in the middle first, and then count the number of people on the left and right sides of him who are larger and smaller than him. biggerr * smallerl

Code:

class Solution {
public:
    int numTeams(vector<int>& a) {
        int l = a.size();
        if(l<3) return 0;
        int res = 0;
        for(int i=1; i<l-1; i++){
            int biggerl(0),smallerl(0),biggerr(0),smallerr(0);
            for(int j=0; j<i; j++){
                if(a[j]<a[i]){
                    smallerl++;
                }
                else{
                    biggerl++;
                }
            }
            for(int j=i+1; j<l; j++){
                if(a[j]<a[i]){
                    smallerr++;
                }
                else{
                    biggerr++;
                }
            }
            res += biggerl * smallerr + biggerr * smallerl;
        }
        return res;
    }
};

5370. Designing the subway system

Please implement a class UndergroundSystem, which supports the following three methods:

1. The passenger whose ID is checkIn (int id, string stationName, int t)

enters the subway station stationName at time t.

A passenger can only enter or leave at one subway station at the same time.

2.

The passenger whose checkOut (int id, string stationName, int t) number is id leaves the subway station stationName at time t.

3. getAverageTime (string startStation, string endStation) 

returns the average time spent from the subway station startStation to the subway station endStation.

The trip calculated by the average time includes all trips from startStation to endStation up to now.

When calling getAverageTime, the requested route contains at least one trip.

You can assume that all calls to checkIn and checkOut are logical. In other words, if a customer arrives at a subway station at time t1, the time t2 he leaves must satisfy t2> t1. All events are given in chronological order.

Solution:

This design problem is not difficult, it is more complicated, there are a lot of things to be recorded, station name, person's id, time of getting on and off, because the same person will get on and off many times, so the best The way is to record the corresponding time when you get off the car. The pair in C ++ is really a very useful structure. Then for the record after getting off the car, here is the idea of ​​@Ikagura gangster. The startstation and endstation are stitched together as the key , So it is very convenient to record.

Code:

class UndergroundSystem {
private:
    unordered_map<int,pair<string,int>> record;//记录人的id和上车时间
    unordered_map<string,pair<int,int>> count;//记录下车后的时间
public:
    UndergroundSystem() {
        record.clear();
        count.clear();
    }
    
    void checkIn(int id, string stationName, int t) {
        record[id] = {stationName,t};//上车记录
    }
    
    void checkOut(int id, string stationName, int t) {
        string name = record[id].first + stationName;//拼接
        count[name].first += t - record[id].second;//记录
        count[name].second += 1;
    }
    
    double getAverageTime(string startStation, string endStation) {
        string name = startStation + endStation;
        return double(count[name].first)/double(count[name].second);
    }
};

/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem* obj = new UndergroundSystem();
* obj->checkIn(id,stationName,t);
* obj->checkOut(id,stationName,t);
* double param_3 = obj->getAverageTime(startStation,endStation);
*/

5371. Find all good strings

Gives you two strings s1 and s2 of length n, and a string evil. Please return the number of good strings.

The definition of a good string is: its length is n, the lexicographic order is greater than or equal to s1, the lexicographic order is less than or equal to s2, and does not contain evil as a substring.

Since the answer may be very large, please return the result of the answer pair 10 ^ 9 + 7

Example 1:

Input: n = 2, s1 = "aa", s2 = "da", evil = "b"

Output: 51

Explanation: There are a total of 25 good strings starting with 'a': "aa", " ac "," ad ", ...," az ". There are also 25 good strings starting with 'c': "ca", "cc", "cd", ..., "cz". Finally, there is a good string starting with 'd': "da".

I have a showdown, and I do n’t understand after reading the solution. Recently, it is time-consuming to learn the Huawei test bank, so I will not study it and throw a link:

https://leetcode-cn.com/problems/find-all-good-strings/solution/shu-wei-dp-kmpqian-zhui-shu-zu-java-by-henrylee4/

Just sauce.

Published 13 original articles · won 27 · views 2649

Guess you like

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