Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8426224

Browse files
add 658
1 parent bf327b1 commit 8426224

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|659|[Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_659.java) | O(n) | O(n) | Medium | HashMap
26+
|658|[Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_658.java) | O(n) | O(1) | Medium |
2627
|657|[Judge Route Circle](https://leetcode.com/problems/judge-route-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_657.java) | O(n) | O(1) | Easy |
2728
|654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_654.java) | O(n^2) | O(n) | Medium | Tree
2829
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_653.java) | | | Easy | Tree
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 658. Find K Closest Elements
8+
*
9+
* Given a sorted array, two integers k and x,
10+
* find the k closest elements to x in the array.
11+
* The result should also be sorted in ascending order.
12+
* If there is a tie, the smaller elements are always preferred.
13+
14+
Example 1:
15+
Input: [1,2,3,4,5], k=4, x=3
16+
Output: [1,2,3,4]
17+
18+
Example 2:
19+
Input: [1,2,3,4,5], k=4, x=-1
20+
Output: [1,2,3,4]
21+
22+
Note:
23+
The value k is positive and will always be smaller than the length of the sorted array.
24+
Length of the given array is positive and will not exceed 104
25+
Absolute value of elements in the array and x will not exceed 104
26+
*/
27+
public class _658 {
28+
public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
29+
List<Integer> result = new ArrayList<>();
30+
int index = findInsertPosition(arr, x);
31+
int right = 0;
32+
if (arr.get(index) == x) {
33+
result.add(arr.get(index));
34+
right = index + 1;
35+
k--;
36+
} else {
37+
right = index;
38+
}
39+
int left = index - 1;
40+
while (k-- > 0) {
41+
if (left >= 0 && right < arr.size()) {
42+
if (x - arr.get(left) <= arr.get(right) - x) {
43+
result.add(0, arr.get(left--));
44+
} else {
45+
result.add(result.size(), arr.get(right++));
46+
}
47+
} else if (left >= 0) {
48+
result.add(0, arr.get(left--));
49+
} else if (right < arr.size()) {
50+
result.add(result.size(), arr.get(right++));
51+
}
52+
}
53+
return result;
54+
}
55+
56+
private int findInsertPosition(List<Integer> arr, int x) {
57+
if (arr == null || arr.size() == 0) return 0;
58+
int len = arr.size();
59+
for (int i = 0; i < len; i++) {
60+
if (arr.get(0) > x) {
61+
return 0;
62+
} else if (arr.get(len-1) < x) {
63+
return len;
64+
} else if (arr.get(i) < x && arr.get(i+1) > x) {
65+
return i+1;
66+
} else if (arr.get(i) == x) {
67+
return i;
68+
}
69+
}
70+
return len;
71+
}
72+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._658;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class _658Test {
14+
private static _658 test;
15+
private static List<Integer> arr;
16+
private static List<Integer> expected;
17+
private static int k;
18+
private static int x;
19+
20+
@BeforeClass
21+
public static void setup(){
22+
test = new _658();
23+
}
24+
25+
@Test
26+
public void test1(){
27+
k = 4;
28+
x = 3;
29+
expected = new ArrayList<>(Arrays.asList(1,2,3,4));
30+
arr = new ArrayList<>(Arrays.asList(1,2,3,4,5));
31+
assertEquals(expected, test.findClosestElements(arr, k, x));
32+
}
33+
34+
@Test
35+
public void test2(){
36+
k = 4;
37+
x = -1;
38+
expected = new ArrayList<>(Arrays.asList(1,2,3,4));
39+
arr = new ArrayList<>(Arrays.asList(1,2,3,4,5));
40+
assertEquals(expected, test.findClosestElements(arr, k, x));
41+
}
42+
43+
@Test
44+
public void test3(){
45+
k = 3;
46+
x = 5;
47+
arr = new ArrayList<>(Arrays.asList(0,0,1,2,3,3,4,7,7,8));
48+
expected = new ArrayList<>(Arrays.asList(3,3,4));
49+
assertEquals(expected, test.findClosestElements(arr, k, x));
50+
}
51+
}

0 commit comments

Comments
 (0)