480. Sliding Window Median Hard

@problem@discussion
#Array#Hash Table#Sliding Window#Heap (Priority Queue)



1/**
2 * [480] Sliding Window Median
3 *
4 * The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.
5 * 
6 * 	For examples, if arr = [2,<u>3</u>,4], the median is 3.
7 * 	For examples, if arr = [1,<u>2,3</u>,4], the median is (2 + 3) / 2 = 2.5.
8 * 
9 * You are given an integer array nums and an integer k. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
10 * Return the median array for each window in the original array. Answers within 10^-5 of the actual value will be accepted.
11 *  
12 * Example 1:
13 * 
14 * Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
15 * Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
16 * Explanation: 
17 * Window position                Median
18 * ---------------                -----
19 * [1  3  -1] -3  5  3  6  7        1
20 *  1 [3  -1  -3] 5  3  6  7       -1
21 *  1  3 [-1  -3  5] 3  6  7       -1
22 *  1  3  -1 [-3  5  3] 6  7        3
23 *  1  3  -1  -3 [5  3  6] 7        5
24 *  1  3  -1  -3  5 [3  6  7]       6
25 * 
26 * Example 2:
27 * 
28 * Input: nums = [1,2,3,4,2,3,1,4,2], k = 3
29 * Output: [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= k <= nums.length <= 10^5
35 * 	-2^31 <= nums[i] <= 2^31 - 1
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/sliding-window-median/
41// discuss: https://leetcode.com/problems/sliding-window-median/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn median_sliding_window(nums: Vec<i32>, k: i32) -> Vec<f64> {
47        vec![]
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_480() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.