可能因为刚应付完一系列面试,这周末状态明显松懈,比赛的时候心态也很佛。。比赛过程不多解释,就着重写题目分析了。
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:
i
, j
, k
) with rating (rating[i]
, rating[j]
, rating[k]
).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漏洞的版本。 3.【medium】1396. Design Underground System Implement the class 1. 2. 3. You can assume all calls to Example 1: Constraints: 题目链接:https://leetcode-cn.com/problems/design-underground-system/ 这道题就是根据题目逻辑进行模拟即可,没有任何坑。 需要一个空间用于记录进站情况,一个空间记录平均用时情况。 class Solution {
public:
int numTeams(vector
UndergroundSystem
that supports three methods: checkIn(int id, string stationName, int t)
id
, gets in the station stationName
at time t
. checkOut(int id, string stationName, int t)
id
, gets out from the station stationName
at time t
.getAverageTime(string startStation, string endStation)
startStation
and the endStation
.startStation
to endStation
that happened directly.getAverageTime
is always valid.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.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
20000
operations.1 <= id, t <= 10^6
1 <= stationName.length <= 10
10^-5
of the actual value will be accepted as correct.思路
class UndergroundSystem {
public:
unordered_map