Reputation: 351
I am working on an exercise and I thought I was done until I realized that some of my results were wrong.
This is the function I called to get the result:
System.out.println(chargingSmartPhone(10, 50));
It is expected to return 7. I got 7 but the calculation is not right.
I am expected to follow this instruction:
S = 10, T = 50. In this condition R = 10 applies, and charge becomes 10 + 10 = 20.
Now charge rate changes to R = 5, then after 6 more minutes charge becomes 20+(5+5+5+5+5+5) = 50
Therefore total time: 7 minutes.
THis means I should have
10 + 10
20 <= 10
==========
20 + 5
25 <= 230
==========
25 + 5
30 <= 230
==========
30 + 5
35 <= 230
==========
35 + 5
40 <= 230
==========
40 + 5
45 <= 230
==========
45 + 5
50 <= 230
==========
7
But instead, I have this result:
10 + 10
20 <= 10
==========
20 + 5
25 <= 230
==========
25 + 8
33 <= 559
==========
33 + 2
35 <= 1009
==========
35 + 7
42 <= 5000
==========
42 + 8
50 <= 10000
==========
50 + 3
53 <= 1000000000
==========
7
I have to terminate the program when the intialCharge += r[i] is greater than final which is working fine.
Below is my code:
public static int chargingSmartPhone(int initialCharge, int finalCharge){
int count = 0;
int[] v = {10, 230, 559, 1009, 5000, 10000, 1000000000};
int[] r = {10, 5, 8, 2, 7, 8, 3};
for(int i = 0; i < v.length; i++){
if(initialCharge <= finalCharge) { // InitialCharge is S, FinalCharge is T
if (initialCharge <= v[i]) {
System.out.println(initialCharge + " + " + r[i]);
initialCharge += r[i];
System.out.println(initialCharge +" <= "+ v[i]);
System.out.println("==========");
count++;
}
}
}
return count;
}
public static void main(String[] args){
System.out.println(chargingSmartPhone(10, 50));
}
What am I doing wrong?
Upvotes: 0
Views: 241
Reputation: 2660
Here's a solution I came up with:
class Main {
public static void main(String[] args) {
System.out.println(chargingSmartPhone(10,50));
}
public static int chargingSmartPhone(int initialCharge, int finalCharge){
int start = initialCharge;
double count = 0;
int[] v = {10, 230, 559, 1009, 5000, 10000, 1000000000};
int[] r = {10, 5, 8, 2, 7, 8, 3};
for(int i = 0; i < v.length; i++){
while(start > v[i]){
i++;
}
if(finalCharge < v[i]){
count += Math.ceil((double)(finalCharge - initialCharge) / (r[i]));
break;
}
else{
count += Math.ceil((double)(v[i] - initialCharge) / (r[i]));
initialCharge += Math.ceil((double)(v[i] - initialCharge) / (r[i])) * r[i];
}
if(initialCharge == v[i]){
count += 1;
initialCharge += r[i];
if(initialCharge > finalCharge){
break;
}
}
}
return (int)count;
}
}
I think you may have a problem with the understanding of the problem. Here is what the problem is asking you to simulate with inputs 10 and 50:
"Since the initial charge is 10, the phone charges 10 units in 1 minute. Now, the charge is 20. Therefore, I will now charge 5 units in 1 minute. Since the charge (now 25) is still less than 230, I will now charge 5 units in 1 minute. "
If you keep repeating this process, it will eventually take 7 minutes to reach 60 units of charge.
The way I approached this problem was that first, I brought our iterator, i
, up to where our current starting charge is with the while loop.
Then, if the finalCharge was less than the current value of R, (meaning the phone can now charge up to finalCharge without changing the rate of charge), we just simply figure out how many minutes it takes to get from initial to final and round up. Remember to use a double here to cast so that we don't risk truncation!
However, if the finalCharge can not be reached without changing the rate of charge, we will bring initialCharge up to the current value of V with similar math.
But, one scenario that we need to account for is if our starting value is equal to the current value of V. If so, we just add one minute to get it to the next iteration. If this single iteration results in us reaching our target charge, we will break.
Remember that when we print out our answer, we want to convert it back to an integer, so cast your return value.
Let me know if you have any questions!
Upvotes: 1