0
0

More than 1 year has passed since last update.

Leetcode 2225. Find Players With Zero or One Losses

Posted at

2225. Find Players With Zero or One Losses

アプローチ

Hashmap

class Solution {
    public List<List<Integer>> findWinners(int[][] matches) {
        
        HashMap<Integer, Integer> t1 = new HashMap<>();
        HashMap<Integer, Integer> t2 = new HashMap<>();

        for (int i = 0; i < matches.length; i++) {
            t1.put(matches[i][0], t1.getOrDefault(matches[i][0], 0) + 1);
        }

        for (int i = 0; i < matches.length; i++) {
            t2.put(matches[i][1], t2.getOrDefault(matches[i][1], 0) + 1);
        }

        List<Integer> r1 = t1.entrySet().stream().filter(obj -> t2.containsKey(obj.getKey()) == false).map(Map.Entry::getKey).collect(Collectors.toList());
        List<Integer> r2 = t2.entrySet().stream().filter(obj -> obj.getValue() == 1).map(Map.Entry::getKey).collect(Collectors.toList());
        List<List<Integer>> result = new ArrayList<>();
        Collections.sort(r1);
        Collections.sort(r2);

        result.add(r1);
        result.add(r2);

        return result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0