class Solution { public int[] kWeakestRows(int[][] mat, int k) { int m = mat.length, n = mat[0].length; double[] arr = new double[m]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i] += mat[i][j]; } arr[i] += 0.01*i; } // a: after sorting; num: index double[] a = new double[k]; int[] nums = new int[m]; for (int i = 0; i < m; ++i) { nums[i] = i; } for (int i = 0; i < k; ++i) { int index = i; for (int j = i; j < m; ++j) { if (arr[index] > arr[j]) { index = j; } } a[i] = arr[index]; double tmp1 = arr[i]; arr[i] = arr[index]; arr[index] = tmp1; int tmp2 = nums[i]; nums[i] = nums[index]; nums[index] = tmp2; } int[] num_k = new int[k]; System.arraycopy(nums, 0, num_k, 0, k); return num_k; } }