2225. Find Players With Zero or One Losses
給你一個陣列matches,每個陣列裡面有兩個數字,第一個數字表示贏家編號,第二
個數字找出輸家編號,回傳一個List分別包含了沒輸過的玩家編號和只輸過一場的玩
家編號,玩家編號需要升序排列。
Example:
Input: matches =
[[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
Constraints:
1 <= matches.length <= 10^5
matches[i].length == 2
1 <= winneri, loseri <= 10^5
winner[i] != loser[i]
All matches[i] are unique.
方法一 TreeMap
思路:
1.首先,因為題目要找只輸過一場和沒輸過的玩家,所以我們要把所有(贏和輸)
的數字都加入到一個集合裡面,並去除輸超過一場的玩家,贏的次數不用統計只
要統計輸的即可。
2.再來,因為題目要求要排序所以我們統計每個玩家輸的次數,插入一個有序Map之中
3.遍歷Map從ID小的玩家編號開始插入answer,沒輸過為0的放第一個,只輸一場為1的
放第二個,其他不滿足條件的不插入。
JavaCode: