Get into the habit of writing together! This is the 5th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
1. Problem description
Gives you an array of integers matches
which represent beats matches[i] = [winneri, loseri]
in a game .winneri
loseri
Returns a list of length 2** answer
:
answer[0]
is a list of all players who have not lost any games.answer[1]
is a list of all players who happened to lose a game.
The values in both lists should be returned in increasing order.
Notice:
- Only those players who participated in at least one match were considered.
- The generated test case guarantees that no two matches will have the same result .
Topic link: Players who have lost zero or one game .
Second, the subject requirements
Sample
输入: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
输出: [[1,2,10],[4,5,7,8]]
解释:
玩家 1、2 和 10 都没有输掉任何比赛。
玩家 4、5、7 和 8 每个都输掉一场比赛。
玩家 3、6 和 9 每个都输掉两场比赛。
因此,answer[0] = [1,2,10] 和 answer[1] = [4,5,7,8] 。
复制代码
visit
1.哈希、判重
2.建议用时15~35min
复制代码
3. Problem Analysis
This question is actually very easy to understand. At first, my idea was to use a hash table to count the losers.
If you lose 0 games , you can only judge whether the player who won the game is in it or not. If you lose a game, you can directly judge the value of the hash table to 1 .
However, this method has a flaw. The execution time is too long, and the winner needs to be judged to prevent repeated joining.
Start optimizing!
The map can be sorted automatically, so this time use the map output storage. We first store the winning player in the map, and set the initial value to 0. Then store the losing player to the map, at this time we only need to judge 0 1.
Fourth, the encoding implementation
1. Before optimization
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
map<int,int>l,k;//map存储输掉比赛的人数
int i,j,m=matches.size();//初始化数据
vector<int>ans1,ans2;//分别存储
for(i=0;i<m;i++)
l[matches[i][1]]++;//记录输掉比赛的人数
for(i=0;i<m;i++)
{
if(l[matches[i][0]]==0&&k[matches[i][0]]==0)//没有输
{
ans1.push_back(matches[i][0]);
k[matches[i][0]]=1;//用来判重
}
if(l[matches[i][1]]==1&&k[matches[i][1]]==0)//只输一场
{
ans2.push_back(matches[i][1]);
k[matches[i][1]]=1;//用来判重
}
}
sort(ans1.begin(),ans1.end());//排序
sort(ans2.begin(),ans2.end());//排序
return {ans1,ans2};//输出结果
}
};
复制代码
2. After optimization
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
map<int,int>m;//定义map
vector<int>ans1,ans2;
int i,n=matches.size();//初始数据
for(i=0;i<n;i++)//存储赢得人数、置为0
m[matches[i][0]]=0;
for(i=0;i<n;i++)//存储输人数++
m[matches[i][1]]++;
for(auto j=m.begin();j!=m.end();j++)//在map循环内部存储输出
{
if(j->second==0)//为0
ans1.push_back(j->first);//存储没输的玩家
else if(j->second==1)//为1
ans2.push_back(j->first);//存储只输一场的玩家
}
return {ans1,ans2};
}
};
复制代码
5. Test results
The code is a lot simpler, but the execution time is still very long. Let’s optimize it next time.