Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fc6de3

Browse files
Merge pull request #8 from borrelunde/problem_0035_search_insert_position
LeetCode problem: 35. Search Insert Position
2 parents 4a8c73d + 84305cf commit 6fc6de3

File tree

3 files changed

+245
-0
lines changed
  • src

3 files changed

+245
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 35. Search Insert Position
2+
3+
Difficulty: `Easy`
4+
Topics: `Array`, `Binary Search`
5+
6+
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return
7+
the index where it would be if it were inserted in order.
8+
9+
You must write an algorithm with `O(log n)` runtime complexity.
10+
11+
**Example 1:**
12+
13+
```text
14+
Input: nums = [1,3,5,6], target = 5
15+
Output: 2
16+
```
17+
18+
**Example 2:**
19+
20+
```text
21+
Input: nums = [1,3,5,6], target = 2
22+
Output: 1
23+
```
24+
25+
**Example 3:**
26+
27+
```text
28+
Input: nums = [1,3,5,6], target = 7
29+
Output: 4
30+
```
31+
32+
**Constraints:**
33+
34+
- `1 <= nums.length <= 10^4`
35+
- `-10^4 <= nums[i] <= 10^4`
36+
- `nums` contains **distinct** values sorted in **ascending** order.
37+
- `-10^4 <= target <= 10^4`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.borrelunde.leetcodesolutions.problem0035.searchinsertposition;
2+
3+
/**
4+
* This is the solution to the LeetCode problem: 35. Search Insert Position
5+
*
6+
* @author Børre A. Opedal Lunde
7+
* @since 2024年01月28日
8+
*/
9+
public class Solution {
10+
11+
public int searchInsert(int[] nums, int target) {
12+
13+
int low = 0;
14+
int high = nums.length - 1;
15+
16+
int middleIndex = 0;
17+
int middleValue = 0;
18+
19+
// Binary search. This is documented more in the solution to the
20+
// LeetCode problem 704. Binary Search.
21+
while (low <= high) {
22+
23+
middleIndex = low + (high - low) / 2;
24+
middleValue = nums[middleIndex];
25+
26+
if (middleValue == target) {
27+
return middleIndex;
28+
} else if (middleValue > target) {
29+
high = middleIndex - 1;
30+
} else {
31+
low = middleIndex + 1;
32+
}
33+
}
34+
35+
// If we get here, the binary search didn't find the target. So, let's
36+
// insert it. If the target is less than the value at the middle index,
37+
// we simply replace that. Otherwise, after it.
38+
return target < middleValue
39+
? middleIndex // Take the place of the existing element.
40+
: middleIndex + 1;
41+
}
42+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.borrelunde.leetcodesolutions.problem0035.searchinsertposition;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Nested;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
/**
10+
* This is the test to the LeetCode problem: 35. Search Insert Position
11+
*
12+
* @author Børre A. Opedal Lunde
13+
* @since 2024年01月28日
14+
*/
15+
@DisplayName("Search Insert Position")
16+
class SolutionTest {
17+
18+
private final Solution solution = new Solution();
19+
20+
@Test
21+
@DisplayName("Example one")
22+
void exampleOne() {
23+
int[] nums = {1, 3, 5, 6};
24+
int target = 5;
25+
26+
int expected = 2;
27+
int actual = solution.searchInsert(nums, target);
28+
29+
assertEquals(expected, actual);
30+
}
31+
32+
@Test
33+
@DisplayName("Example two")
34+
void exampleTwo() {
35+
int[] nums = {1, 3, 5, 6};
36+
int target = 2;
37+
38+
int expected = 1;
39+
int actual = solution.searchInsert(nums, target);
40+
41+
assertEquals(expected, actual);
42+
}
43+
44+
@Test
45+
@DisplayName("Example three")
46+
void exampleThree() {
47+
int[] nums = {1, 3, 5, 6};
48+
int target = 7;
49+
50+
int expected = 4;
51+
int actual = solution.searchInsert(nums, target);
52+
53+
assertEquals(expected, actual);
54+
}
55+
56+
@Nested
57+
@DisplayName("Given array with one element")
58+
class GivenArrayWithOneElement {
59+
60+
final int[] nums = {1};
61+
62+
@Test
63+
@DisplayName("Should return index 0 when target is less than the element")
64+
void shouldReturnIndex0WhenTargetIsLessThanTheElement() {
65+
int expected = 0;
66+
int actual = solution.searchInsert(nums, Integer.MIN_VALUE);
67+
68+
assertEquals(expected, actual);
69+
}
70+
71+
@Test
72+
@DisplayName("Should return its index when target is equal to the element")
73+
void shouldReturnItsIndexWhenTargetIsEqualToTheElement() {
74+
int expected = 0;
75+
int actual = solution.searchInsert(nums, 1);
76+
77+
assertEquals(expected, actual);
78+
}
79+
80+
@Test
81+
@DisplayName("Should return index 1 when target is greater than the element")
82+
void shouldReturnIndex1WhenTargetIsGreaterThanTheElement() {
83+
int expected = 1;
84+
int actual = solution.searchInsert(nums, Integer.MAX_VALUE);
85+
86+
assertEquals(expected, actual);
87+
}
88+
}
89+
90+
@Nested
91+
@DisplayName("Given array with two elements")
92+
class GivenArrayWithTwoElements {
93+
94+
final int[] nums = {1, 3};
95+
96+
@Test
97+
@DisplayName("Should return index 0 when target is less than the first element")
98+
void shouldReturnIndex0WhenTargetIsLessThanTheFirstElement() {
99+
int expected = 0;
100+
int actual = solution.searchInsert(nums, Integer.MIN_VALUE);
101+
102+
assertEquals(expected, actual);
103+
}
104+
105+
@Test
106+
@DisplayName("Should return index 0 when target is equal to the first element")
107+
void shouldReturnIndex0WhenTargetIsEqualToTheFirstElement() {
108+
int expected = 0;
109+
int actual = solution.searchInsert(nums, 1);
110+
111+
assertEquals(expected, actual);
112+
}
113+
114+
@Test
115+
@DisplayName("Should return index 1 when target is greater than the first element and less than the second element")
116+
void shouldReturnIndex1WhenTargetIsGreaterThanTheFirstElementAndLessThanTheSecondElement() {
117+
int expected = 1;
118+
int actual = solution.searchInsert(nums, 2);
119+
120+
assertEquals(expected, actual);
121+
}
122+
123+
@Test
124+
@DisplayName("Should return index 1 when target is equal to the second element")
125+
void shouldReturnIndex1WhenTargetIsEqualToTheSecondElement() {
126+
int expected = 1;
127+
int actual = solution.searchInsert(nums, 3);
128+
129+
assertEquals(expected, actual);
130+
}
131+
132+
@Test
133+
@DisplayName("Should return index 2 when target is greater than the second element")
134+
void shouldReturnIndex2WhenTargetIsGreaterThanTheSecondElement() {
135+
int expected = 2;
136+
int actual = solution.searchInsert(nums, Integer.MAX_VALUE);
137+
138+
assertEquals(expected, actual);
139+
}
140+
}
141+
142+
@Nested
143+
@DisplayName("Given array with only the same element")
144+
class GivenArrayWithOnlyTheSameElement {
145+
146+
final int[] nums = {1, 1, 1, 1, 1, 1, 1, 1, 1};
147+
148+
@Test
149+
@DisplayName("Should return index 0 when target is less than the element")
150+
void shouldReturnIndex0WhenTargetIsLessThanTheElement() {
151+
int expected = 0;
152+
int actual = solution.searchInsert(nums, Integer.MIN_VALUE);
153+
154+
assertEquals(expected, actual);
155+
}
156+
157+
@Test
158+
@DisplayName("Should return index 9 when target is greater than the element")
159+
void shouldReturnIndex9WhenTargetIsGreaterThanTheElement() {
160+
int expected = 9;
161+
int actual = solution.searchInsert(nums, Integer.MAX_VALUE);
162+
163+
assertEquals(expected, actual);
164+
}
165+
}
166+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /