Skip to content

final #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AP1403 - Algorithms/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 65 additions & 4 deletions AP1403 - Algorithms/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public class Exercises {
*/
public int[] productIndices(int[] values, int target) {
// todo
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;
}

Expand All @@ -26,7 +33,27 @@ public int[] productIndices(int[] values, int target) {
*/
public int[] spiralTraversal(int[][] values, int rows, int cols) {
// todo
return null;
int [] res = new int [rows*cols];
int top = 0, bottom = rows-1, left = 0, right = cols-1, index = 0;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) {
res[index++] = values[top][i];
}
top++;
for (int i = top; i <= bottom; i++) {
res[index++] = values[i][right];
}
right--;
for (int i = right; i >= left; i--) {
res[index++] = values[bottom][i];
}
bottom--;
for (int i = bottom; i >= top; i--) {
res[index++] = values[i][left];
}
left++;
}
return res;
}

/*
Expand Down Expand Up @@ -54,11 +81,45 @@ public int[] spiralTraversal(int[][] values, int rows, int cols) {
if you're familiar with lists and arraylists, you can also edit method's body to use them instead of array
*/
public int[][] intPartitions(int n) {
// todo
return null;
int count = countPartitions(n, n);
int[][] result = new int[count][n];
int[] buffer = new int[n];
fillPartitions(n, n, buffer, 0, result, new int[]{0});
return trimArray(result, count, n);
}

private int countPartitions(int n, int max) {
if (n == 0) return 1;
if (n < 0 || max == 0) return 0;
return countPartitions(n - max, max) + countPartitions(n, max - 1);
}

private void fillPartitions(int n, int max, int[] buffer, int index, int[][] result, int[] count) {
if (n == 0) {
System.arraycopy(buffer, 0, result[count[0]], 0, index);
count[0]++;
return;
}
for (int i = Math.min(n, max); i >= 1; i--) {
buffer[index] = i;
fillPartitions(n - i, i, buffer, index + 1, result, count);
}
}

private int[][] trimArray(int[][] result, int count, int n) {
int[][] finalResult = new int[count][];
for (int i = 0; i < count; i++) {
int length = 0;
while (length < n && result[i][length] != 0) {
length++;
}
finalResult[i] = new int[length];
System.arraycopy(result[i], 0, finalResult[i], 0, length);
}
return finalResult;
}

public static void main(String[] args) {
// you can test your code here

}
}