【leetcode】第 182 场周赛

可能因为刚应付完一系列面试,这周末状态明显松懈,比赛的时候心态也很佛。。比赛过程不多解释,就着重写题目分析了。

赛后总结

不足

1. 容器使用还是不够透彻,map的初始化要搞清楚。

优点

1. 简单题做的很快。

2. 心态是真的好,不过有点过于佛了。。也不利于思考。

 

题目分析

1. 【easy】1394. Find Lucky Integer in an Array

Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.

Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

Example 2:

Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

Example 3:

Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in the array.

Example 4:

Input: arr = [5]
Output: -1

Example 5:

Input: arr = [7,7,7,7,7,7,7]
Output: 7

Constraints:

  • 1 <= arr.length <= 500
  • 1 <= arr[i] <= 500

题目链接:https://leetcode-cn.com/problems/find-lucky-integer-in-an-array/

思路

根据题意,只需要 数值=出现次数 ,那么遍历统计即可。

class Solution {
public:
    int findLucky(vector& arr) {
        int num = arr.size();
        if(num==0) return -1;
        int res = -1;
        map rec;
        for(int i=0; ifirst == iter->second){
                res = iter->first;
                break;
            }
        }
        return res;
    }
};

 

2.【medium】1395. Count Number of Teams

There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

You have to form a team of 3 soldiers amongst them under the following rules:

  • Choose 3 soldiers with index (ijk) with rating (rating[i]rating[j]rating[k]).
  • A team is valid if:  (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

Example 1:

Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 

Example 2:

Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.

Example 3:

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

Constraints:

  • n == rating.length
  • 1 <= n <= 200
  • 1 <= rating[i] <= 10^5

题目链接:https://leetcode-cn.com/problems/count-number-of-teams/

思路

题目要求选出的3个数字递增或递减,但是数列本身不是有序的。

设计一个dp,dp[i]记录第i位前面小于rating[i]的数字个数,因为数列并不有序,因此要对i前面所有位置j进行一次判断。

1)若rating[j]

2)若rating[j]>rating[i],则res+=(j-dp[j]),此时是找递减组,组的数量是j位置前大于rating[j]的元素个数,用下标-dp[j]求得。

其实,这个思路代码有bug,计算递减组的时候没有考虑到有相等元素的情况,比如:[2,5,3,5,4,1]。

虽然leetcode上能通过,但最好还是递减也单独进行记录。下面写的是钻了leetcode漏洞的版本。

class Solution {
public:
    int numTeams(vector& rating) {
        int num = rating.size();
        if(num<3) return 0;
        int res = 0;
        vector dp(num,0);  // 左边小于它的数字个数
        for(int i=1; i=0; --j){
                if(rating[j]rating[i]){
                    res += (j-dp[j]);
                }
            }
        }
        return res;
    }
};

 

3.【medium】1396. Design Underground System

Implement the class UndergroundSystem that supports three methods:

1. checkIn(int id, string stationName, int t)

  • A customer with id card equal to id, gets in the station stationName at time t.
  • A customer can only be checked into one place at a time.

2. checkOut(int id, string stationName, int t)

  • A customer with id card equal to id, gets out from the station stationName at time t.

3. getAverageTime(string startStation, string endStation) 

  • Returns the average time to travel between the startStation and the endStation.
  • The average time is computed from all the previous traveling from startStation to endStation that happened directly.
  • Call to getAverageTime is always valid.

You can assume all calls to checkIn and checkOut methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. All events happen in chronological order.

Example 1:

Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]

Output
[null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);
undergroundSystem.checkOut(27, "Waterloo", 20);
undergroundSystem.checkOut(32, "Cambridge", 22);
undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0
undergroundSystem.checkOut(10, "Waterloo", 38);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.0

Constraints:

  • There will be at most 20000 operations.
  • 1 <= id, t <= 10^6
  • All strings consist of uppercase, lowercase English letters and digits.
  • 1 <= stationName.length <= 10
  • Answers within 10^-5 of the actual value will be accepted as correct.

题目链接:https://leetcode-cn.com/problems/design-underground-system/

思路

这道题就是根据题目逻辑进行模拟即可,没有任何坑。

需要一个空间用于记录进站情况,一个空间记录平均用时情况。

class UndergroundSystem {
public:
    unordered_map> in;
    unordered_map>> time;   // num, time
    UndergroundSystem() {

    }
    
    void checkIn(int id, string stationName, int t) {
        in[id] = make_pair(stationName, t);
    }
    
    void checkOut(int id, string stationName, int t) {
        string start = in[id].first;
        double usetime = t*1.0 - in[id].second;
        if(time.find(start)!=time.end() && time[start].find(stationName)!=time[start].end()){
            int num = time[start][stationName].first;
            double oldtime = time[start][stationName].second;
            time[start][stationName].second = (num * oldtime + usetime) / ( num + 1.0);
            ++time[start][stationName].first;
        }else{
            time[start][stationName] = make_pair(1, usetime);
        }
        in.erase(id);
    }
    
    double getAverageTime(string startStation, string endStation) {
        return time[startStation][endStation].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);
 */

 

你可能感兴趣的:(code,c++,比赛)