英文:
How to sum up step1 and step2 to reach the given distance, count the various sums of these steps and return it as an integer?
问题
public static int jump(int distance, int range1, int range2){
int res = 0, counter = 0;
range1 + range1 + range1 + range1
counter = range1 + jump(distance, range1, range2) == distance ?
if ( range1 + jump(distance, range1, range2) == distance ){
counter++;
} else if ( )
range1 + range1 + range2
range2 + range1 + range1
range2 + range2
return counter;
}
Example & instructions for this function:
Method call: jump(4, 1, 2);
Output: 5
What the function should do in the background:
1 + 1 + 1 + 1
1 + 1 + 2
1 + 2 + 1
2 + 1 + 1
2 + 2
So it eventually was able to sum up the steps1 and steps2 5 times differently and should return 5
then.
英文:
public static int jump(int distance, int range1, int range2){
int res = 0, counter = 0;
range1 + range1 + range1 + range1
counter = range1 + jump(distance, range1, range2) == distance ?
if ( range1 + jump(distance, range1, range2) == distance ){
counter++;
} else if ( )
range1 + range1 + range2
range2 + range1 + range1
range2 + range2
return counter;
}
Example & instructions for this function:
Method call: jump(4, 1, 2);
Output: 5
What the function should do in the background:
1 + 1 + 1 + 1
1 + 1 + 2
1 + 2 + 1
2 + 1 + 1
2 + 2
So it eventually was able to sum up the steps1 and steps2 5 times differently and should return 5
then.
答案1
得分: 2
我们可以进行递归调用,例如 jump(3, 1, 2)
。
jump(3, 1, 2)
/ \
jump(2, 1, 2) jump(1, 1, 2)
/ \ /
jump(1, 1, 2) jump(0, 1, 2) jump(0, 1, 2)
/
jump(0, 1, 2)
注意:我们能够精确到达 jump(0, 1, 2)
恰好3次,因此答案是3。
在每个函数调用中,我们需要处理以下三种情况之一:
情况1:当距离小于0时,意味着没有选择确切的步数,因此我们不计算这条路径并返回0。
情况2:当距离恰好为0时,意味着选择了确切的步数,我们返回1。
情况3:当距离大于或等于步长1或步长2时,我们进行递归调用,这是选择步长1和步长2的总和。
class Solution {
public static int jump(int distance, int a, int b) {
if(distance < 0) return 0; // 情况1
if(distance == 0) return 1; // 情况2
return jump(distance - a, a, b) + jump(distance - b, a, b); // 情况3
}
public static void main(String[] args) {
System.out.println(jump(3, 1, 2));
}
}
输出结果: 2
英文:
We can do recursive calls ex jump(3, 1, 2)
jump(3, 1, 2)
/ \
jump(2, 1, 2) jump(1, 1, 2)
/ \ /
jump(1, 1, 2) jump(0, 1, 2) jump(0, 1, 2)
/
jump(0, 1, 2)
Note: we are able to reach jump(0, 1, 2)
exactly 3 times, therefore, the answer is 3.
On each function call, we need to deal with any of one of these 3 cases:
case 1: When the distance is less than 0, which means exact steps were not chosen and so we don't count this path and return 0
case 2: When the distance is exactly 0, which means exact steps were chosen and we return 1
case 3: When the distance is greater than or equal to step1 or step2 so we make a recursive call, which is the sum of choosing step1 and step2
class Solution {
public static int jump(int distance, int a, int b) {
if(distance < 0) return 0; // case 1
if(distance == 0) return 1; // case 2
return jump(distance - a, a, b) + jump(distance - b, a, b); // case 3
}
public static void main(String[] args) {
System.out.println(jump(3, 1, 2));
}
}
output: 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论