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
Let us assume there is highway
There are N potholes at points [p1, p2, p3...., pn]
There are equal number of service crew at points [s1, s2, s3...., sn]
One service crew can repair one pothole only
The cost to send a crew at point pX to repair a pothole at point sX is |pX-sX|
How would you find the minimum cost to repair the road?
Ex:
Potholes are at [3, 5, 7]
Service Crew are stationed at [1, 3, 5]
Few possible combinations are:
1->3, 3->5, 5->7 (Cost = 6)
1->5, 3->7, 5->3 (Cost = 10)
Share/Explain the algorithm you'd use to solve this problem?
What is the time & space complexity of your algorithm?
In this example it should be 6, no matter what!
This is the problem of finding maximum matching in a fully connected bipartite graph.
If the problem has same number of potholes and crews, it's a perfect matching.
Intention would be to find out maximum matching between potholes P and crews C.
Assumption: A pothole can be serviced by any crew.
Matching condition: |P(i) - C(j)| is minimum.
Related
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
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
I had an assignment on dynamic programming due last night, but I had to turn it in unfinished because i could not understand how to solve the last problem:
The state wants to monitor traffic on a highway n miles long. It costs ci to install a monitoring device on mile i of the highway. The maximum distance between monitoring devices should not be more than d miles. That is, if there is a monitoring device on mile i, then there must be one monitoring device from mile i + 1 to mile i + d (or it is the case that i + d > n). The state wants a plan that will minimize the cost. Assume there is an array C[1..n] of costs.
Let vk be the cost of the best solution assuming a k mile highway and assuming a monitoring device on mile k. Given C and d, if the values of v1 through vk-1 are known, show how to determine the value of vk. You can write this mathematically, or provide pseudocode in the style of the book. Note you need to take into account all possible values of k from k = 1 to k = n.
I'm sure a problem similar to this will appear on the exam coming up and I'd like to at least know where to begin solving this, so any assistance is appreciated.
Let's define DP[i] as the minimum cost of installing a monitor at station i and some other indexes which are less than i (such that each consecutive station is less than or equal to distance d)
Now the answer to our problem would be
min(DP[n - d + 1], ...DP[n - 2], DP[n - 1], DP[n])
That is the minimum cost of having the last monitor on last d indexes.
Now, the recurrence relation for the dynamic programming can be easily seen as :
DP[i] = min(DP[i - 1], DP[i - 2], ... DP[i - d]) + C[i]
If we want to install a monitor on ith index, we install it by cost C[i], and we must also ensure that we have a monitor in previous d indexes. So, we take the minimum of installing the second last monitor on it's previous d indexes.
If you code the recurrence by naive method it looks O(n * d), but by using the sliding window minimum algorithm using a doubly ended queue, you can reduce the time complexity to asymptotically O(n).
As this is an assignment problem, I won't write in detail. You should be able to follow up from this point.
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 7 years ago.
Improve this question
Say I'm a runner, ran a fixed distance of 42KM, no more, no less.
There are a couple of cities (or shops if you think distances between cities are normally bigger than 42KM) can be traversed in this range.
You can start from ANY vertex(city), how to schedule a route so that I can reach the most number of cities? The runner visit each city exactly once and returns to the origin city. (When the runner returned to the original point, the KMs he ran could be smaller than 42KM)
In real world, this graph should be a cyclic able, non-directed, weighted graph.
However, you are welcome to give any opinion including changing the constraints.
Edit:
I had a brute force solution as follows, still trying to find out a better solution.
(1) setup a collection of possible result set (E.g. 4 cities, a, b, c, d; then I get a combination of 2 cities[ab,ac,ad,bc,bd,cd], 3 cities[abc,abd,acd,bcd], 4 cities[abcd]);
(2) run TSP on each of this result, the result is a distance; if this distance is smaller than 42KM, this result is a candidate.
(3) from the candidate list, choose the one with most cities. This is a brute force solution, however.
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 8 years ago.
Improve this question
Here is an interview question, will somebody give me some hint? I am thinking about DFS or BFS, however, I cannot think out a clear solution from my head.
Three coke machines. Each one has two values min & max, which means if
you get coke from this machine it will load you a random volume in the
range [min, max]. Given a cup size n and minimum soda volume m, show
if it's possible to make it from these machines.
This is assuming you're not allowed to overflow the cup. If you can overflow it, you can always make it.
Let's mark the machines with (min1,max1),(min2,max2),(min3,max3). a1,a2 and a3 shows the amount of times you've used each machine.
We need to find a1, a2 and a3 in order to satisfy :
Condition 1 : a1*min1 + a2*min2 + a3*min3 >= m
Condition 2 : a1*max1 + a2*max2 + a3*max3 <= n
Apparently it's not required to find the most optimal way to fill the cup (minimizing a1+a2+a3) so you can simply use DFS.
You use Condition 2 as the depth limit (meaning if Condition 2 isn't fulfilled you stop going deeper in the graph) and if you ever reach Condition 1 you have yourself the answer (return yes). If you finish the search and find no answers, return no.
Seeing as this is an interview question though, I really doubt that DFS or BFS would be the way to solve it. This could easily time out with big m and n values.
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.