mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
feat(work): add solutions for problems 42, 209, and 34
This commit is contained in:
1 parent
a19fd95cc4
commit
9899813281
3 files changed
+184
No files matched your search
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
42. Trapping Rain Water
|
||||
Difficulty: Hard
|
||||
https://leetcode.com/problems/trapping-rain-water/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given n non-negative integers representing an elevation map where the
|
||||
width of each bar is 1, compute how much water it can trap after
|
||||
raining.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
|
||||
Output: 6
|
||||
Explanation: The above elevation map (black section) is represented
|
||||
by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain
|
||||
water (blue section) are being trapped.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: height = [4,2,0,3,2,5]
|
||||
Output: 9
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• n == height.length
|
||||
|
||||
• 1 <= n <= 2 * 10^4
|
||||
|
||||
• 0 <= height[i] <= 10^5
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def trap(self, height: list[int]) -> int:
|
||||
left, right = 0, len(height) - 1
|
||||
leftMax, rightMax = 0, 0
|
||||
total = 0
|
||||
|
||||
while left < right:
|
||||
leftMax = max(leftMax, height[left])
|
||||
rightMax = max(rightMax, height[right])
|
||||
|
||||
if height[left] < height[right]:
|
||||
total += leftMax - height[left]
|
||||
left += 1
|
||||
else:
|
||||
total += rightMax - height[right]
|
||||
right -= 1
|
||||
|
||||
return total
|
||||
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
209. Minimum Size Subarray Sum
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/minimum-size-subarray-sum/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given an array of positive integers nums and a positive integer
|
||||
target, return the minimal length of a subarray whose sum is greater
|
||||
than or equal to target. If there is no such subarray, return 0
|
||||
instead.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: target = 7, nums = [2,3,1,2,4,3]
|
||||
Output: 2
|
||||
Explanation: The subarray [4,3] has the minimal length under the
|
||||
problem constraint.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: target = 4, nums = [1,4,4]
|
||||
Output: 1
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
|
||||
Output: 0
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= target <= 10^9
|
||||
|
||||
• 1 <= nums.length <= 10^5
|
||||
|
||||
• 1 <= nums[i] <= 10^4
|
||||
|
||||
|
||||
|
||||
Follow up: If you have figured out the O(n) solution, try coding
|
||||
another solution of which the time complexity is O(n log(n)).
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def minSubArrayLen(self, target: int, nums: list[int]) -> int:
|
||||
window_sum = 0
|
||||
length = len(nums) + 1
|
||||
left = 0
|
||||
|
||||
for right in range(len(nums)):
|
||||
window_sum += nums[right]
|
||||
while window_sum >= target:
|
||||
length = min(length, right - left + 1)
|
||||
window_sum -= nums[left]
|
||||
left += 1
|
||||
if length > len(nums):
|
||||
return 0
|
||||
return length
|
||||
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
34. Find First and Last Position of Element in Sorted Array
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given an array of integers nums sorted in non-decreasing order, find
|
||||
the starting and ending position of a given target value.
|
||||
|
||||
If target is not found in the array, return [-1, -1].
|
||||
|
||||
You must write an algorithm with O(log n) runtime complexity.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: nums = [5,7,7,8,8,10], target = 8
|
||||
Output: [3,4]
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: nums = [5,7,7,8,8,10], target = 6
|
||||
Output: [-1,-1]
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: nums = [], target = 0
|
||||
Output: [-1,-1]
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 0 <= nums.length <= 10^5
|
||||
|
||||
• -10^9 <= nums[i] <= 10^9
|
||||
|
||||
• nums is a non-decreasing array.
|
||||
|
||||
• -10^9 <= target <= 10^9
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def searchRange(self, nums: list[int], target: int) -> list[int]:
|
||||
def find(first):
|
||||
result = -1
|
||||
left, right = 0, len(nums) - 1
|
||||
while left <= right:
|
||||
mid = (left + right) // 2
|
||||
if nums[mid] == target:
|
||||
result = mid
|
||||
if first:
|
||||
right = mid - 1
|
||||
else:
|
||||
left = mid + 1
|
||||
elif nums[mid] < target:
|
||||
left = mid + 1
|
||||
else:
|
||||
right = mid - 1
|
||||
return result
|
||||
|
||||
return [find(True), find(False)]
|
||||
Reference in new issue
Block a user