-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBeautifulArrangement.java
143 lines (126 loc) · 3.35 KB
/
BeautifulArrangement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*https://leetcode.com/problems/beautiful-arrangement/solution/*/
/*TLE Brute Force*/
class Solution
{
int count;
int[] primes = {419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541};
public int countArrangement(int n)
{
int[] nums = new int[n];
int i;
for (i = 1; i <= n; ++i)
nums[i-1] = i;
count = 0;
//recursion call
recurPerm(nums,0,n-1);
return count;
}
public void recurPerm(int[] nums, int l, int r)
{
//base case
if (l == r)
{
//add the permutation to the list
if (isBeautiful(nums)) ++count;
}
else
{
//for every integer in the range
for (int i = l; i <= r; ++i)
{
nums = swap(nums,l,i); //backtracking step-1 - swap
recurPerm(nums,l+1,r); //backtracking step-2 - recur
nums = swap(nums,l,i); //backtracking step-3 - backtrack
}
}
}
public int[] swap(int[] nums, int l, int i)
{
int temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
return nums;
}
public boolean isBeautiful(int[] nums)
{
for (int i = 0; i < nums.length; ++i)
if (nums[i]%(i+1) != 0 && (i+1)%nums[i] != 0) return false;
return true;
}
}
/*Small Condition checking*/
class Solution
{
int count;
int[] primes = {419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541};
public int countArrangement(int n)
{
int[] nums = new int[n];
int i;
for (i = 1; i <= n; ++i)
nums[i-1] = i;
count = 0;
//recursion call
recurPerm(nums,0,n-1);
return count;
}
public void recurPerm(int[] nums, int l, int r)
{
//base case
if (l == r)
{
//add the permutation to the list
if (isBeautiful(nums)) ++count;
}
else
{
//for every integer in the range
for (int i = l; i <= r; ++i)
{
nums = swap(nums,l,i); //backtracking step-1 - swap
if (nums[l]%(l+1) == 0 || (l+1)%nums[l] == 0)
recurPerm(nums,l+1,r); //backtracking step-2 - recur
nums = swap(nums,l,i); //backtracking step-3 - backtrack
}
}
}
public int[] swap(int[] nums, int l, int i)
{
int temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
return nums;
}
public boolean isBeautiful(int[] nums)
{
for (int i = 0; i < nums.length; ++i)
if (nums[i]%(i+1) != 0 && (i+1)%nums[i] != 0) return false;
return true;
}
}
/*DP and backtracking*/
class Solution
{
boolean[] visited;
int count;
public int countArrangement(int n)
{
count = 0;
visited = new boolean[n+1];
recur(n,1);
return count;
}
public void recur(int n, int pos)
{
if (pos > n) ++count;
for (int i = 1; i <= n; ++i)
{
if (!visited[i] & (pos%i == 0 || i%pos == 0))
{
visited[i] = true;
recur(n,pos+1);
visited[i] = false;
}
}
}
}