Skip to content

Commit 28322e7

Browse files
add 1389
1 parent cee2806 commit 28322e7

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java) | |Easy|Array|
1112
|1388|[Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1388.java) | |Hard|DP|
1213
|1387|[Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1387.java) | |Medium|Sort, Graph|
1314
|1386|[Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1386.java) | |Medium|Array, Greedy|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1389. Create Target Array in the Given Order
8+
*
9+
* Given two arrays of integers nums and index. Your task is to create target array under the following rules:
10+
* Initially target array is empty.
11+
* From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
12+
* Repeat the previous step until there are no elements to read in nums and index.
13+
* Return the target array.
14+
*
15+
* It is guaranteed that the insertion operations will be valid.
16+
*
17+
* Example 1:
18+
* Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
19+
* Output: [0,4,1,3,2]
20+
* Explanation:
21+
* nums index target
22+
* 0 0 [0]
23+
* 1 1 [0,1]
24+
* 2 2 [0,1,2]
25+
* 3 2 [0,1,3,2]
26+
* 4 1 [0,4,1,3,2]
27+
*
28+
* Example 2:
29+
* Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
30+
* Output: [0,1,2,3,4]
31+
* Explanation:
32+
* nums index target
33+
* 1 0 [1]
34+
* 2 1 [1,2]
35+
* 3 2 [1,2,3]
36+
* 4 3 [1,2,3,4]
37+
* 0 0 [0,1,2,3,4]
38+
*
39+
* Example 3:
40+
* Input: nums = [1], index = [0]
41+
* Output: [1]
42+
*
43+
* Constraints:
44+
* 1 <= nums.length, index.length <= 100
45+
* nums.length == index.length
46+
* 0 <= nums[i] <= 100
47+
* 0 <= index[i] <= i
48+
* */
49+
public class _1389 {
50+
public static class Solution1 {
51+
public int[] createTargetArray(int[] nums, int[] index) {
52+
List<Integer> list = new ArrayList<>();
53+
for (int i = 0; i < nums.length; i++) {
54+
list.add(index[i], nums[i]);
55+
}
56+
int[] target = new int[list.size()];
57+
for (int i = 0; i < target.length; i++) {
58+
target[i] = list.get(i);
59+
}
60+
return target;
61+
}
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1389;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1389Test {
10+
private static _1389.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1389.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{0, 4, 1, 3, 2}, solution1.createTargetArray(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 2, 1}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{1, 2, 3, 4, 0}, solution1.createTargetArray(new int[]{0, 1, 2, 3, 0}, new int[]{0, 1, 2, 3, 4}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertArrayEquals(new int[]{1}, solution1.createTargetArray(new int[]{0}, new int[]{1}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy