首页 > 其他分享 >LeetCode 1329. Sort the Matrix Diagonally

LeetCode 1329. Sort the Matrix Diagonally

时间:2024-05-29 10:55:53浏览次数:12  
标签:Sort 25 mat int 1329 diagonal length Diagonally matrix

原题链接在这里:https://leetcode.com/problems/sort-the-matrix-diagonally/description/

题目:

A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0]mat[3][1], and mat[4][2].

Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.

Example 1:

Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]

Example 2:

Input: mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
Output: [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • 1 <= mat[i][j] <= 100

题解:

For the same diagonal nums, mat[i][j], i - j should be the same.

Group all the numbers based on i - j, sort them and assign them back.

Time Complexity: O(m * n * log(Math.min(m, n)). m = mat.length. n = mat[0].length.

Space: O(m * n).

AC Java:

 1 class Solution {
 2     public int[][] diagonalSort(int[][] mat) {
 3         if(mat == null || mat.length == 0 || mat[0].length == 0){
 4             return mat;
 5         }
 6         
 7         int m = mat.length;
 8         int n = mat[0].length;
 9         HashMap<Integer, PriorityQueue<Integer>> hm = new HashMap<>();
10         for(int i = 0; i < m; i++){
11             for(int j = 0; j < n; j++){
12                 hm.putIfAbsent(i - j, new PriorityQueue<Integer>());
13                 hm.get(i - j).add(mat[i][j]);
14             }
15         }
16         
17         for(int i = 0; i < m; i++){
18             for(int j = 0; j < n; j++){
19                 mat[i][j] = hm.get(i - j).poll();
20             }
21         }
22         
23         return mat;
24     }
25 }

 

标签:Sort,25,mat,int,1329,diagonal,length,Diagonally,matrix
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18219725

相关文章

  • Python3 笔记:sort() 和 sorted() 的区别
    1、sort()可以对列表中的元素进行排序,会改变原列表,之前的顺序不复存在。list.sort(key,reverse=None) key:默认值是None,可指定项目进行排序,此参数可省略。 reverse:默认值是None指做升序排序,“reverse=True”则做降序排序。无论列表中的元素是数值还是字符串都能排序,但......
  • elasticsearch使用Sort排序时Please use a keyword field instead.
    具体报错信息ElasticsearchStatusException[Elasticsearchexception[type=search_phase_execution_exception,reason=allshardsfailed]];nested:ElasticsearchException[Elasticsearchexception[type=illegal_argument_exception,reason=Textfieldsarenotoptimised......
  • 9-4 file-sort命令的使用
    9.4.1file查看文件file命令作用:file-determinefiletype #确定文件类型用法:file/etc/passwd注:linux系统不根据后缀名识别文件类型用file命令查看文件的类型 9.4.2按一定规则排序查看文件查看文件:ls-ltr:按时间排序 t......
  • PWN系列-Unsorted Bin Attack
    PWN系列-UnsortedBinAttack概述UnsortedBinAttack,顾名思义,该攻击与Glibc堆管理中的的UnsortedBin的机制紧密相关。UnsortedBinAttack被利用的前提是控制UnsortedBinChunk的bk指针。UnsortedBinAttack可以达到的效果是实现修改任意地址值为一个较大的数值......
  • redis数据结构:RedisObject,SkipList,SortedSet
    1.RedisObject对象redis中任何KV都会被封装为RedisObject对象,也叫做Redis对象 2.SkipList跳表元素按照升序排列存储,是有序的双向链表节点可以有多个指针,并且跨度不同。指针个数根据节点数自动生成,1~32性能和红黑树;二分查找差不多。实现简单,但是空间复杂度高样例:1——2......
  • JavaScript object array sort by string bug All In One
    JavaScriptobjectarraysortbystringbugAllInOnebug//purestringsarray,sortOK✅letarr=["banana","strawberry","apple"];JSON.stringify(arr.sort());//'["apple","banana","strawbe......
  • LeetCode 1287. Element Appearing More Than 25% In Sorted Array
    原题链接在这里:https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/题目:Givenanintegerarray sorted innon-decreasingorder,thereisexactlyoneintegerinthearraythatoccursmorethan25%ofthetime,returnthat......
  • sort
    https://blog.csdn.net/qq_22642239/article/details/105197937 C++STLsort函数的用法(自定义排序函数)  sort基本简介sort在STL库中是排序函数,有时冒泡、选择等O(n2)算法会超时时,我们可以使用STL中的快速排序函数O(nlogn)完成排序sort......
  • 终于明白了 Array.sort(comparator) 的原理
    终于明白了Array.sort(comparator)的原理原文地址:https://www.jameskerr.blog/posts/javascript-sort-comparators/After13yearsofJavaScript,IfinallyhaveawaytorememberhowthecomparatorfunctioninArray.sort()works.使用JavaScript13年之后,我终于有......
  • luatable.sort
    localfunctioncompare_with_flag(a,b)--如果a和b都有标志,则按照它们的某个字段进行排序ifa.flagandb.flagthenreturna.sort_field<b.sort_field--假设有一个sort_field字段用于排序--如果a有标志而b没有,则a应该排在b前面elseifa......