From 3ef4f80ec4b6dfdbd584d5592b9362d0d9af79ae Mon Sep 17 00:00:00 2001 From: Eric Kim <48634064+hyeonukim@users.noreply.github.com> Date: Sat, 11 Jan 2025 17:28:47 -0800 Subject: [PATCH] 658 --- _posts/2025-01-10-Leetcode-209.md | 4 +- _posts/2025-01-10-Leetcode-219.md | 6 +-- _posts/2025-01-11-Leetcode-658.md | 70 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 _posts/2025-01-11-Leetcode-658.md diff --git a/_posts/2025-01-10-Leetcode-209.md b/_posts/2025-01-10-Leetcode-209.md index b640394..38b7be3 100644 --- a/_posts/2025-01-10-Leetcode-209.md +++ b/_posts/2025-01-10-Leetcode-209.md @@ -2,8 +2,8 @@ title: Leetcode 209. Minimum Size Subarray Sum description: Explanation for Leetcode 209 - Minimum Size Subarray Sum, and its solution in Python. date: 2025-01-10 -categories: [Leetcode, Sliding Windows, Medium] -tags: [Leetcode, Python, Study, Sliding Windows, Medium] +categories: [Leetcode, Sliding Window, Medium] +tags: [Leetcode, Python, Study, Sliding Window, Medium] math: true --- diff --git a/_posts/2025-01-10-Leetcode-219.md b/_posts/2025-01-10-Leetcode-219.md index 213f9d1..2ae1a8f 100644 --- a/_posts/2025-01-10-Leetcode-219.md +++ b/_posts/2025-01-10-Leetcode-219.md @@ -2,13 +2,13 @@ title: Leetcode 219. Contains Duplicate II description: Explanation for Leetcode 219 - Contains Duplicate II, and its solution in Python. date: 2025-01-10 -categories: [Leetcode, Sliding Windows, Easy] -tags: [Leetcode, Python, Study, Sliding Windows, Easy] +categories: [Leetcode, Sliding Window, Easy] +tags: [Leetcode, Python, Study, Sliding Window, Easy] math: true --- ## Problem -[Leetcode 219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/submissions/1504764844/) +[Leetcode 219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/description/) Example: ``` diff --git a/_posts/2025-01-11-Leetcode-658.md b/_posts/2025-01-11-Leetcode-658.md new file mode 100644 index 0000000..4a14f64 --- /dev/null +++ b/_posts/2025-01-11-Leetcode-658.md @@ -0,0 +1,70 @@ +--- +title: Leetcode 658. Find K Closest Elements +description: Explanation for Leetcode 658 - Find K Closest Elements, and its solution in Python. +date: 2025-01-11 +categories: [Leetcode, Sliding Window, Medium] +tags: [Leetcode, Python, Study, Sliding Window, Medium] +math: true +--- + +## Problem +[Leetcode 658 - Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/description/) + + +Example: +``` +Input: arr = [1,2,3,4,5], k = 4, x = 3 +Output: [1,2,3,4] + +Input: arr = [1,1,2,3,4,5], k = 4, x = -1 +Output: [1,1,2,3] +``` + +## Approach + +Since we need to return k elements taht are closest to x, and the input array is sorted. We can use binary search to find the window that is closest to x. + +Our initial left = 0, while right = len(arr) - k because we need extra k space for the return array + +If arr[mid] is farther from target than arr[mid+k] which is k places ahead of mid then we need to pull left to mid with 1 offset; otherwise we can pull right at mid + +Visualization of the Approach: +``` +arr = [1,2,3,4,5], k = 2, x = 5 + l r +mid = 1, nums[mid] = 2 +x - arr[mid] > arr[mid+k] - x -> 5 - 1 > 4 - 5 -> 4 > -1. Thus left = mid+1 + +arr = [1,2,3,4,5] + l r +mid = 2, nums[mid] = 3 +x - arr[mid] > arr[mid+k] - x -> 5 - 3 > 5 - 5 -> 2 > 0. Thus left = mid+1 + +arr = [1,2,3,4,5] + lr +left == right stop while loop + +arr[left: left+k] = [4,5] +``` + +Here is the Python code for the solution: +```python +class Solution: + def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]: + left, right = 0, len(arr)-k + + while left < right: + mid = (left + right) // 2 + + if x - arr[mid] > arr[mid+k] - x: + left = mid+1 + else: + right = mid + + return arr[left: left+k] +``` +## Time Complexity and Space Complexity + +Time Complexity: $O(log(n-k)+k)$ + +Space Complexity: $O(k)$ \ No newline at end of file