首页 > 其他分享 >Kth Smallest Element in a Sorted Matrix

Kth Smallest Element in a Sorted Matrix

时间:2023-01-29 03:44:25浏览次数:43  
标签:pq matrix int maxHeap PriorityQueue Smallest Kth Sorted lambda

class Solution {                 // 14 ms, faster than 55.67%
public int kthSmallest( int[][] matrix, int k ) {
  int m = matrix.length, n = matrix[0].length ;    // For general, the matrix need not be a square
  PriorityQueue<Integer> maxHeap = new PriorityQueue<>( (o1, o2) -> Integer.compare(o2, o1) ) ;
  for ( int r = 0 ; r < m ; ++r ) {
    for ( int c = 0 ; c < n ; ++c ) {
      maxHeap.offer( matrix[r][c] ) ;
      if ( maxHeap.size() > k ) maxHeap.poll() ;  // maxHeap() 从大到小排,排到第k个,队顶最大弹出;新元素加入;
    }
  }
    return maxHeap.poll();
  }
}

 


You can use lambda expression since Java 8.

The following code will print 10, the larger.

// There is overflow problem when using simple lambda as comparator, as pointed out by Фима Гирин.
// PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);

PriorityQueue<Integer> pq =new PriorityQueue<>((x, y) -> Integer.compare(y, x));

pq.add(10);
pq.add(5);
System.out.println(pq.peek());

The lambda function will take two Integers as input parameters, subtract them from each other, and return the arithmetic result. The lambda function implements the Functional Interface, Comparator<T>. (This is used in place, as opposed to an anonymous class or a discrete implementation.)

 

标签:pq,matrix,int,maxHeap,PriorityQueue,Smallest,Kth,Sorted,lambda
From: https://www.cnblogs.com/YeYangzhi/p/17071637.html

相关文章

  • [LeetCode] 1061. Lexicographically Smallest Equivalent String
    Youaregiventwostringsofthesamelength s1 and s2 andastring baseStr.Wesay s1[i] and s2[i] areequivalentcharacters.Forexample,if s1=......
  • ABC 285 F - Substring of Sorted String
    好久都没写线段树的题解了……水一发题意:给定一个字符串,满足两种操作。第一种为修改串上某个地方的字母,第二种为查询一个区间,并判断当整个字符串按照升序排序后这一段区......
  • F - Substring of Sorted String
    题目链接题解(树状数组)我们维护两个树状数组,一个记录\(1\simi\)中\(s_i>s_{i+1}\)的数量,即逆序对数量,另一个记录\(1\simi\)中\(26\)个字母的数量。在修改时很好......
  • python-内建函数-排序函数sorted函数
    1.排序函数sorted()函数:对所有可迭代的对象进行排序操作语法格式:sorted(iterable,*,key=None,reverse=False)key:指定带有单个参数的函数,用于从interable的......
  • sortedArrDistanceLessK() 已知一个几乎有序的数组。几乎有序是指,如果把数组排好顺序
    packageclass06;importjava.util.Arrays;importjava.util.PriorityQueue;/***sortedArrDistanceLessK()*已知一个几乎有序的数组。几乎有序是指,如果把数组......
  • Hackthebox-Precious
    USER目标地址10.10.11.189还是日常简单信息收集的时候开放了22和80,80访问以后发现存在一个域名http://precious.htb/绑定hosts,绑定完成后我的插件告诉我好用了Phus......
  • hackthebox-Soccer
    靶机地址10.10.11.194user权限发现存在80端口嘛然后访问一下然后发现转到域名为本地绑定一下IP和域名这里因为是htb的靶场就不扫子域名了直接爆目录扫到一个目......
  • MAUI新生4.6-主题设置LightTheme&DarkTheme
    通过主题设置,可以在运行时更改应用的主题外观,比如切换亮色主题和暗黑主题。主题设置并没有新的知识点,基本的原理如下:定义每个应用主题的ResourceDictionary,每个ResourceD......
  • Python中的sorted()函数
    sorted()函数的主要用法>>>list=[1,3,2,4]#输入>>>sorted(list)#输入[1,2,3,4]#输出默认升序排列>>>sorted(list,reverse=True)#输入#降序排列[4,3,2,1......
  • Huawei Sweden Hackthon 2022 (华为瑞典2022黑客马拉松)参赛心得体验
    有幸在斯德哥尔摩的T-Centralen的WorldTradeCenter参加了HuaweiSwedenHackthon2022,即使是作为一支入围水队也收获了宝贵的经验。比赛体验是相当不错,有自助晚餐、早餐......