Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2399591

Browse files
committed
O(n) space and O(nlogn) time using hashmap and sorting.
1 parent f1d88e6 commit 2399591

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
3+
4+
Return a list answer of size 2 where:
5+
6+
answer[0] is a list of all players that have not lost any matches.
7+
answer[1] is a list of all players that have lost exactly one match.
8+
The values in the two lists should be returned in increasing order.
9+
10+
Note:
11+
12+
You should only consider the players that have played at least one match.
13+
The testcases will be generated such that no two matches will have the same outcome.
14+
15+
16+
Example 1:
17+
18+
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
19+
Output: [[1,2,10],[4,5,7,8]]
20+
Explanation:
21+
Players 1, 2, and 10 have not lost any matches.
22+
Players 4, 5, 7, and 8 each have lost one match.
23+
Players 3, 6, and 9 each have lost two matches.
24+
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
25+
Example 2:
26+
27+
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
28+
Output: [[1,2,5,6],[]]
29+
Explanation:
30+
Players 1, 2, 5, and 6 have not lost any matches.
31+
Players 3 and 4 each have lost two matches.
32+
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
33+
34+
35+
Constraints:
36+
37+
1 <= matches.length <= 105
38+
matches[i].length == 2
39+
1 <= winneri, loseri <= 105
40+
winneri != loseri
41+
All matches[i] are unique.
42+
"""
43+
class Solution:
44+
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
45+
hmap = collections.OrderedDict()
46+
for match in matches:
47+
hmap[match[1]] = hmap.get(match[1],0) + 1
48+
if match[0] not in hmap: hmap[match[0]] = 0
49+
result = []
50+
temp1,temp2 = [],[]
51+
for k,v in hmap.items():
52+
if v == 0:
53+
temp1.append(k)
54+
if v == 1:
55+
temp2.append(k)
56+
result.append(sorted(temp1))
57+
result.append(sorted(temp2))
58+
return result

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /