简体   繁体   中英

The number of unique elements in a submatrix with elements in a given range?

I have thought of the following, but it requires a lot of memory.

Since I need to find the unique elements in any sub matrix and elements are within range 1-10, I create an array of size 10 for each matrix member, which stores the number of occurrences of each element up to that matrix member (from left) in that particular row.

eg

if row is 1, 2, 2, 4, 3, 5

then arr[3][10]={1,2,0,1,0,0,0,0,0,0} //for the 3rd element '4', '1' occurred once, '2' twice and '4' once.

and arr[5][10]={1,2,1,1,1,0,0,0,0,0} //for the last element '5'

Thus, I can find out the number of unique elements between any two members by just traversing each of the two arrays and see which of the array members are different.

I can push all the unique elements in each row of the sub matrix into a set and find the number of unique elements.

However, this method requires a lot of memory if the matrix size increases. Is there any better way to so this?

Sorry for the offtopic, but questions like this should result in ban .

You asked for a solution to problem from active programming contest :

http://www.codechef.com/DEC13/problems/RECTQUER

your approach is right. Go ahead and you will get AC. I implemented what you have written in your question and I got AC. use a 3-D matrix of size [300][300][10] and do exactly what you have suggested. Memory requirement will not be too high.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM