当前位置:网站首页>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;
}
}
边栏推荐
- VPS安装Virtualmin面板
- Entitas learning [3] multi context system
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
- Games101 Lesson 8 shading 2 Notes
- [Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
- JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
- Elevator dispatching (pairing project) ②
- Elevator dispatching (pairing project) ④
- Application of slice
猜你喜欢
Summary of collection: (to be updated)
Elevator dispatching (pairing project) ④
Introduction to Lichuang EDA
Automatic translation between Chinese and English
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
OSI model notes
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Simple understanding of seesion, cookies, tokens
随机推荐
Reptile learning 3 (winter vacation learning)
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
Shift EC20 mode and switch
re. Sub() usage
Install freeradius3 in the latest version of openwrt
Configure SSH key to realize login free
Daemon xinted and logging syslogd
Automatic translation between Chinese and English
Day01 preliminary packet capture
Review of week 278 of leetcode II
OSI model notes
Global function Encyclopedia
Lvs+kept realizes four layers of load and high availability
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
LVS+Keepalived实现四层负载及高可用
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
Simple understanding of seesion, cookies, tokens
Climb Phoenix Mountain on December 19, 2021
[solve the error of this pointing in the applet] SetData of undefined
Notes on writing test points in mind mapping