Skip to content

Commit 5f60567

Browse files
add 1234
1 parent 797a2e9 commit 5f60567

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ _If you like this project, please leave me a star._ ★
1212
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
1313
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
1414
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | | | |Easy||
15+
|1234|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1234.java) | | | |Easy||
1516
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | | | |Easy||
1617
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | | | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
1718
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy||
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* 1243. Array Transformation
11+
*
12+
* Given an initial array arr, every day you produce a new array using the array of the previous day.
13+
*
14+
* On the i-th day, you do the following operations on the array of day i-1 to produce the array of day i:
15+
*
16+
* If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
17+
* If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
18+
* The first and last elements never change.
19+
* After some days, the array does not change. Return that final array.
20+
*
21+
* Example 1:
22+
* Input: arr = [6,2,3,4]
23+
* Output: [6,3,3,4]
24+
* Explanation:
25+
* On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].
26+
* No more operations can be done to this array.
27+
*
28+
* Example 2:
29+
* Input: arr = [1,6,3,4,3,5]
30+
* Output: [1,4,4,4,4,5]
31+
* Explanation:
32+
* On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5].
33+
* On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5].
34+
* No more operations can be done to this array.
35+
*
36+
* Constraints:
37+
* 1 <= arr.length <= 100
38+
* 1 <= arr[i] <= 100
39+
* */
40+
public class _1234 {
41+
public static class Solution1 {
42+
public List<Integer> transformArray(int[] arr) {
43+
int[] copy;
44+
do {
45+
copy = Arrays.copyOf(arr, arr.length);
46+
for (int i = 1; i < arr.length - 1; i++) {
47+
if (copy[i] < copy[i - 1] && copy[i] < copy[i + 1]) {
48+
arr[i]++;
49+
} else if (copy[i] > copy[i - 1] && copy[i] > copy[i + 1]) {
50+
arr[i]--;
51+
}
52+
}
53+
} while (!Arrays.equals(copy, arr));
54+
return Arrays.stream(arr)
55+
.boxed()
56+
.collect(Collectors.toList());
57+
}
58+
}
59+
}
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._1234;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static junit.framework.TestCase.assertTrue;
10+
11+
public class _1234Test {
12+
private static _1234.Solution1 solution1;
13+
private static int[] arr;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1234.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
arr = new int[]{6, 2, 3, 4};
23+
assertTrue(solution1.transformArray(arr).equals(Arrays.asList(6, 3, 3, 4)));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
arr = new int[]{1, 6, 3, 4, 3, 5};
29+
assertTrue(solution1.transformArray(arr).equals(Arrays.asList(1, 4, 4, 4, 4, 5)));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)