可能因爲剛應付完一系列面試,這週末狀態明顯鬆懈,比賽的時候心態也很佛。。比賽過程不多解釋,就着重寫題目分析了。
賽後總結
不足
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<int>& arr) {
int num = arr.size();
if(num==0) return -1;
int res = -1;
map<int,int> rec;
for(int i=0; i<num; ++i){
++rec[arr[i]];
}
for(auto iter=rec.rbegin(); iter!=rec.rend(); ++iter){
if(iter->first == 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 (
i
,j
,k
) 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]<rating[i],則++dp[i],res+=dp[j],此時可以看作找遞增組,組的數量就是dp[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<int>& rating) {
int num = rating.size();
if(num<3) return 0;
int res = 0;
vector<int> dp(num,0); // 左邊小於它的數字個數
for(int i=1; i<num; ++i){
for(int j=i-1; j>=0; --j){
if(rating[j]<rating[i]){
++dp[i];
res += dp[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 stationstationName
at timet
. - 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 stationstationName
at timet
.
3. getAverageTime(string startStation, string endStation)
- Returns the average time to travel between the
startStation
and theendStation
. - The average time is computed from all the previous traveling from
startStation
toendStation
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<int, pair<string, int>> in;
unordered_map<string, unordered_map<string, pair<int, double>>> 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);
*/