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 26a0855

Browse files
Merge pull request #11 from borrelunde/problem_0034_find_first_and_last_position_of_element_in_sorted_array
LeetCode problem: 34. Find First and Last Position of Element in Sorted Array
2 parents 73f7d1a + a88d1fc commit 26a0855

File tree

3 files changed

+287
-0
lines changed
  • src
    • main/java/com/borrelunde/leetcodesolutions/problem0034/findfirstandlastpositionofelementinsortedarray
    • test/java/com/borrelunde/leetcodesolutions/problem0034/findfirstandlastpositionofelementinsortedarray

3 files changed

+287
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 34. Find First and Last Position of Element in Sorted Array
2+
3+
Difficulty: `Medium`
4+
Topics: `Array`, `Binary Search`
5+
6+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a
7+
given `target` value.
8+
9+
If `target` is not found in the array, return `[-1, -1]`.
10+
11+
You must write an algorithm with `O(log n)` runtime complexity.
12+
13+
**Example 1:**
14+
15+
```text
16+
Input: nums = [5,7,7,8,8,10], target = 8
17+
Output: [3,4]
18+
```
19+
20+
**Example 2:**
21+
22+
```text
23+
Input: nums = [5,7,7,8,8,10], target = 6
24+
Output: [-1,-1]
25+
```
26+
27+
**Example 3:**
28+
29+
```text
30+
Input: nums = [], target = 0
31+
Output: [-1,-1]
32+
```
33+
34+
**Constraints:**
35+
36+
- `0 <= nums.length <= 10^5`
37+
- `-10^9 <= nums[i] <= 10^9`
38+
- `nums` is a non-decreasing array.
39+
- `-10^9 <= target <= 10^9`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.borrelunde.leetcodesolutions.problem0034.findfirstandlastpositionofelementinsortedarray;
2+
3+
/**
4+
* This is the solution to the LeetCode problem: 34. Find First and Last
5+
* Position of Element in Sorted Array
6+
*
7+
* @author Børre A. Opedal Lunde
8+
* @since 2024年01月30日
9+
*/
10+
public class Solution {
11+
12+
public int[] searchRange(int[] nums, int target) {
13+
final int leftIndex = binarySearch(nums, target, true);
14+
final int rightIndex = binarySearch(nums, target, false);
15+
return new int[]{leftIndex, rightIndex};
16+
}
17+
18+
private static int binarySearch(int[] nums, int target, boolean first) {
19+
20+
int low = 0;
21+
int high = nums.length;
22+
23+
while (low < high) {
24+
25+
// Bitwise right shift (>> 1) divides by two to get the middle.
26+
final int searchIndex = low + ((high - low) >> 1);
27+
final int searchValue = nums[searchIndex];
28+
29+
// If the search value is equal to the target, we should check if
30+
// its index meets the criteria of either the leftmost instance of
31+
// the target or the rightmost instance of the target. Otherwise,
32+
// narrow the search in the direction required.
33+
if (searchValue == target) {
34+
35+
// This is a check to find either the leftmost instance of the
36+
// target or the rightmost instance of the target.
37+
if (first) {
38+
// If there is no index to the left, simply return the
39+
// search index.
40+
if (searchIndex == 0) {
41+
return searchIndex;
42+
}
43+
44+
// Otherwise, check if the left value is less than the
45+
// target. If it is, we know the search index is the
46+
// leftmost instance of target.
47+
final int leftValue = nums[searchIndex - 1];
48+
if (leftValue < target) {
49+
return searchIndex;
50+
}
51+
52+
// However, if left is not, narrow the search to the left.
53+
high = searchIndex;
54+
} else {
55+
// If there is no index to the right, simply return the
56+
// search index. As above, but in the other direction.
57+
if (searchIndex >= nums.length - 1) {
58+
return searchIndex;
59+
}
60+
61+
// Otherwise, check if the right value is less than the
62+
// target. If it is, we know the search index is the
63+
// rightmost instance of target.
64+
final int rightValue = nums[searchIndex + 1];
65+
if (rightValue > target) {
66+
return searchIndex;
67+
}
68+
69+
// However, if right is not, narrow the search to the right.
70+
low = searchIndex + 1;
71+
}
72+
} else if (searchValue > target) {
73+
high = searchIndex;
74+
} else {
75+
low = searchIndex + 1;
76+
}
77+
}
78+
79+
// Return invalid because the target was not found.
80+
return - 1;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.borrelunde.leetcodesolutions.problem0034.findfirstandlastpositionofelementinsortedarray;
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.assertArrayEquals;
8+
9+
/**
10+
* This is the test to the LeetCode problem: 34. Find First and Last Position of
11+
* Element in Sorted Array
12+
*
13+
* @author Børre A. Opedal Lunde
14+
* @since 2024年01月30日
15+
*/
16+
@DisplayName("Find First and Last Position of Element in Sorted Array")
17+
class SolutionTest {
18+
19+
private final Solution solution = new Solution();
20+
21+
private static final int MINIMUM = - 1_000_000_000;
22+
private static final int MAXIMUM = 1_000_000_000;
23+
24+
@Test
25+
@DisplayName("Example one")
26+
void exampleOne() {
27+
int[] nums = {5, 7, 7, 8, 8, 10};
28+
int target = 8;
29+
30+
int[] expected = new int[]{3, 4};
31+
int[] actual = solution.searchRange(nums, target);
32+
33+
assertArrayEquals(expected, actual);
34+
}
35+
36+
@Test
37+
@DisplayName("Example two")
38+
void exampleTwo() {
39+
int[] nums = {5, 7, 7, 8, 8, 10};
40+
int target = 6;
41+
42+
int[] expected = new int[]{- 1, - 1};
43+
int[] actual = solution.searchRange(nums, target);
44+
45+
assertArrayEquals(expected, actual);
46+
}
47+
48+
@Test
49+
@DisplayName("Should return invalid when array is empty")
50+
void shouldReturnInvalidWhenArrayIsEmpty() {
51+
int[] nums = {};
52+
int target = 0;
53+
54+
int[] expected = new int[]{- 1, - 1};
55+
int[] actual = solution.searchRange(nums, target);
56+
57+
assertArrayEquals(expected, actual);
58+
}
59+
60+
@Test
61+
@DisplayName("Should return the same element when the array contains the target only once")
62+
void shouldReturnTheSameElementWhenTheArrayContainsTheTargetOnlyOnce() {
63+
int[] nums = {- 2, - 1, 0, 1, 2};
64+
int target = 0;
65+
66+
int[] expected = new int[]{2, 2};
67+
int[] actual = solution.searchRange(nums, target);
68+
69+
assertArrayEquals(expected, actual);
70+
}
71+
72+
@Nested
73+
@DisplayName("When the array contains only the same number")
74+
class WhenTheArrayContainsOnlyTheSameNumber {
75+
76+
final int[] nums = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
77+
78+
@Test
79+
@DisplayName("Should return invalid when target is greater than that number")
80+
void shouldReturnInvalidWhenTargetIsGreaterThanThatNumber() {
81+
int target = 2;
82+
83+
int[] expected = {- 1, - 1};
84+
int[] actual = solution.searchRange(nums, target);
85+
86+
assertArrayEquals(expected, actual);
87+
}
88+
89+
@Test
90+
@DisplayName("Should return invalid when target is less than that number")
91+
void shouldReturnInvalidWhenTargetIsLessThanThatNumber() {
92+
int target = 0;
93+
94+
int[] expected = {- 1, - 1};
95+
int[] actual = solution.searchRange(nums, target);
96+
97+
assertArrayEquals(expected, actual);
98+
}
99+
100+
@Test
101+
@DisplayName("Should return first and last element position when target is that number")
102+
void shouldReturnFirstAndLastElementPositionWhenTargetIsThatNumber() {
103+
int target = 1;
104+
105+
int[] expected = {0, 9};
106+
int[] actual = solution.searchRange(nums, target);
107+
108+
assertArrayEquals(expected, actual);
109+
}
110+
}
111+
112+
@Nested
113+
@DisplayName("When the array contains the minimum number")
114+
class WhenTheArrayContainsTheMinimumNumber {
115+
116+
final int[] nums = {MINIMUM, MINIMUM, - 1, 0, 1, 2};
117+
118+
@Test
119+
@DisplayName("Should return first and last element when target is that number")
120+
void shouldReturnFirstAndLastElementWhenTargetIsThatNumber() {
121+
int[] expected = {0, 1};
122+
int[] actual = solution.searchRange(nums, MINIMUM);
123+
124+
assertArrayEquals(expected, actual);
125+
}
126+
127+
@Test
128+
@DisplayName("Should return first and last element when target is another number")
129+
void shouldReturnFirstAndLastElementWhenTargetIsAnotherNumber() {
130+
int target = 0;
131+
132+
int[] expected = {3, 3};
133+
int[] actual = solution.searchRange(nums, target);
134+
135+
assertArrayEquals(expected, actual);
136+
}
137+
}
138+
139+
@Nested
140+
@DisplayName("When the array contains the maximum number")
141+
class WhenTheArrayContainsTheMaximumNumber {
142+
143+
final int[] nums = {- 1, 0, 1, 2, MAXIMUM, MAXIMUM};
144+
145+
146+
@Test
147+
@DisplayName("Should return first and last element when target is that number")
148+
void shouldReturnFirstAndLastElementWhenTargetIsThatNumber() {
149+
int[] expected = {4, 5};
150+
int[] actual = solution.searchRange(nums, MAXIMUM);
151+
152+
assertArrayEquals(expected, actual);
153+
}
154+
155+
@Test
156+
@DisplayName("Should return first and last element when target is another number")
157+
void shouldReturnFirstAndLastElementWhenTargetIsAnotherNumber() {
158+
int target = 0;
159+
160+
int[] expected = {1, 1};
161+
int[] actual = solution.searchRange(nums, target);
162+
163+
assertArrayEquals(expected, actual);
164+
}
165+
}
166+
}

0 commit comments

Comments
(0)

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