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.
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 2 days ago.
Improve this question
You are given n balls and n cups. Each cup holds a particular weight, and once a ball is placed in it tells you whether the ball is too heavy or light or just right. You can’t compare the weight of the balls directly. A perfect pairing between balls and cups exist. Design an expected nlogn algorithm to find the pairing. Hint: modify quicksort.
I’ve thought about this problem for a long time with no leads.
Is there a efficient way to compare the weight of two balls, or am I thinking about this wrong? Can someone please give a hint?
If you compare all balls with a single randomly picked cup, you will find the matching ball, and the other balls will be partitioned into those higher and those lower. You can use the matching ball to also partition the cups in a similar way. Then you have essentially randomized quicksort.
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assume that we know in advance that each record in an unsorted array is at distance at most d << n from its position in the sorted array. We would like to take advantage of this property. Assume that all n keys are distinct. For example: Let the list be 3 8 18 2 7 20 24 15 22 30 40. It is not hard to see that for this unsorted list each record is at distance at most 3 from its position in the sorted array.
Design a sort that has O(n lg d) running time.
It is assignment question. Some hints will be useful.
Here's my tip for doing it (I'd post a full solution, but as you say, this is from an assignment):
You already know that an element is within 2d of the correct index. How might you be able to scan through the array, but only looking through at most 2d elements at once?
More specifically, suppose you just figured out the ith element by checking everything from index i - d to i + d. How might you use what you already know to figure out the i+1th element in O(log d) time?
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am trying to find a way to solve an Optimization problem as follows:
I have 22 different objects that can be selected more than once. I have a evaluation function f that takes the multiplicities and calculates the total value.
f is a product over fractions of linear (affine) terms and as such, differentiable and even smooth in the allowed region.
I want to optimize f with respect to the 22 variables, with the additional conditions that certain sums may not exceed certain values (for example, if a,...,v are my variables, a + e + i + m + q + s <= 9). By this, all of the variables are bounded.
If f were strictly monotonuous, this could be solved optimally by a (minimalistically modified) knapsack solution. However, the function isnt convex. That means it is even impossible to assume that if taking an object A is better than B on an empty knapsack, that this choice holds even when adding a third object C (as C could modify B's benefit to be better than A). This means that a greedy algorithm cannot be used;
Are there similar algorithms that solve such a problem in a optimal (or at least, nearly optimal) way?
EDIT: As requested, an example of what the problem is (I chose 5 variables a,b,c,d,e for simplicity)
for example,
f(a,b,c,d,e) = e*(a*0.45+b*1.2-1)/(c+d)
(Every variable only appears once, if this helps at all)
Also, for example, a+b+c=4, d+e=3
The problem is to optimize that with respect to a,b,c,d,e as integers. There is a bunch of optimization algorithms that hold for convex functions, but very few for non-convex...