Skip to content

Commit

Permalink
658
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonukim committed Jan 12, 2025
1 parent 8b94876 commit 3ef4f80
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
4 changes: 2 additions & 2 deletions _posts/2025-01-10-Leetcode-209.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
6 changes: 3 additions & 3 deletions _posts/2025-01-10-Leetcode-219.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
70 changes: 70 additions & 0 deletions _posts/2025-01-11-Leetcode-658.md
Original file line number Diff line number Diff line change
@@ -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)$

0 comments on commit 3ef4f80

Please sign in to comment.