当前位置:网站首页>Leetcode: 408 sliding window median
Leetcode: 408 sliding window median
2022-07-04 11:42:00 【w7486】
The median is the number in the middle of an ordered sequence . If the length of the sequence is even , There is no middle number ; At this point, the median is the average of the two most intermediate numbers .
for example :
[2,3,4], The median is 3
[2,3], The median is (2 + 3) / 2 = 2.5
Give you an array nums, There is a length of k From the left to the right . In the window k Number , Every time the window moves to the right 1 position . Your task is to find out the median number of elements in the new window after each window move , And output an array of them .Example :
give nums = [1,3,-1,-3,5,3,6,7], as well as k = 3.
window position Median
--------------- -----
[1 3 -1] -3 5 3 6 7 1
1 [3 -1 -3] 5 3 6 7 -1
1 3 [-1 -3 5] 3 6 7 -1
1 3 -1 [-3 5 3] 6 7 3
1 3 -1 -3 [5 3 6] 7 5
1 3 -1 -3 5 [3 6 7] 6
therefore , Returns the median array of the sliding window [1,-1,-1,3,5,6].Tips :
You can assume k Always effective , namely :k Always less than or equal to the number of elements of the input non empty array .
The error with the true value is within 10 ^ -5 The answer within will be regarded as the correct answer .
The first idea is to traverse the array , Take out a window array every time you traverse , Then get the results according to the rules and put them in the result array .
Code:
class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
double[] res = new double[nums.length-k+1];
for (int i = 0; i + k <= nums.length; i++) {
Array of sliding windows
double[] arr = new double[k];
for (int j = 0; j < k; j++) {
arr[j] = nums[j+i];
}
// stay arr Get the median in
Arrays.sort(arr);
int mid = k / 2;
res[i] = k % 2 == 0 ? (arr[mid-1] + arr[mid])/2 : arr[mid];
}
return res;
}
}
边栏推荐
猜你喜欢
SQL greatest() function instance detailed example
QQ group administrators
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Practical dry goods: deploy mini version message queue based on redis6.0
Serialization oriented - pickle library, JSON Library
Usage of case when then else end statement
Realize cross tenant Vnet connection through azure virtual Wan
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
2018 meisai modeling summary +latex standard meisai template sharing
How to judge the advantages and disadvantages of low code products in the market?
随机推荐
QQ get group information
Review of week 278 of leetcode II
Elevator dispatching (pairing project) ②
LVS+Keepalived实现四层负载及高可用
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
[ES6] template string: `string`, a new symbol in es2015
Heartbeat error attempted replay attack
Serialization oriented - pickle library, JSON Library
Analysis function in SQL
试题库管理系统–数据库设计[通俗易懂]
QQ one click cookie acquisition
Install freeradius3 in the latest version of openwrt
C language memory layout
Simple understanding of seesion, cookies, tokens
First knowledge of spark - 7000 words +15 diagrams, and learn the basic knowledge of spark
Detailed explanation of classic process synchronization problems
SSH原理和公钥认证
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
Usage of with as
No response after heartbeat startup