-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMax_Nos_of_A's.java
More file actions
59 lines (44 loc) · 1.47 KB
/
Copy pathMax_Nos_of_A's.java
File metadata and controls
59 lines (44 loc) · 1.47 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
public class MaximumAs {
/**
* Author: Gaurav Shrivastava
*/
public static int MaxA(int N){
// The optimal string length is N when N is smaller than 7
if (N <= 6)
return N;
// An array to store result of subproblems
int screen[] = new int [N];
int b; // To pick a breakpoint
// Initializing the optimal lengths array for uptil 6 input
// strokes.
int n;
for (n=1; n<=6; n++)
screen[n-1] = n;
// Solve all subproblems in bottom manner
for (n=7; n<=N; n++)
{
// Initialize length of optimal string for n keystrokes
screen[n-1] = 0;
// For any keystroke n, we need to loop from n-3 keystrokes
// back to 1 keystroke to find a breakpoint 'b' after which we
// will have ctrl-a, ctrl-c and then only ctrl-v all the way.
for (b=n-3; b>=1; b--)
{
// if the breakpoint is at b'th keystroke then
// the optimal string would have length
// (n-b-1)*screen[b-1];
int curr = (n-b-1)*screen[b-1];
if (curr > screen[n-1])
screen[n-1] = curr;
}
}
return screen[N-1];
}
public static void main(String[] args) {
int N;
// for the rest of the array we will rely on the previous
// entries to compute new ones
for (N=1; N<=20; N++)
System.out.println("Maximum Number of A's with "+N+" keystrokes is "+ MaxA(N));
}
}