forked from IDeserve/learn
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodeDecode.java
More file actions
Latest commit
92 lines (64 loc) · 1.82 KB
/
Copy pathEncodeDecode.java
File metadata and controls
92 lines (64 loc) · 1.82 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
packagequestions.virendra;
publicclassEncodeDecode {
publicstaticintdecode(Stringmessage)
{
intmsgLen = message.length();
if(msgLen == 0 || msgLen == 1)
return1;
intcount = 0;
if(message.charAt(msgLen - 1 ) > '0')//last digit
count = decode(message.substring(0, msgLen - 1));//trimmed message by decreasing length by 1
if((message.charAt(msgLen - 2) < '2') || ( message.charAt(msgLen - 2) == '2' && message.charAt(msgLen - 1) < '7' ) )
count += decode(message.substring(0, msgLen - 2));
returncount;
}
publicstaticintdecodeDp(Stringmessage)
{
intmsgLen = message.length();
int[] decodeCount = newint[msgLen + 1];
decodeCount[0] =1;
decodeCount[1] =1;
for(inti=2; i< msgLen + 1; i++)
{
if(message.charAt(i - 1 ) > '0')
decodeCount[i] = decodeCount[i-1];
if((message.charAt(i - 2) < '2') || ( message.charAt(i - 2) == '2' && message.charAt(i - 1) < '7' ) )
decodeCount[i] = decodeCount[i] + decodeCount[i-2];
}
returndecodeCount[msgLen];
}
publicstaticintcountBinary(intN)
{
intC0 = 1;
intC1 = 1;
for(inti=1; i<N; i++)
{
inttemp = C1;
C1 = C0;
C0 = C0 + temp;
}
returnC0 + C1;
}
publicstaticintcutRod(intcost[])
{
intn = cost.length;
intdp[] = newint[cost.length +1];
dp[0] = 0;
inti, j;
for (i = 1; i<=n; i++)
{
intmax_val = Integer.MIN_VALUE;
for (j = 0; j < i; j++)
{
max_val = Math.max(max_val, cost[j] + dp[i-j-1]);
}
dp[i] = max_val;
}
returndp[n];
}
publicstaticvoidmain(Stringargs[])
{
introdcosts[] = {1, 5, 8, 9, 10, 17, 17, 20};
System.out.println(cutRod(rodcosts));
}
}