Skip to content

Commit 2703d35

Browse files
authored
Added task 2089.
1 parent 1d35af5 commit 2703d35

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2001_2100.s2089_find_target_indices_after_sorting_array;
2+
3+
// #Easy #Array #Sorting #Binary_Search #2022_05_27_Time_1_ms_(97.90%)_Space_42.5_MB_(83.38%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> targetIndices(int[] nums, int target) {
10+
int count = 0;
11+
int lessthan = 0;
12+
for (int n : nums) {
13+
if (n == target) {
14+
count++;
15+
}
16+
if (n < target) {
17+
lessthan++;
18+
}
19+
}
20+
List<Integer> result = new ArrayList<>();
21+
for (int i = 0; i < count; i++) {
22+
result.add(lessthan++);
23+
}
24+
return result;
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2089\. Find Target Indices After Sorting Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` and a target element `target`.
6+
7+
A **target index** is an index `i` such that `nums[i] == target`.
8+
9+
Return _a list of the target indices of_ `nums` after _sorting_ `nums` _in **non-decreasing** order_. If there are no target indices, return _an **empty** list_. The returned list must be sorted in **increasing** order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,5,2,3], target = 2
14+
15+
**Output:** [1,2]
16+
17+
**Explanation:** After sorting, nums is [1,**2**,**2**,3,5].
18+
19+
The indices where nums[i] == 2 are 1 and 2.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,5,2,3], target = 3
24+
25+
**Output:** [3]
26+
27+
**Explanation:** After sorting, nums is [1,2,2,**3**,5].
28+
29+
The index where nums[i] == 3 is 3.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,2,5,2,3], target = 5
34+
35+
**Output:** [4]
36+
37+
**Explanation:** After sorting, nums is [1,2,2,3,**5**].
38+
39+
The index where nums[i] == 5 is 4.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* `1 <= nums[i], target <= 100`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2001_2100.s2089_find_target_indices_after_sorting_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void targetIndices() {
12+
assertThat(
13+
new Solution().targetIndices(new int[] {1, 2, 5, 2, 3}, 2),
14+
equalTo(Arrays.asList(1, 2)));
15+
}
16+
17+
@Test
18+
void targetIndices2() {
19+
assertThat(
20+
new Solution().targetIndices(new int[] {1, 2, 5, 2, 3}, 3),
21+
equalTo(Arrays.asList(3)));
22+
}
23+
24+
@Test
25+
void targetIndices3() {
26+
assertThat(
27+
new Solution().targetIndices(new int[] {1, 2, 5, 2, 3}, 5),
28+
equalTo(Arrays.asList(4)));
29+
}
30+
}

0 commit comments

Comments
 (0)