Skip to content

Commit

Permalink
map & sort [M]
Browse files Browse the repository at this point in the history
not so hard, use hashtable
  • Loading branch information
aucker committed May 22, 2024
1 parent e49fe51 commit ec0055a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
34 changes: 34 additions & 0 deletions daily/May22.cpp
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};
}
};
16 changes: 16 additions & 0 deletions daily/python/2225_find_players_w_zero_one_losses.py
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]
20 changes: 20 additions & 0 deletions daily/rust/2225_find_players_w_zero_one_losses.rs
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
}
}

0 comments on commit ec0055a

Please sign in to comment.