Maximize revenue algorithm [duplicate] - algorithm

This question already has answers here:
Algorithm design: can you provide a solution to the multiple knapsack problem?
(2 answers)
Optimal way of filling 2 knapsacks?
(3 answers)
Closed 6 years ago.
We have two boxes, size of 20 and 30. We want to pack fruits in the boxes in a way that gives us the biggest revenue.
Price Size
APPLES 5 19
ORANGES 11 11
BANANAS 2 5
KIWI 8 16
MANGO 22 23
CHERRY 2 9
Example looks very similar to rod cutting problem. First solve rod cutting problem for the first box (size of 20) and then with the remaining items solve rod cutting problem for the second box (size of 30). Or vice versa.
But then I came up with this example:
If we first pack first box (size of 20), the best combination leads to Oranges (11|11) and Bananas (2|5) which gives us price 13 and size 16. Rod cutting 'algorithm' will always choose best Price/Size combination.
Now lets continue with the second box (size of 30).
We pack Mango (22|23) and that's it. We have ran out of space for any other fruit.
Total revenue of two boxes in this example equals to 13 + 22 = 35. However, we could have had revenue of 37, with Cherry (2|9) in the first box and Bananas (2|5) in the second one.
If we first packed the second, bigger box, we would get correct result. But does that way (packing boxes starting with bigger ones) always works? I believe, despite starting with bigger boxes, there will always be the case where this algorithm will fail. What would be better approach to solve this problem?
EDIT:
In this example we have two boxes but it could be N boxes. I am looking for a solution that would solve this problem in general, N boxes.

Related

Algorithm: Unable to understand this Programming Challenge

I am unable to understand this question.What this question want us to find and what is given.Can anyone just explain it in a naive manner .
Question
The Sheriff of the Hackers' community had a very urgent job to do. He
needs to diffuse all the bombs placed by Joker. Joker is extremely
clever and cruel. He selected a region of 1000 X 1000 points, chose N
center points of the form (X,Y) and placed bombs on every integral
point that was on or inside the circular region of radius R around
that chosen point. Then he went to every such point and activated
those bombs that lied on or inside at least two such circular regions.
In order to make an efficient plan for the Bomb Disposal Squad, You
need to help the Sheriff to determine the number of active bombs.
INPUT:
The first line of the input contains the N. Next N lines contain 3
space separated integers X Y R as described above.
OUTPUT:
Print the number of active bombs.
CONSTRAINTS:
1 <= N <= 10
1 <= X,Y,R <= 1000
NOTE :
The search area for the Sheriff is a square with diagonal points (1,1)
and (1000,1000)
Sample Input
2
5 5 2
3 4 3
Sample Output
9
Explanation
All of the following 9 points are inside atleast 2 circles
3 5
4 4
4 5
4 6
5 3
5 4
5 5
5 6
6 4
I have no idea how to approach this problem.Where did the joker plant the bomb and how should I check for it whether a particular region has bomb planted.Sorry I might have misunderstood this also.
the best way to explain this is starting with squares....
imagine a grid as such:
a 1 2 3 4 5 6 7
b 1 2 3 4 5 6 7
c 1 2 3 4 5 6 7
d 1 2 3 4 5 6 7
e 1 2 3 4 5 6 7
f 1 2 3 4 5 6 7
Lets say the center point was (c,4) and the width was 3. Your square would be drawn on points (b,3) (b,4) (b,5)(c,3),(d,3) (d,4) (d,5) (c,5)
(c,4) would be in the middle.....
So... with a square you would have 9 (add up all the edge points I described and the center point which is contained in the square).
You are being asked to do the same thing..... with circles.... Good luck...
The simplest way to explain what this question wants is:
Given a list of circles, defined by a center (X, Y) and a radius (R), how many integral points (i.e. integers, (1,4) vs (1.1, 4.4)) of the 1000x1000 grid are within 2 or more of those circles.
That's what is being asked.
There are a lot of ways this could be solved, but that would be a different questions.
The most basic technique would be to iterate across all 1000x1000 points, calculate teh distance from each point to the center of each circle, and for those points who distance is less than R for a given circle, add 1. All points with a value more than 1 would be reported.
This is not necessarily the most efficient algorithm. For example, if there are 4 circles in the list, you would be calculating 4,000,000 distances.
But it will work.
There are a lot of ways to solve this. One way is to check each point in the matrix to see how many circles it lies in. Another way is to keep a list of points inside a circle, and increment a counter if the point already exists.

Unique methods to generate sudoku puzzle [duplicate]

This question already has answers here:
How to generate Sudoku boards with unique solutions
(15 answers)
Closed 8 years ago.
How many possible unique ways are there to generate a Sudoku Puzzle??
I can think of only two possible ways
1) Take a solved Sudoku puzzle and shuffle the rows and columns
2) Generate a random number and check if it violates any Sudoku constraints, repeat untill number does not violate any Sudoku constraint for every square(theoretically possible but normally it leads to deadlocking )
Are there any other ways?
Here is a 20-page PDF, titled "Sudoku Puzzles Generating: from Easy to Evil", that you'd probably find useful in your quest.
To answer your question:
Are there any other ways?
Yes. Yes there are.
Simple way to generate up to Take a Solved Sudoko puzzle,
step 1 ) replace all 1 with A , 2 with B till 9 with I,
Step 2) do a shuffle in each horizontal and vertical block block of using a random between 1 and 3, here in each there can only be 3 possible combinations.
step 3) now shuffle the block there can only be 3 vertical and 3 horizontal shuffle
step 4) rotate the block 1 to 4 time..
step 5) mirror the puzzle vertically and horizontally using a random between 1 and 2.
step 6) replace all A with any number 1 to 9..
guessing this will produce around 38,093,690,880 combos....

What algorithm can be used here [closed]

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 10 years ago.
Improve this question
This is an interview question, not a homework.
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
I must the count all possible numbers that the game administrator can shout.
For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.
Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.
To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.
This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.
In your example with lists :
{2 5 3}, {8 1 6}, {7 4 9}
say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them
For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.
For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.
For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.
For "9" : this is the other way round, it is always the greatest.
take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.

Minimum Distance

There is a set of 9 students and 3 schools Every school can be alloted at max 3 students .Every school and student has its coordinates .Now we have to allot student in such a way that the sum of distance from all the student to the school should be minimum.
I was asked this question in an interview.Can anyone suggest an algorithm for this?
Initially I tried greedy approach but that does not work.Then I tried applying a dynamic programming approach but could not come up with an optimal sub-structure.
Every school has 3 places, all 3 schools have 9 places. And you should find the best match between 9 places and 9 students.
This assignment problem may be solved with Hungarian algorithm.
How about exhaustive search since the problem size is small enough?
First school chooses 3 students out of 9 to start.
Second school chooses 3 students out of 6 remaining.
Last school gets stuck with the 3 students remaining.
So (9 choose 3) * (6 choose 3) = 1680

number of possible sudoku puzzles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Wiki http://en.wikipedia.org/wiki/Mathematics_of_Sudoku says Sudoku has 6,670,903,752,021,072,936,960 possible permutations.I tried to find out but it seems difficult.Can someone tell me how this number is calculated.
You can find all about it in this Wiki: http://en.wikipedia.org/wiki/Mathematics_of_Sudoku.
"the number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960 . This number is equal to 9! × 722 × 27 × 27,704,267,971, the last factor of which is prime. The result was derived through logic and brute force computation."
You can read the most recent rewrite of the original publication by Bertram Felgenhauer and Frazer Jarvis : Mathematics of Sudoku, it details the computation over 7 pages. The calculation actually isn't trivial (the idea being to enumerate distinct and valid Sudoku grids, rather than all possible arrangements of digits over a 9x9 grid).
Interestingly there was an estimation of the number of possible sudokus posted in an internet forum before the actual value was calculated and published by Felgenhauer & Jarvis.
The author of the post points out that there are some unproven assumptions in his guess. But the estimated value differs by 0.2% from the actual value published later.
In this Wiki you can find some estimation of other types of sudoku based on similar guesses.
Here is the full post from The New Sudoku Players' Forum:
by Guest » Fri Apr 22, 2005 1:27 pm
Lets try this from a whole different direction:
Step A:
Pretend that the only 'rule' was the 'block' rule, and that the row and column rules did not exist. Then each block could be arranged 9! ways, or 9!^9 ways to populate the puzzle (1.0911*10^50 'solutions').
Step B1:
If we then say 'let us add a rule about unique values in a row', then the top three blocks can be filled as follows:
Block 1: 9! ways
Block 2: 56 ways to select which values go in each 3-cell row, and 3! ways to arrange them (remember that we haven't invented a column rule yet).
Block 3: with 1 and 2 filled, the values that go in each row is now defined, but each row can be arranged 3! ways.
Therefore, we have 9! * 56 * 3!^6 ways to fill the top three blocks, and this value cubed to fill all nine blocks. (or 8.5227*10^35 solutions). Note that this represents a 'reduction ratio' (denoted as R) of 1.2802*10^14, by adding this one new rule.
Step B2: But we could have just as easily added a 'unique in columns' rule, and achieved the same results downward instead of across, with the same value of R.
Step C: (and here is where my solution is not rigorous) What if we assume that each of these rules would constrain the number of valid solutions by exactly the same ratio? Then there would be a combined reduction ratio of R^2. So the intitial value of 1.0911*10^50 solutions would reduce by a factor of R^2, or 1.639*10^28, leaving 6.6571*10^21 valid solutions.
This post and the account are attributed to Kevin Kinfoil (Felgenhauer & Jarvis).
Additional notes
Assume the Block 1 is
1 2 3
4 5 6
7 8 9
Then we have the following possibilities for Block2, if we ignore the order of the rows
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 1 2 3
this is 1 possibility
1 2 3 7 8 9
4 5 6 1 2 3
7 8 9 4 5 6
this is 1 possibility
1 2 3 two of 4,5,6, one of 7,8,9 3*3
4 5 6 the two remaining of 7,8,9, one of 1,2,3 3
7 8 9 the two remaining of 1,2,3, the remaining of (two of 4,5,6) 1
these are (3*3)*3*1=27 possibilities
1 2 3 two of 7,8,9, one of 4,5,6 3*3
4 5 6 two of 1,2,3, the remaining of 7,8,9 3
7 8 9 the two remaining of 4,5,6, the remaining of two of 1,2,3 1
these are (3*3)*3*1=27
So all in all these are 1+1+27+27=56 possibilities.

Resources