高级农民
- 积分
- 1187
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-9-6
- 最后登录
- 1970-1-1
|
第十六天,数组类里面的sliding window,还有prefix sum
239
| | Array
SlidingWindow
近期高频
| Given an array nums, there is a sliding window of size k which is moving from
the very left of the array to the very left. You can only see the k numbers in
the window. Each time the sliding window moves left by one position.
Return the max sliding window.
Example:
Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output:[3,3,5,5,6,7]
Explanation:
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
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 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
| 这题给定一个数组和一个window的长度,窗口从头移动到尾,
要求返回一个数组包含了每个窗口的最大值。这题解法是使用双端队列,
主要思路就是我们把当前窗口最大值放在deque最左边,当窗口要移动的时候就把最左边的值
拿出来当作当前窗口的最大值加入结果中。
从头遍历数组,4个操作:
- 当deque里面最左边的数超出了window的范围就移除,保证deque里面的数在window里面
- 如果当前数组的值num大于deuqe最右边的值,就移除掉deque右边的值直到deque最右边大于等于当前num或者为空。这一步保证了deque最左边的值为当前窗口最大值。
例子: [1, 3, -1, -3, 5, 3, 6, 7]
i = 0 dq = [1] result = [] dq为空,跳过第1,2步
i = 1 dq = [3] result = [] 3大于1,移除1
i = 2 dq = [3, -1] result = [3] -1不大于dq最右边数, 当窗口长度等于k时开始加到结果
i = 3 dq = [3, -1, -3] result = [3, 3] -3不大于dq最右边数, 加当前最大值3
i = 4 dq = [5] result = [3,3,5] dq最左边3超出window移除,dq从右到左最右边数比5小
i = 5 dq = [5, 3] result = [3,3,5,5] 5在当前window且3不大于dq最右边
i = 6 dq = [6] result = [3,3,5,5,6] 6比dq从右到左所以数都大
i = 7 dq = [7] result = [3,3,5,5,6,7] 7比dq从右到左所以数都大
有一点要注意就是这里演示方便用的值,其实我们要存index到dq,
才好检查他是否在当前窗口里面。
| 295
| | Array
heap
近期高频
| 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 value.
For example,
[2,3,4], the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
Example:
addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
| 这题要设计寻找当前数据的中位数的方法,
一个用来往里面加数,一个用来返回当前中位数。
思路其实挺简单,就是用两个堆,分别用来存小的和大的部分,
然后返回中位数的时候只需检测两个堆长度是否相等,
如果相等就返回小的部分的最大值和大的部分最小值的平均值,
不相等就返回大的部分的最小值。实现的话取决于语言,java可以维护最大堆和最小堆,
python默认最小堆要维护一个最大堆就把数取负放进去。
| 53
| | Array
DP
prefix sum
高频
| Given an integer array nums, find the contiguous subarray
which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
| 从一个数组中找子数组使其组成的和最大, 这题首先需要算当前子数组的和,
就是说要控制子数组的window,如何控制呢?
遍历过程就是窗口右边界, 关键在何时移动左边界,
每一次右指针移动我们就比较当前window里面数的和与当前数的大小,当前数大,
说明前面window子数组和还没有当前的大爷大,所以窗口左指针移动到你大爷这,
然后继续看下一个。
这题使用一个一位数组,dp 表示i这个位置子数组能取到最大的和。优化一下我们可以不用数组达到O(1) space, 稍微看一下知道我们只需要记录前一位子数组和就好了。
| 325
| | Array
prefix sum
HashTable
低频
| Given an array nums and a target value k,
find the maximum length of a subarray that sums to k.
If there isn't one, return 0 instead.
Input: nums = [1, -1, 5, -2, 3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
| 和two sum类似,运用prefix sum的思路来确定子数组和。公式如下,
preSum[0:i] = preSum[i:n] - k
例子: [1, -1, 5, -2, 3] k=3
prefixsum [1, 0, 5, 3, 6]
map [1:0, 0:1, 5:2, 3:3, 6:4]
index为3时候,取prefix sum为3-3=0的index截取子数组,更新长度。
| 209
| | Array
sliding window
中频
| Given an array of n positive integers and a positive integer s,
find the minimal length of a contiguous subarray of which the sum ≥ s.
If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
| 求最短的子数组,其和大于等于给定s的值。然后还是sliding window和prefix sum。
解体思路就是维护一个窗口,当前窗口和小于s时候,移动右指针;
当窗口和大于等于s时候,先跟新最短长度,然后移动左窗口直到小于s。
| 238
| | Array
超高频
| Given an array nums of n integers where n > 1, return an array output such
that output is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O([i]n).
[/i][/i] | 要求数组中每个数除开自己的所有数的乘积,不让用除法,所以得另外想办法。
思路还是从prefix product开始,我们可以看出这样可以得到每个数左边乘积的一个数组,
同理我们可以算出每个数右边数的乘积,然后把两个乘积乘起来就是除开自己的所有数乘积。
nums #1 , 2, 3, 4
左边乘积 #1 , 1, 2, 6
nums #1 , 2, 3, 4
右边乘积 #24, 12, 4, 1
| 152
| | Array, prefixsum
中频
| Given an integer array nums, find the contiguous subarray within an array
(containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
| 要求找出子数组的最大乘积,这题有负数。
这题思路和上题类似,也是算出左右prefix product,我们可以看出最大值为6,
当然解体时只需一个变量保存当前最大值就好了,不用开数组。
nums #2, 3, -2, 4
左边乘积 #2, 6, -24, -96
右边乘积 #-96, -24, -8 ,4
这题还有一个trick,就是0出现的时候,你后面子数组乘积就全为0了,所以我们遇见0了以后要从新计算乘积。
| 228
| | Array
Capital one高频
| Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
| 在排序数组里面找出连续的数字的起始和终止位置,比较简单,
遍历数组检测是否连续,连续就继续,不连续就把当前窗口左右边界加入结果,
移动窗口到当前位置。
| 163
| | Array
G家中低频
| Given a sorted integer array nums, where the range of elements are
in the inclusive range[lower,upper], return its missing ranges.
Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output: ["2", "4->49", "51->74", "76->99"]
| 和上面一题差不多,用个pre变量记录当前range的起始,
需要检测pre和当前值的距离,一个数和大于1个数调整输出格式就好。
|
|
|