中级农民
- 积分
- 155
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-4-17
- 最后登录
- 1970-1-1
|
写了下第一轮的代码,这里使用普通的priorityqueue, 时间复杂度是O(nk)
- public class MaximumInMinimumInWindowK {
- public static void main(String[] args) {
- int[] arr = {3, 2, 5, 2, 9, 3, 7, 6, 5};
- System.out.println(getMaximumInMinimumInWindowK(arr, 6));
- }
- public static int getMaximumInMinimumInWindowK(int[] arr, int k) {
- if (arr == null || arr.length == 0) {
- return Integer.MIN_VALUE;
- }
- int res = Integer.MIN_VALUE;
- PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
- for (int i = 0; i < arr.length; i++) {
- pq.offer(arr[i]);
- if (i >= k - 1) {
- res = Math.max(res, pq.peek());
- pq.remove(arr[i - k + 1]);
- }
- }
- return res;
- }
- }
复制代码 |
|