This commit is contained in:
parent
e522ef06bd
commit
6151b8b474
1 changed files with 18 additions and 0 deletions
18
medium/partition_array_such_that_maximum_difference_is_k.py
Normal file
18
medium/partition_array_such_that_maximum_difference_is_k.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def partitionArray(self, nums: List[int], k: int) -> int:
|
||||||
|
nums.sort()
|
||||||
|
left = 0
|
||||||
|
right = 0
|
||||||
|
result = 0
|
||||||
|
while right < len(nums):
|
||||||
|
if nums[left] + k < nums[right]:
|
||||||
|
left = right
|
||||||
|
result += 1
|
||||||
|
else:
|
||||||
|
right += 1
|
||||||
|
return result + 1
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue