I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n0,n 0)
to(0n, 0n)
. If you model your code thatthe other way around, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EEN|ENN" "EEN|NEN" "ENE|NEN" "ENE|ENN"
Note that EEN
will bring you to the same spot as ENE
. You can see that each of those share the same second parts as well. This is duplication which we can use to speed up the algorithm.
If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EEN|ENN" "EEN|NEN" "ENE|NEN" "ENE|ENN"
Note that EEN
will bring you to the same spot as ENE
. You can see that each of those share the same second parts as well. This is duplication which we can use to speed up the algorithm.
If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(0, 0)
to(n,n)
. If you model your code the other way around, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EEN|ENN" "EEN|NEN" "ENE|NEN" "ENE|ENN"
Note that EEN
will bring you to the same spot as ENE
. You can see that each of those share the same second parts as well. This is duplication which we can use to speed up the algorithm.
If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EENENN""EEN|ENN" "ENEENN""EEN|NEN" "ENENEN""ENE|NEN" "EENNEN""ENE|ENN"
Note that EEN
will bring you to the same spot as ENE
. You can in fact see that each of those share the same second parts as well. This is duplication which we can use to speed up the algorithm.
If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EENENN" "ENEENN" "ENENEN" "EENNEN"
You can in fact see duplication. If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EEN|ENN" "EEN|NEN" "ENE|NEN" "ENE|ENN"
Note that EEN
will bring you to the same spot as ENE
. You can see that each of those share the same second parts as well. This is duplication which we can use to speed up the algorithm.
If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a gut feeling there is a direct way to compute this amount, but I am not really into all the mathematics, but you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EENENN" "ENEENN" "ENENEN" "EENNEN"
You can in fact see duplication. If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a gut feeling there is a direct way to compute this amount, but I am not really into all the mathematics, but you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EENENN" "ENEENN" "ENENEN" "EENNEN"
You can in fact see duplication. If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.
I have a feeling there is a direct way to compute this amount, but I am not really into all the mathematics, you could possibly look it up. I'll talk about your code as is:
You should really remove all the superfluous white space, it hampers readability.
In Java, opening braces are not on a new line.
private
beforestatic
.Do not save te result of your recursive function in a variable, you can return at once.
The problem states you move from
(n,n)
to(0, 0)
. If you model your code that way, you can simplify it quite a bit. No need to passn
to your recursive function for example, as you can simply compare with 0.Try to always use braces, even if you only have one line inside the block, as for example with the
if
part in your recursive function. It helps prevent unnoticed bugs due to formatting, and helps ease up modifying it later on.In the else block, you do not need to check if
x
andy
are valid, if you check for 0 up front.In the recursive function, if the initial
if
succeeds, the result will always be 1. So you can return at once, it makes it more readable. Return early is a good thing in my opinion. The variable can then be put inside theelse
, reducing the scope.You mention memoization, yet you do not seem to be using it. If you take the example, and the paths taken there:
"EEENNN" "EENENN" "ENEENN" "ENENEN" "EENNEN"
You can in fact see duplication. If you think about it, there are generally multiple ways to get to some point (x,y)
. If you think of this as the starting point, the possible paths to the destination will obviously be the same, no matter how you get there. So what you can do is create a lookup table for a calculated location, so you calculate each one only once. This will give you an extreme amount of speedup I would think, on problems with a big amount of possible paths. Which you will get to quickly, even for small values of n
.