diff --git a/daily/May22.cpp b/daily/May22.cpp new file mode 100644 index 0000000..61ceb67 --- /dev/null +++ b/daily/May22.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +class Solution { + public: + vector> findWinners(vector>& matches) { + unordered_map lost_map; + for (int i = 0; i < matches.size(); i++) { + int lose = matches[i][1]; + lost_map[lose]++; + } + + vector notLost; + vector 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}; + } +}; \ No newline at end of file diff --git a/daily/python/2225_find_players_w_zero_one_losses.py b/daily/python/2225_find_players_w_zero_one_losses.py index e69de29..d0b88c3 100644 --- a/daily/python/2225_find_players_w_zero_one_losses.py +++ b/daily/python/2225_find_players_w_zero_one_losses.py @@ -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] diff --git a/daily/rust/2225_find_players_w_zero_one_losses.rs b/daily/rust/2225_find_players_w_zero_one_losses.rs index e69de29..b839d29 100644 --- a/daily/rust/2225_find_players_w_zero_one_losses.rs +++ b/daily/rust/2225_find_players_w_zero_one_losses.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn find_winners(matches: Vec>) -> Vec> { + 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 + } +} \ No newline at end of file