-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
not so hard, use hashtable
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> findWinners(vector<vector<int>>& matches) { | ||
unordered_map<int, int> lost_map; | ||
for (int i = 0; i < matches.size(); i++) { | ||
int lose = matches[i][1]; | ||
lost_map[lose]++; | ||
} | ||
|
||
vector<int> notLost; | ||
vector<int> oneLost; | ||
|
||
for (int i = 0; i < matches.size(); i++) { | ||
int winner = matches[i][0]; | ||
int loser = matches[i][1]; | ||
if (lost_map.find(winner) == lost_map.end()) { | ||
// not find winner | ||
notLost.push_back(winner); | ||
lost_map[winner] = 2; | ||
} | ||
|
||
if (lost_map[loser] == 1) { | ||
oneLost.push_back(loser); | ||
} | ||
} | ||
|
||
sort(notLost.begin(), notLost.end()); | ||
sort(oneLost.begin(), oneLost.end()); | ||
return {notLost, oneLost}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from collections import Counter | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def findWinners(self, matches: List[List[int]]) -> List[List[int]]: | ||
players = set(x for match in matches for x in match) | ||
loss_count = Counter(loser for _, loser in matches) | ||
|
||
winners = sorted(x for x in players if x not in loss_count) | ||
# winners = sorted(winners) | ||
|
||
one_loss_players = sorted(x for x, c in loss_count.items() if c == 1) | ||
# one_loss_players = sorted(one_loss_players) | ||
|
||
return [winners, one_loss_players] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
impl Solution { | ||
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> { | ||
let mut loss_count = HashMap::new(); | ||
for m in matches { | ||
loss_count.entry(m[0]).or_insert(0); | ||
*loss_count.entry(m[1]).or_insert(0) += 1; | ||
} | ||
|
||
let mut ans = vec![vec![], vec![]]; | ||
for (player, cnt) in loss_count { | ||
if cnt < 2 { | ||
ans[cnt as usize].push(player); | ||
} | ||
} | ||
|
||
ans[0].sort_unstable(); | ||
ans[1].sort_unstable(); | ||
ans | ||
} | ||
} |