LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 1337. The K Weakest Rows in a Matrix

Posted at

1337. The K Weakest Rows in a Matrix

アプローチ

Java Hashmap

class Solution {
    public int[] kWeakestRows(int[][] mat, int k) {
        Map<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < mat.length; i++) {
            int count = 0;
            for (int j = 0; j < mat[i].length; j++) {
                if (mat[i][j] == 1) count++;
            }
            map.put(i, count);
        }
        return map.entrySet().stream()
                 .sorted(Map.Entry.comparingByValue())
                 .limit(k)
                 .map(Map.Entry::getKey)
                 .mapToInt(it -> it)
                 .toArray();
    }
}

下記の解説を参考しました

https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2828853/Java-Memory-usage-less-than-99.71-oror-TreeMap-%2B-Stream-API

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