Minimum numbers of attacks needed [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We are given 2 Dimensional grid of cells.Each cell may or may not contain a monster.
We are given a list of cells that contain monsters.
In a single attack we can kill all the monsters standing in a row or in a column. We
need to tell the minimum number of attacks that will be require to destroy all the monsters.
Constraints:
1 ≤ N ≤ 1000
1 ≤ X, Y ≤ 10^9
Example:
Input:
3
0 0
1 0
0 1
Output:
2
How to approach this problem..??

This can be modelled as a graph problem.
Create a graph node for each row and column where there's a monster.
Connect the nodes if a monster is on that row and that column.
This is a bipartite graph, and you want to do minimum vertex cover. König's theorem shows that for bipartite graphs the problem is equivalnt with the maximum matching problem which can be solved in polinomial time:
http://en.wikipedia.org/wiki/Maximum_matching#Maximum_matchings_in_bipartite_graphs

This reminds me the Set Cover Problem:
You have a set U that stores the N monsters: U = {1, 2, ..., N}, a set S of possible attacks. Each attack is as well a set of the indexes of the monster that the attack kills. In your example, S is S = { {1}, {2}, {}, {1}, {2} }. You have to find the smallest set C in S whose union is U.

Related

Using FFT to find all possible sum [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given array A[] and B[], to find how frequently each possible sums A[i] + B[j] appears we can use FFT
For example
A = [1,2,3], B = [2,4].
The sum 3 can be obtained in 1 way, sum 4 : 1 way, sum 5 : 2 ways, sum 6 : 1 way, sum 7 : 1 way
The way we can do this is to construct two polynomials, P and Q with their power corresponding to the element of the array. And apply the regular FFT.
So is there an efficient way of backtracking the numbers that forms the above. To elaborate, we know 3 can be formed in 1 way. But how do we know which two numbers form it?
One way to do it would be the classic two sum algorithm, i.e given an array and a target sum. Find the pairs that create the sum. Which is O(n)
Given we can have N different targets, the resulting algorithm is O(n^2). But I want to keep it under O(nlogn).

How to create faster Pythagorean triples? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
As we know, with the previous three positive integers (a < b < c), if it is three sides of the triangles are called Pythagorean Triple.
Given integers a. How to find the remaining two of the three Pythagorean quickly.
However, if there are multiple answers, the output b, c largest.
example:
input
9
output
40 41
(9,12,15), (9, 40, 41) are 2 answers. but is the most accurate (9, 40, 41).
I do not know how to solve optimization. Please help me.
Thank you very much.
As
a² = c²-b²=(c-b)*(c+b)
you can try the factors of a² from smallest to largest, as b, c are largest if b+c is largest and thus if c-b is smallest. For odd a, one easily sees that
c = (a²+1)/2
b = (a²-1)/2
give integers with the required property. For even a, the smallest workable factor where the sum of the two factors is even is 2, resulting in
c = (a²+4)/4
b = (a²-4)/4
This simple formula even has an old name: The Platonic sequence

Possible Array combinations based on constraints [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.

Perfect Balancing, linear time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I rebuild a binary searching tree into perfect balanced in linear time?
I think i'll do rotations to find median, but I'm not sure if its good way.
Thanks for any ideas.
At the very least, you can do it in two steps:
Extract a sorted array from the tree using in-order traversal.
Construct a near-perfect binary tree. For example, by just capping the height with h=log2n where n is the number of elements. You will get only a part of the perfect tree if n is not equal to 2k-1 for some integer k, but the height will still be minimal possible.
Here is an explanatory image for constructing the tree of values 1, 2, 3, ... 10:
8
4 10
2 6 9
1 3 5 7
Alternatively, on step 2, you can put the middle element of the array as root, divide what remains into two equally sized parts, and proceed recursively. An example:
5
2 8
1 3 7 10
4 6 9
Each of the steps can be performed in linear time.

Algorithmic classification [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to classify the following problem:
I have N empty boxes (ni is the volume of the i-th box, 1 <= i <= N) and M divisible items (mj is the volume of j-th item j, 1 <= j <= M). The total volume of all boxes is exactly equal to the total volume of all items. I need to find a distribution of items among boxes which minimizes the number of item divisions.
I suppose this problem is NP-complete, and is some kind of set coverage problem, but I can't find appropriate variation of it.
The special case N=2 and n_1 = n_2 is exactly the Subset Sum problem
http://en.wikipedia.org/wiki/Subset_sum_problem
since the optimum value of the problem formulated above is 0 if and only if
the instance (viewed as an instance of Subset Sum) has a solution. Hence, the presented problem is indeed NP-hard.

Resources