Skip to content

Commit 9461a37

Browse files
refactor 667
1 parent a1cf007 commit 9461a37

File tree

2 files changed

+61
-73
lines changed

2 files changed

+61
-73
lines changed

src/main/java/com/fishercoder/solutions/_667.java

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,34 @@ public class _667 {
99

1010
public static class Solutoin1 {
1111
/**
12-
* This brute force solution will result in TLE as soon as n = 10 and k = 4.
12+
* inspired by this post: https://leetcode.com/problems/beautiful-arrangement-ii/discuss/1154683/Short-and-Simple-Solution-or-Multiple-Approaches-Explained-with-Examples-! and implemented it on my own
1313
*/
1414
public int[] constructArray(int n, int k) {
15-
List<List<Integer>> allPermutaions = findAllPermutations(n);
16-
int[] result = new int[n];
17-
for (List<Integer> perm : allPermutaions) {
18-
if (isBeautifulArrangement(perm, k)) {
19-
convertListToArray(result, perm);
20-
break;
15+
List<Integer> list = new ArrayList<>();
16+
int maxSoFar = 1;
17+
list.add(1);
18+
boolean plus = true;
19+
while (k > 0) {
20+
if (plus) {
21+
plus = false;
22+
int num = list.get(list.size() - 1) + k;
23+
maxSoFar = Math.max(maxSoFar, num);
24+
list.add(num);
25+
} else {
26+
plus = true;
27+
list.add(list.get(list.size() - 1) - k);
2128
}
29+
k--;
2230
}
23-
return result;
24-
}
25-
26-
private void convertListToArray(int[] result, List<Integer> perm) {
27-
for (int i = 0; i < perm.size(); i++) {
28-
result[i] = perm.get(i);
31+
for (int start = maxSoFar + 1; start <= n; start++) {
32+
list.add(start);
2933
}
30-
}
31-
32-
private boolean isBeautifulArrangement(List<Integer> perm, int k) {
33-
Set<Integer> diff = new HashSet<>();
34-
for (int i = 0; i < perm.size() - 1; i++) {
35-
diff.add(Math.abs(perm.get(i) - perm.get(i + 1)));
34+
int[] result = new int[n];
35+
for (int i = 0; i < list.size(); i++) {
36+
result[i] = list.get(i);
3637
}
37-
return diff.size() == k;
38-
}
39-
40-
private List<List<Integer>> findAllPermutations(int n) {
41-
List<List<Integer>> result = new ArrayList<>();
42-
backtracking(new ArrayList<>(), result, n);
4338
return result;
4439
}
45-
46-
private void backtracking(List<Integer> list, List<List<Integer>> result, int n) {
47-
if (list.size() == n) {
48-
result.add(new ArrayList<>(list));
49-
return;
50-
}
51-
for (int i = 1; i <= n; i++) {
52-
if (list.contains(i)) {
53-
continue;
54-
}
55-
list.add(i);
56-
backtracking(list, result, n);
57-
list.remove(list.size() - 1);
58-
}
59-
}
6040
}
6141

6242
public static class Solutoin2 {
Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions._667;
45
import org.junit.BeforeClass;
5-
import org.junit.Ignore;
66
import org.junit.Test;
77

88
import static org.junit.Assert.assertArrayEquals;
99

1010
public class _667Test {
11-
private static _667.Solutoin1 solution1;
12-
private static _667.Solutoin2 solution2;
13-
private static int[] expected;
14-
15-
@BeforeClass
16-
public static void setup() {
17-
solution1 = new _667.Solutoin1();
18-
solution2 = new _667.Solutoin2();
19-
}
20-
21-
@Test
22-
public void test1() {
23-
expected = new int[] { 1, 2, 3 };
24-
assertArrayEquals(expected, solution1.constructArray(3, 1));
25-
assertArrayEquals(expected, solution2.constructArray(3, 1));
26-
}
27-
28-
@Test
29-
@Ignore//this problem requires you to return any one of the legit answer, so the results vary from time to time, so comment out this test
30-
public void test2() {
31-
expected = new int[] { 1, 3, 2 };
32-
assertArrayEquals(expected, solution1.constructArray(3, 2));
33-
assertArrayEquals(expected, solution2.constructArray(3, 2));
34-
}
35-
36-
@Test
37-
@Ignore//this problem requires you to return any one of the legit answer, so the results vary from time to time, so comment out this test
38-
public void test3() {
39-
expected = new int[] { 1, 5, 2, 4, 3, 6, 7, 8, 9, 10 };
40-
// assertArrayEquals(expected, solution1.constructArray(10, 4));//this is not working, so comment out
41-
assertArrayEquals(expected, solution2.constructArray(10, 4));
42-
}
11+
private static _667.Solutoin1 solution1;
12+
private static _667.Solutoin2 solution2;
13+
private static int[] expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _667.Solutoin1();
18+
solution2 = new _667.Solutoin2();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
expected = new int[]{1, 2, 3};
24+
assertArrayEquals(expected, solution1.constructArray(3, 1));
25+
assertArrayEquals(expected, solution2.constructArray(3, 1));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
CommonUtils.printArray(solution1.constructArray(3, 2));
31+
CommonUtils.printArray(solution2.constructArray(3, 2));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
CommonUtils.printArray(solution1.constructArray(10, 4));
37+
CommonUtils.printArray(solution2.constructArray(10, 4));
38+
}
39+
40+
@Test
41+
public void test4() {
42+
CommonUtils.printArray(solution1.constructArray(5, 3));
43+
CommonUtils.printArray(solution2.constructArray(5, 3));
44+
}
45+
46+
@Test
47+
public void test5() {
48+
CommonUtils.printArray(solution1.constructArray(5, 2));
49+
CommonUtils.printArray(solution2.constructArray(5, 2));
50+
}
4351

4452
}

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