I'm trying to understand this question on leetcode
Partition Equal subset problem
The solution section has recommended a naive approach to recurse, in one part it suggests this:
isSum (subSetSum, n) = isSum(subSetSum- nums[n], n-1) || isSum(subSetSum, n-1)
But in the sample code, the recursion logic is set as:
bool result = dfs(nums, n - 1, subSetSum - nums[n - 1]) || dfs(nums, n - 1, subSetSum);
Why is it that in the solution we're subtracting nums[n] and in the final solution we're subtracting nums[n-1]. And which one is the right solution ? I tried and both seem to be giving the right result, something is wrong here but I cannot see what.
Any suggestions ?
Related
I'm new to R and am struggling with the apply function. It is really slow to execute and I was trying to optimize some code I received.
I am trying to do some matrix operations (element-wise multiplication and division on ~10^6 element matrices) then sum the rows of the resulting matrix. I found the fantastic library Rfast and it executes what I thought was the same code in about 1/30 the time, but I am getting systematic differences between my 'optimized' answer and the previous answer.
The original code was something along the lines of
ans <- apply(object, 1, function(x) sum((x - a) / b))
and my code is
ans = Rfast:::rowsums((object-a)/b)
I'm not sure if it's because one of the methods is throwing away precision or making rounding errors - any thoughts?
Edit
Trying to reproduce the error is pretty hard...
I have been able to isolate the discrepancy to when I divide by my vector b with entries each ~ 3000 (i.e. [3016.460436, 3021.210321, 3033.3303219]. If I take this term out the two methods give the same answer.
I then tried two methods to improve my answer, one was dividing b by 1000 then dividing the sum by 1000 at the end. This didn't work, presumably because the float precision is the same either way.
I also tried forcing my b vector to be integers, which also didn't work.
Sample data doesn't reproduce my error either, which is frustrating...
objmat = rbind(rep(c(1,0,0),1000),rep(c(0,0,1),1000))
amat = rbind(rep(c(0.064384654, 0.025465132, 0.36543214),1000))
bmat = rbind(rep(c(1016.460431,1021.210431,1033.330431),1000))
ans = apply(objmat,1,function(x) sum((x-amat)/bmat))
gives
ans[1] = 0.5418828413
rowsums((objmat[1,]-amat)/bmat) = 0.5418828413
I think it has to be a floating point precision error, but I'm not sure why my dummy data doesn't reproduce it, or which method (apply or rowsums) would be more accurate!
Let us consider the following pseudocode:
int n=n;
int A[][]
scanf(A[][],%d);
for i=1:n;i++
{
x=A[i][i]
for j=1:n;j++
{
if x<A[i][j]
a=x;
x=A[i][j];
A[i][i]=x;
A[i][j]=a;
return A[][]
I am fumbling on this pseudo code.the question, I think is just that the diagonal entries are compared and exchanged for the greatest entries. But, will the output depend on the entries of the matrix or will be independent of it is my main question. Specifically, is there any general formula for the output? Is it dependent on the type of matrix A I think it should some power of A. Any hints? Thanks beforehand.
You could just write your code on any language you love.
n = 3
A = [[1,2,3], [3,5,6], [7,8,9]]
for i in range(n):
x=A[i][i]
for j in range(n):
a = None
if x < A[i][j]:
a = x
x=A[i][j]
A[i][i]=x
A[i][j]=a
print (A)
Gives you:
[[3, 1, 2], [None, 6, 3], [None, 7, None]]
But, will the output depend on the entries of the matrix or will be
independent of it is my main question.
Ofc it depends. Your can see the initial data in the output. That means output depends on data.
Specifically, is there any general formula for the output?
I believe NO, but I cant mathematically prove. Just look at Nones appear in output. I hardly imagine such formula.
Is it dependent on the type of matrix A I think it should some power
of A.
What is 'type of matrix' ?
I am doing this small task which I have to arrange asymptotic runtime in ascending order. Here are the runtimes:
Here is the order I believe they should go in:
log10(n^4), n^3, 2^((log4n)), 2^(100n), e^pi^4096, n! + 12^1000
Is this correct? Or are there any errors?
Thanks!
I don't believe that the ordering you've given here is correct. Here are a few things to think about:
Notice that 2log4 n = 2(log2 n / log2 4) = 2(log2 n) / 2. Can you simplify this expression?
How fast does the function eπ4096 grow as a function of n?
Hope this helps!
So I'm trying to learn R and using a number of resources including a book called "Discovering Statistics using R" and a bunch of other cool eBooks.
I understand a great method in programming is the Euclid's Algorithm.
Implementing it in a loop can be achieved like this:
gcd(x,y) //assuming x is the largest value
//do
r = x%y;
x = y;
y = r;
//while r != 0;
return x;
After several searches on Google, SO and Youtube refreshing my memory of gcd algorithms, I wasn't able to find one that doesn't use a loop. Even recursive methods seem to use loops.
How can this be achieved in R without the use of loops or if statements?
Thanks in advance.
Using the statement "without loops or the if statement" literally, here is a recursive version that uses ifelse:
gcd <- function(x,y) {
r <- x%%y;
return(ifelse(r, gcd(y, r), y))
}
One might not expect it, but this is actually vectorized:
gcd(c(1000, 10), c(15, 10))
[1] 5 10
A solution using if would not handle vectors of length greater than 1.
Reducing GCD for two integers enables you to compute GCD for any sequence of integers (sorted or not):
gcd2 <- function(a, b) {
if (b == 0) a else Recall(b, a %% b)
}
gcd <- function(...) Reduce(gcd2, c(...))
You can solve it recursively.
euclids <- function(x,y){
theMax = max(x,y)
theMin = min(x,y)
if (theMax == theMin) return (theMax)
else return (euclids(theMin, theMax-theMin))
}
It's easy to do with a couple modulo operations. Sadly, I left my personal gcd code on a different machine (in a galaxy far away) - but you can find the source in either the numbers or pracma packages.
BTW, here's a good way to find existing code: library(sos); ???gcd
I am a novice in R, and so what seemed to work fine in C and Python, surprisingly breaks down in R.
I am trying to calculate the product of the first 1000 Fibonacci numbers. Here is the full code:
#PRRODUCT OF FIBONACCI NUMBERS
Fibonacci<-rep(0, 1000)
Fibonacci[0]<-1
Fibonacci[1]<-1
Product<-1
for (i in 2:1000)
{
Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])
Product<-Fibonacci[i]*Product
}
Fibonacci[1000]
Product
This returns the following error:
Error in Fibonacci[i] <- (Fibonacci[X - 1]) + (Fibonacci[X - 2]) :
replacement has length zero
I am inclined to think I have misunderstood operating with different elements of an array (perhaps the i-2 in the vector description is not correct), but I haven't found anything over the past hour and a half which would have helped me correct it.
So, any insights into the cause of the problem would be most appreciated.
Thank you in advance.
Arrays in R are 1-based.
Fibonacci[1]<-1
Fibonacci[2]<-1
Product<-1
for (i in 3:1000)
{
(the remainder as in your question)
The problem is Fibonacci[0] which is a 0-length numeric. When i = 2, this expression has a right hand side of numeric(0):
Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])