Skip to content

Commit 2ea40e7

Browse files
refactor 667
1 parent 71af3d0 commit 2ea40e7

File tree

1 file changed

+75
-97
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+75
-97
lines changed

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

Lines changed: 75 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,86 @@
55
import java.util.List;
66
import java.util.Set;
77

8-
/**
9-
* 667. Beautiful Arrangement II
10-
*
11-
* Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n
12-
* and obeys the following requirement:
13-
* Suppose this list is [a1, a2, a3, ... , an],
14-
* then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.
15-
* If there are multiple answers, print any of them.
16-
17-
Example 1:
18-
19-
Input: n = 3, k = 1
20-
Output: [1, 2, 3]
21-
Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.
22-
23-
Example 2:
24-
25-
Input: n = 3, k = 2
26-
Output: [1, 3, 2]
27-
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.
28-
29-
Note:
30-
31-
The n and k are in the range 1 <= k < n <= 104.
32-
*/
33-
348
public class _667 {
359

36-
public static class Solutoin1 {
37-
/**This brute force solution will result in TLE as soon as n = 10 and k = 4.*/
38-
public int[] constructArray(int n, int k) {
39-
List<List<Integer>> allPermutaions = findAllPermutations(n);
40-
int[] result = new int[n];
41-
for (List<Integer> perm : allPermutaions) {
42-
if (isBeautifulArrangement(perm, k)) {
43-
convertListToArray(result, perm);
44-
break;
45-
}
46-
}
47-
return result;
48-
}
10+
public static class Solutoin1 {
11+
/**
12+
* This brute force solution will result in TLE as soon as n = 10 and k = 4.
13+
*/
14+
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;
21+
}
22+
}
23+
return result;
24+
}
4925

50-
private void convertListToArray(int[] result, List<Integer> perm) {
51-
for (int i = 0; i < perm.size(); i++) {
52-
result[i] = perm.get(i);
53-
}
54-
}
26+
private void convertListToArray(int[] result, List<Integer> perm) {
27+
for (int i = 0; i < perm.size(); i++) {
28+
result[i] = perm.get(i);
29+
}
30+
}
5531

56-
private boolean isBeautifulArrangement(List<Integer> perm, int k) {
57-
Set<Integer> diff = new HashSet<>();
58-
for (int i = 0; i < perm.size() - 1; i++) {
59-
diff.add(Math.abs(perm.get(i) - perm.get(i + 1)));
60-
}
61-
return diff.size() == k;
62-
}
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)));
36+
}
37+
return diff.size() == k;
38+
}
6339

64-
private List<List<Integer>> findAllPermutations(int n) {
65-
List<List<Integer>> result = new ArrayList<>();
66-
backtracking(new ArrayList<>(), result, n);
67-
return result;
68-
}
40+
private List<List<Integer>> findAllPermutations(int n) {
41+
List<List<Integer>> result = new ArrayList<>();
42+
backtracking(new ArrayList<>(), result, n);
43+
return result;
44+
}
6945

70-
private void backtracking(List<Integer> list, List<List<Integer>> result, int n) {
71-
if (list.size() == n) {
72-
result.add(new ArrayList<>(list));
73-
return;
74-
}
75-
for (int i = 1; i <= n; i++) {
76-
if (list.contains(i)) {
77-
continue;
78-
}
79-
list.add(i);
80-
backtracking(list, result, n);
81-
list.remove(list.size() - 1);
82-
}
83-
}
84-
}
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+
}
60+
}
8561

86-
public static class Solutoin2 {
87-
/**This is a very smart solution:
88-
* First, we can see that the max value k could reach is n-1 which
89-
* comes from a sequence like this:
90-
* when n = 8, k = 5, one possible sequence is:
91-
* 1, 8, 2, 7, 3, 4, 5, 6
92-
* absolute diffs are:
93-
* 7, 6, 5, 4, 1, 1, 1
94-
* so, there are total 5 distinct integers.
95-
*
96-
* So, we can just form such a sequence by putting the first part first and
97-
* decrement k along the way, when k becomes 1, we just put the rest numbers in order.*/
98-
public int[] constructArray(int n, int k) {
99-
int[] result = new int[n];
100-
int left = 1;
101-
int right = n;
102-
for (int i = 0; i < n && left <= right; i++) {
103-
if (k > 1) {
104-
result[i] = k-- % 2 != 0 ? left++ : right--;
105-
} else {
106-
result[i] = k % 2 != 0 ? left++ : right--;
107-
}
108-
}
109-
return result;
110-
}
111-
}
62+
public static class Solutoin2 {
63+
/**
64+
* This is a very smart solution:
65+
* First, we can see that the max value k could reach is n-1 which
66+
* comes from a sequence like this:
67+
* when n = 8, k = 5, one possible sequence is:
68+
* 1, 8, 2, 7, 3, 4, 5, 6
69+
* absolute diffs are:
70+
* 7, 6, 5, 4, 1, 1, 1
71+
* so, there are total 5 distinct integers.
72+
* <p>
73+
* So, we can just form such a sequence by putting the first part first and
74+
* decrement k along the way, when k becomes 1, we just put the rest numbers in order.
75+
*/
76+
public int[] constructArray(int n, int k) {
77+
int[] result = new int[n];
78+
int left = 1;
79+
int right = n;
80+
for (int i = 0; i < n && left <= right; i++) {
81+
if (k > 1) {
82+
result[i] = k-- % 2 != 0 ? left++ : right--;
83+
} else {
84+
result[i] = k % 2 != 0 ? left++ : right--;
85+
}
86+
}
87+
return result;
88+
}
89+
}
11290
}

0 commit comments

Comments
 (0)