Skip to content

Commit 0482e8f

Browse files
authored
Create largest-subarray-length-k.py
1 parent a559c93 commit 0482e8f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/largest-subarray-length-k.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def largestSubarray(self, nums, k):
6+
"""
7+
:type nums: List[int]
8+
:type k: int
9+
:rtype: List[int]
10+
"""
11+
left, right, l = 0, 1, 0
12+
while right+k-1 < len(nums) and right+l < len(nums):
13+
if nums[left+l] == nums[right+l]:
14+
l += 1
15+
continue
16+
if nums[left+l] > nums[right+l]:
17+
right += l+1
18+
else:
19+
left = max(right, min(left+l+1, len(nums)-k))
20+
right = left+1
21+
l = 0
22+
return nums[left:left+k]

0 commit comments

Comments
 (0)