Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package algorithms.curated170.medium.fourkeyskeyboard;

public class FourKeysKeyboardDP {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


public int maxA(int N) {
int[] best = new int[N + 1];

for (int press = 1; press <= N; press++) {
best[press] = best[press - 1] + 1;
if (press > 6) {
best[press] = Math.max(best[press], best[press - 4] * 3);
best[press] = Math.max(best[press], best[press - 5] * 4);
best[press] = Math.max(best[press], best[press - 6] * 5);
}
}
return best[N];
}

public static void main(String[] args) {
var solution = new FourKeysKeyboardDP();

for (int i = 0; i < 50; i++) {
System.out.println(solution.maxA(i));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package algorithms.curated170.medium.fourkeyskeyboard;

public class FourKeysKeyboardMath {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


final int[] first16Max = new int[] { 0, 1, 2, 3, 4, 5, 6, 9, 12, 16, 20, 27, 36, 48, 64, 81 };

public int maxA(int N) {
int quadrupleTimes = N > 15 ? (N - 11) / 5 : 0;
return getQuadrupledCount(N - 5 * quadrupleTimes, quadrupleTimes);
}

private int getQuadrupledCount(int index, int quadrupleTimes) {
return first16Max[index] << (2 * quadrupleTimes);
}

public static void main(String[] args) {
var solution = new FourKeysKeyboardMath();

for (int i = 0; i < 50; i++) {
System.out.println(solution.maxA(i));
}
}
}