-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatedDigits.java
More file actions
31 lines (29 loc) · 853 Bytes
/
rotatedDigits.java
File metadata and controls
31 lines (29 loc) · 853 Bytes
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
/*Dynamic Programming*/
public class rotatedDigits{
public static void main(String args[]){
int n=50;
int count=rotated_digits(n);
System.out.println(count);
}
public static int rotated_digits(int N) {
int[] dp = new int[N + 1];
int count = 0;
for(int i = 0; i <= N; i++){
if(i < 10){
if(i == 0 || i == 1 || i == 8) dp[i] = 1;
else if(i == 2 || i == 5 || i == 6 || i == 9){
dp[i] = 2;
count++;
}
} else {
int a = dp[i / 10], b = dp[i % 10];
if(a == 1 && b == 1) dp[i] = 1;
else if(a >= 1 && b >= 1){
dp[i] = 2;
count++;
}
}
}
return count;
}
}