Skip to content

feat: update solutions to lc problems: No.2225,2705 #2873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 52 additions & 89 deletions solution/2200-2299/2225.Find Players With Zero or One Losses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ tags:

<!-- solution:start -->

### 方法一:哈希表
### 方法一:哈希表 + 排序

我们用一个哈希表 $\text{cnt}$ 记录每个玩家输掉的比赛场次。

然后遍历哈希表,将输掉 $0$ 场比赛的玩家放入 $\text{ans}[0]$,将输掉 $1$ 场比赛的玩家放入 $\text{ans}[1]$。

最后将 $\text{ans}[0]$ 和 $\text{ans}[1]$ 按照升序排序,返回结果。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为比赛场次数。

<!-- tabs:start -->

Expand All @@ -92,16 +100,14 @@ tags:
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
cnt = Counter()
for a, b in matches:
if a not in cnt:
cnt[a] = 0
cnt[b] += 1
for winner, loser in matches:
if winner not in cnt:
cnt[winner] = 0
cnt[loser] += 1
ans = [[], []]
for u, v in cnt.items():
for x, v in sorted(cnt.items()):
if v < 2:
ans[v].append(u)
ans[0].sort()
ans[1].sort()
ans[v].append(x)
return ans
```

Expand All @@ -111,19 +117,14 @@ class Solution:
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int[] m : matches) {
int a = m[0], b = m[1];
cnt.putIfAbsent(a, 0);
cnt.put(b, cnt.getOrDefault(b, 0) + 1);
for (var e : matches) {
cnt.putIfAbsent(e[0], 0);
cnt.merge(e[1], 1, Integer::sum);
}
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
ans.add(new ArrayList<>());
for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
int u = entry.getKey();
int v = entry.getValue();
if (v < 2) {
ans.get(v).add(u);
List<List<Integer>> ans = List.of(new ArrayList<>(), new ArrayList<>());
for (var e : cnt.entrySet()) {
if (e.getValue() < 2) {
ans.get(e.getValue()).add(e.getKey());
}
}
Collections.sort(ans.get(0));
Expand All @@ -139,18 +140,19 @@ class Solution {
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
unordered_map<int, int> cnt;
for (auto& m : matches) {
int a = m[0], b = m[1];
if (!cnt.count(a)) cnt[a] = 0;
++cnt[b];
map<int, int> cnt;
for (auto& e : matches) {
if (!cnt.contains(e[0])) {
cnt[e[0]] = 0;
}
++cnt[e[1]];
}
vector<vector<int>> ans(2);
for (auto& [u, v] : cnt) {
if (v < 2) ans[v].push_back(u);
for (auto& [x, v] : cnt) {
if (v < 2) {
ans[v].push_back(x);
}
}
sort(ans[0].begin(), ans[0].end());
sort(ans[1].begin(), ans[1].end());
return ans;
}
};
Expand All @@ -161,17 +163,16 @@ public:
```go
func findWinners(matches [][]int) [][]int {
cnt := map[int]int{}
for _, m := range matches {
a, b := m[0], m[1]
if _, ok := cnt[a]; !ok {
cnt[a] = 0
for _, e := range matches {
if _, ok := cnt[e[0]]; !ok {
cnt[e[0]] = 0
}
cnt[b]++
cnt[e[1]]++
}
ans := make([][]int, 2)
for u, v := range cnt {
for x, v := range cnt {
if v < 2 {
ans[v] = append(ans[v], u)
ans[v] = append(ans[v], x)
}
}
sort.Ints(ans[0])
Expand All @@ -185,14 +186,16 @@ func findWinners(matches [][]int) [][]int {
```ts
function findWinners(matches: number[][]): number[][] {
const cnt: Map<number, number> = new Map();
for (const [a, b] of matches) {
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
cnt.set(b, (cnt.get(b) || 0) + 1);
for (const [winner, loser] of matches) {
if (!cnt.has(winner)) {
cnt.set(winner, 0);
}
cnt.set(loser, (cnt.get(loser) || 0) + 1);
}
const ans: number[][] = [[], []];
for (let [u, v] of cnt.entries()) {
for (const [x, v] of cnt) {
if (v < 2) {
ans[v].push(u);
ans[v].push(x);
}
}
ans[0].sort((a, b) => a - b);
Expand All @@ -210,14 +213,16 @@ function findWinners(matches: number[][]): number[][] {
*/
var findWinners = function (matches) {
const cnt = new Map();
for (const [a, b] of matches) {
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
cnt.set(b, (cnt.get(b) || 0) + 1);
for (const [winner, loser] of matches) {
if (!cnt.has(winner)) {
cnt.set(winner, 0);
}
cnt.set(loser, (cnt.get(loser) || 0) + 1);
}
const ans = [[], []];
for (let [u, v] of cnt.entries()) {
for (const [x, v] of cnt) {
if (v < 2) {
ans[v].push(u);
ans[v].push(x);
}
}
ans[0].sort((a, b) => a - b);
Expand All @@ -230,46 +235,4 @@ var findWinners = function (matches) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### JavaScript

```js
/**
* @param {number[][]} matches
* @return {number[][]}
*/
var findWinners = function (matches) {
const onlyWins = new Set(),
oneLose = new Set(),
moreLosses = new Set();

for (const [winner, loser] of matches) {
if (!moreLosses.has(loser)) {
if (oneLose.has(loser)) {
oneLose.delete(loser);
moreLosses.add(loser);
} else {
onlyWins.delete(loser);
oneLose.add(loser);
}
}

if (!moreLosses.has(winner) && !oneLose.has(winner)) {
onlyWins.add(winner);
}
}

return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)];
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading