-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathExercises.java
More file actions
98 lines (80 loc) · 2.57 KB
/
Exercises.java
File metadata and controls
98 lines (80 loc) · 2.57 KB
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Exercises {
public int[] productIndices(int[] values, int target) {
for (int i = 0; i < values.length; i++) {
for (int j = i + 1; j < values.length; j++) {
if (values[i] * values[j] == target) {
return new int[]{i,j};
}
}
}
return null;
}
public int[] spiralTraversal(int[][] values, int rows, int cols) {
int[] result = new int[rows * cols];
int index = 0;
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++){
result[index++] = values[top][i];
}
top++;
if (top <= bottom) {
for (int i = top; i <= bottom; i++) {
result[index++] = values[i][right];
}
right--;
}
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result[index++] = values[bottom][i];
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--){
result[index++] = values[i][left];
}
left++;
}
}
return result;
}
public static int[][] intPartitions(int n) {
List<int[]> parts = new ArrayList<>();
int[] partition = new int[n];
int index = 0;
partition[index] = n;
while (true) {
int[] validParts = Arrays.copyOf(partition, index + 1);
parts.add(validParts);
int remaining = 0;
while (index >= 0 && partition[index] == 1) {
remaining += partition[index];
index--;
}
if (index < 0) {
break;
}
partition[index]--;
remaining++;
while (remaining > partition[index]) {
partition[index + 1] = partition[index];
remaining -= partition[index];
index++;
}
partition[index + 1] = remaining;
index++;
}
int[][] result = new int[parts.size()][];
for (int i = 0; i < parts.size(); i++) {
result[i] = parts.get(i);
}
return result;
}
public static void main(String[] args) {
// you can test your code here
}
}