Given a matrix A of M rows and N columns of integers, find the length of longest common subsequence among rows of matrix.
Constraints:
1 < M, N < 10^5
1 < A[I][J] < 10^7
ex:
A = [[4,5,1,2,3], [1,2,3,6,7]]
subsequences among rows
subsequences are:
[],
[1,2],
[1,2,3]
longest and common among both are [1,2,3] should return **3 as length**.
how to solve it?
Related
Is there a bit trick to check whether a number can be expressed as sum of x powers of 2?
Example: For x=3 n=21, the numbers are 16, 4, and 1. If n=30, it should be false, because there are no 3 powers of two to represent 30.
For a number n …
… the minimum x is the number of 1-bits in n. This number is called popcount(n).
Example: The binary number 0b1011 needs at least popcount(0b1011)=3 powers of two to be summed up (0b1000+0b0010+0b0001).
… the maximum x is n. Because 1 is a power of two you can add 1 n times to get n.
Now comes the hard question. What if x is between popcount(n) and n?
As it turns out, all of these x are possible. To build a sum of x powers of two …
start at the shortest sum (the binary representation of n)
If you have less than x addends, split any addend that is bigger than 1 into two addends, increasing the number of addends by one. This can be done until you arrive at x=n.
Example: Can 11=0b1011 be expressed as a sum of x=7 powers of two?
Yes, because popcount(n)=3 <= x=7 <= n=11.
To build a sum with x=7 powers of two we use
11 = 0b1011 = 0b1000+0b10+0b1 | only 3 addends, so split one
= (0b100+0b100)+0b10+0b1 | only 4 addends, so split another one
= ((0b10+0b10)+0b100)+0b10+0b1 | only 5 addends, so split another one
= (((0b1+0b1)+0b10)+0b100)+0b10+0b1 | only 6 addends, so split another one
= (((0b1+0b1)+(0b1+0b1))+0b100)+0b10+0b1 | 7 addends, done
Implementation
To implement the check »can n can be expressed as sum of x powers of two?« use
isSumOfXPowersOfTwo(n, x) {
return x<=n && x>=popcount(n).
}
For efficient bit-twiddling implementations of popcount see this question. Some processors even have an instruction for that.
I have an array
h <- array(0, c(100, 5, 9));
The length 9 vector is sort of an identification sequence. Given some i in 1:100, is there a way I can obtain the index j in 1:5 for which the length 9 sequence matches some vector V? ie. the index j such that
identical(h(i,j,1:9),V)
is true.
Edit: this is assuming that one of the 5 do match for the given i. It'd be even better if one can obtain the pair (i,j) for which it matches with V.
I am new to permutations and combinations. I am provided with the number n and I can make a number with the help of n+1 digits which are 0,1,...n. I need to find in how many ways I can make the sum n by putting these n+1 digits in n places.
Like i am having number n=2
then
(a) 0 and 2 (b)1 and 1 (c)2 and 0 .
for a number n=3
then
(a)0,0,3 (b)0,3,0 (c)3,0,0 (d)0,1,2 (e)0,2,1 (f)1,0,2 (g)1,2,0 (h)2,0,1 (i)2,1,0 (j)1,1,1
So in total i have 10 ways to generate sum=3 by using the digits 0,1,2,3.
And also consider i can put these n+1 digits in n places only.
The number of compositions of n into k nonnegative summands is (n+k-1) choose n by the stars-and-bars method. You have k=n, so the count is 2n-1 choose n. Your examples were 3C2=3 and 5C3=10.
Yes, the number of total ways for making the sum=n using n+1 digits is equal to (n+n-1)C(n-1) or you can say (n+n-1)C(n).
I have a full matrix of numbers. On a computer, I can easily set with zeroes a row or a column. I would like to know how I can represent this operation symbolically in a mathematical expression.
For a n x n matrix A and with
e = ones(n)
e[k] = 0
matrix multiplication
A*diag(e)
zeros the k-th column and
diag(e)*A
zeros the k-th row
I'm looking to check if a vector of data is in rank form. That is; each observations is a number between 1 and N, where N is the number of observations, but in a random, unknown order.
The simplest check I could think of was the gaussian sum, using (N * (N + 1)) / 2 and comparing that to the sum of the vector. Except in my case I have 200,000 observations and the sum of all numbers from 1 to 200,000 is greater than 2^32. Apart from getting a 64 bit computer what is the fastest method of checking that data is in rank form.
checkForRanks <- setdiff(1:N,X)
isRanked <- length(checkForRanks) > 0
if(!isRanked) stop("Please rank data")