生活随笔
收集整理的這篇文章主要介紹了
[Leetcode][第632题][JAVA][最小区间][堆][滑动窗口]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【問題描述】[困難]
【解答思路】
1. 堆
復雜度
class Solution {public int[] smallestRange(List
<List
<Integer>> nums
) {int rangeLeft
= 0, rangeRight
= Integer
.MAX_VALUE
;int minRange
= rangeRight
- rangeLeft
;int max
= Integer
.MIN_VALUE
;int size
= nums
.size();int[] next
= new int[size
];PriorityQueue
<Integer> priorityQueue
= new PriorityQueue<Integer>(new Comparator<Integer>() {public int compare(Integer index1
, Integer index2
) {return nums
.get(index1
).get(next
[index1
]) - nums
.get(index2
).get(next
[index2
]);}});for (int i
= 0; i
< size
; i
++) {priorityQueue
.offer(i
);max
= Math
.max(max
, nums
.get(i
).get(0));}while (true) {int minIndex
= priorityQueue
.poll();int curRange
= max
- nums
.get(minIndex
).get(next
[minIndex
]);if (curRange
< minRange
) {minRange
= curRange
;rangeLeft
= nums
.get(minIndex
).get(next
[minIndex
]);rangeRight
= max
;}next
[minIndex
]++;if (next
[minIndex
] == nums
.get(minIndex
).size()) {break;}priorityQueue
.offer(minIndex
);max
= Math
.max(max
, nums
.get(minIndex
).get(next
[minIndex
]));}return new int[]{rangeLeft
, rangeRight
};}
}
2. 滑動窗口+哈希表
復雜度
public int[] smallestRange(List
<List
<Integer>> nums
) {int size
= nums
.size();Map
<Integer
, List
<Integer>> indices
= new HashMap<Integer
, List
<Integer>>();int xMin
= Integer
.MAX_VALUE
, xMax
= Integer
.MIN_VALUE
;for (int i
= 0; i
< size
; i
++) {for (int x
: nums
.get(i
)) {List
<Integer> list
= indices
.getOrDefault(x
, new ArrayList<Integer>());list
.add(i
);indices
.put(x
, list
);xMin
= Math
.min(xMin
, x
);xMax
= Math
.max(xMax
, x
);}}int[] freq
= new int[size
];int inside
= 0;int left
= xMin
, right
= xMin
- 1;int bestLeft
= xMin
, bestRight
= xMax
;while (right
< xMax
) {right
++;if (indices
.containsKey(right
)) {for (int x
: indices
.get(right
)) {freq
[x
]++;if (freq
[x
] == 1) {inside
++;}}while (inside
== size
) {if (right
- left
< bestRight
- bestLeft
) {bestLeft
= left
;bestRight
= right
;}if (indices
.containsKey(left
)) {for (int x
: indices
.get(left
)) {freq
[x
]--;if (freq
[x
] == 0) {inside
--;}}}left
++;}}}return new int[]{bestLeft
, bestRight
};}
【總結】
1.滑動窗口核心思想
int left
= 0, right
= 0;while (right
< s
.size()) {`window
.add(s
[right
]);right
++;while (window needs shrink
) {window
.remove(s
[left
]);left
++;}
}
2.Map-map.getOrDefault()用法
為什么要用?
Map中在每個數據上進行累加我必須先給每個key賦初值才行 (如果不賦初值在計算一開始就會報空指針異常)
API
解釋
Map.getOrDefault(Object key, V defaultValue)方法的作用是:
??(1)當Map集合中存在這個key時,就使用這個key值,(若是數值型可以在此基礎上進行運算)
??(2)如果沒有就使用默認值defaultValue
3.困難題目不在于思想的理解 而在于每句代碼的理解
轉載:https://leetcode-cn.com/problems/smallest-range-covering-elements-from-k-lists/solution/zui-xiao-qu-jian-by-leetcode-solution/
參考鏈接:https://blog.csdn.net/weixin_44325444/article/details/106306900?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
總結
以上是生活随笔為你收集整理的[Leetcode][第632题][JAVA][最小区间][堆][滑动窗口]的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。