Skip to content

Commit 788eb3d

Browse files
committed
binary search questions: done
1 parent d971393 commit 788eb3d

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.leetcode.arrays.binarysearch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Easy
7+
* Link: https://leetcode.com/problems/search-insert-position/
8+
* Description:
9+
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it
10+
* would be if it were inserted in order.
11+
* <p>
12+
* You may assume no duplicates in the array.
13+
* <p>
14+
* Example 1:
15+
* Input: [1,3,5,6], 5
16+
* Output: 2
17+
* <p>
18+
* Example 2:
19+
* Input: [1,3,5,6], 2
20+
* Output: 1
21+
* <p>
22+
* Example 3:
23+
* Input: [1,3,5,6], 7
24+
* Output: 4
25+
* <p>
26+
* Example 4:
27+
* Input: [1,3,5,6], 0
28+
* Output: 0
29+
*
30+
* Similar question: {@link SmallestLetterGreaterThanTarget}.
31+
*
32+
* @author rampatra
33+
* @since 2019-08-19
34+
*/
35+
public class SearchInsertPosition {
36+
37+
/**
38+
* Runtime: <a href="https://leetcode.com/submissions/detail/253066747/">0 ms</a>.
39+
*
40+
* @param nums
41+
* @param target
42+
* @return
43+
*/
44+
public static int searchInsert(int[] nums, int target) {
45+
int low = 0;
46+
int high = nums.length - 1;
47+
while (low <= high) {
48+
int mid = low + (high - low) / 2;
49+
if (nums[mid] == target) {
50+
return mid;
51+
} else if (nums[mid] < target) {
52+
low = mid + 1;
53+
} else {
54+
high = mid - 1;
55+
}
56+
}
57+
return low;
58+
}
59+
60+
public static void main(String[] args) {
61+
assertEquals(2, searchInsert(new int[]{1, 2}, 3));
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.leetcode.arrays.binarysearch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Easy
7+
* Link: https://leetcode.com/problems/find-smallest-letter-greater-than-target/
8+
* Description:
9+
* Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find
10+
* the smallest element in the list that is larger than the given target.
11+
*
12+
* Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.
13+
*
14+
* Examples:
15+
*
16+
* Input:
17+
* letters = ["c", "f", "j"]
18+
* target = "a"
19+
* Output: "c"
20+
*
21+
* Input:
22+
* letters = ["c", "f", "j"]
23+
* target = "c"
24+
* Output: "f"
25+
*
26+
* Input:
27+
* letters = ["c", "f", "j"]
28+
* target = "d"
29+
* Output: "f"
30+
*
31+
* Input:
32+
* letters = ["c", "f", "j"]
33+
* target = "g"
34+
* Output: "j"
35+
*
36+
* Input:
37+
* letters = ["c", "f", "j"]
38+
* target = "j"
39+
* Output: "c"
40+
*
41+
* Input:
42+
* letters = ["c", "f", "j"]
43+
* target = "k"
44+
* Output: "c"
45+
*
46+
* Note:
47+
* - letters has a length in range [2, 10000].
48+
* - letters consists of lowercase letters, and contains at least 2 unique letters.
49+
* - target is a lowercase letter.
50+
*
51+
* @author rampatra
52+
* @since 2019-08-19
53+
*/
54+
public class SmallestLetterGreaterThanTarget {
55+
56+
/**
57+
* Runtime: <a href="https://leetcode.com/submissions/detail/253061487/">0 ms</a>.
58+
*
59+
* @param letters
60+
* @param target
61+
* @return
62+
*/
63+
public static char nextGreatestLetter(char[] letters, char target) {
64+
int low = 0, hi = letters.length - 1;
65+
while (low <= hi) {
66+
int mid = low + (hi - low) / 2;
67+
if (letters[mid] <= target) {
68+
low = mid + 1;
69+
} else {
70+
hi = mid - 1;
71+
}
72+
}
73+
return letters[low % letters.length];
74+
}
75+
76+
public static void main(String[] args) {
77+
assertEquals('a', nextGreatestLetter(new char[]{'a'}, 'z'));
78+
assertEquals('b', nextGreatestLetter(new char[]{'a', 'b'}, 'a'));
79+
assertEquals('b', nextGreatestLetter(new char[]{'a', 'b', 'c'}, 'a'));
80+
assertEquals('a', nextGreatestLetter(new char[]{'a', 'b', 'c'}, 'z'));
81+
assertEquals('c', nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'a'));
82+
assertEquals('f', nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'c'));
83+
assertEquals('f', nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'd'));
84+
assertEquals('b', nextGreatestLetter(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l',
85+
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}, 'a'));
86+
}
87+
}

0 commit comments

Comments
 (0)