006 Given a matrix, rearrange its number

first, sort the values in this matrix by its frequency. if there is a tie, then order them by their values.
then, place the sorted number diagonally, starting from the bottom right corner, like the following:
5 4 4
4 4 3
3 2 1

brute force:
first we sort them, then we put the back by the order of “right bottom to left top”

public class sortmatrixByoccurance {
public void solution(int[][] matrix){
Map<Integer, Integer> map = new HashMap<>(); //using hashmap to store it's proficiency
int m = matrix.length;
int n = matrix[0].length;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
map.put(matrix[i][j], map.getOrDefault(matrix[i][j],0)+1);
}
}

List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet()); //sort the entry set of this map by the frequency and value
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
if(o1.getValue() == o2.getValue()){
return o1.getKey().compareTo(o2.getKey());
}else{
return o1.getValue().compareTo(o2.getValue());
}
}
});
int index = 0;
for(int i=m-1; i>=0; i--){
for(int j=n-1; j>=0; j--){ //? confused here
matrix[i][j] = list.get(index).getKey();
list.get(index).setValue(list.get(index).getValue()-1);
if(list.get(index).getValue() == 0){
index++;
}
}
}

}

public static void main(String[] args){
sortmatrixByoccurance sb = new sortmatrixByoccurance();
int[][] matrix = new int[][]{{2,2,3,3},{1,1,1,2},{2,2,4,4},{9,10,11,12}};
sb.solution(matrix);
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[0].length; j++){
System.out.print(matrix[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
}
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Tech In Pieces

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-25

07-04 7022
07-11 3827
03-30 962
01-25 2991
02-16 816