当前位置:网站首页>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;
}
}
边栏推荐
- 三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
- Usage of case when then else end statement
- Dos and path
- os. Path built-in module
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
- QQ group administrators
- Daemon xinted and logging syslogd
- Elevator dispatching (pairing project) ③
- Heartbeat启动后无反应
- re. Sub() usage
猜你喜欢
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7](/img/44/1861f9016e959ed7c568721dd892db.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7

Analysis function in SQL

2020 Summary - Magic year, magic me

Ternsort model integration summary
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20](/img/d5/4bce239b522696b5312b1346336b5f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11](/img/6a/398d9cceecdd9d7c9c4613d8b5ca27.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11

Games101 Lesson 8 shading 2 Notes

DVC use case (VI): Data Registry
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5

Alibaba cloud server connection intranet operation
随机推荐
Elevator dispatching (pairing project) ④
Decrypt the advantages of low code and unlock efficient application development
Elevator dispatching (pairing project) ③
QQ get group link, QR code
Detailed array expansion analysis --- take you step by step analysis
2021-10-20
Local MySQL forget password modification method (Windows) [easy to understand]
Alibaba cloud server connection intranet operation
regular expression
OSI seven layer model & unit
Games101 Lesson 8 shading 2 Notes
03_ Armv8 instruction set introduction load and store instructions
CSDN documentation specification
Global function Encyclopedia
How to create a new virtual machine
Ternsort model integration summary
Iptables cause heartbeat brain fissure
Post man JSON script version conversion
MBG combat zero basis
Lvs+kept realizes four layers of load and high availability