Container with most water algorithm issue - algorithm

Given n non-negative integers a1, a2, ..., an, where each represents
a point at coordinate (i, ai). n vertical lines are drawn such that
the two endpoints of line i is at (i, ai) and (i, 0). Find two lines,
which together with x-axis forms a container, such that the container
contains the most water
What I do not understand about this question is how am I supposed to know the y-coordinate value(height) of the n vertical lines.

If the values you are given are {1,2,3,4,4,1,3}, the vertical lines would be:
4| | |
3| | | | |
2| | | | | |
1| | | | | | | |
0| -------------------
1 2 3 4 5 6 7
ai is the height of the ith line, and it's x coordinate is i.

Related

A NFA accepting ;anguages whose final digit didn't appear before

Give a non-deterministic finite automata(NFA) which accepts the following language:
The set of strings over the alphabet {0,1,...,9} such that the final digit has not appeared before.
I have encountered this problem on introduction to automata theory languages and computation page 67 Exercise 2.3.4
When the alphabet is {0,1}, which is the simplest non-trivial situation, we can get a simplest NFA like this:
The simplest non-trivial situation
So further you can get an NFA like this:
enter image description here
An easy way to get an NFA for this is to consider each of the 10 cases separately. Consider the case of all strings in the language whose last digit is 0. Such strings contain no other 0s; a regular expression for them is (1+2+3+4+5+6+7+8+9)*0. An NFA for them looks sort of like
1,2,3,4,5,6,8,9
/--\
| /
V /
o----->A0--0-->[B0]
We can imagine 9 others with states Ax, Bx, where Bx is accepting for each x, and Ax loops to itself on every symbol besides x, and to Bx on symbol x.
We can then glue all of these together by introducing a new initial state S which has an empty/lambda transition to the Ax states. The final NFA would have:
one initial state S
empty/lambda/epsilon transitions from S to states A0, A1, A2, A3, A4, A5, A6, A7, A8 and A9
transitions from A0 to B0 on 0, A1 to B1 on 1, A2 to B2 on 2, and so on
all missing transitions from states A0, A1, A2, and so on, back to those same states
In fact, why not copy/paste the above and just write the whole thing down?
1,2,3,4,5,6,7,8,9
/--\
| /
V /
/----->A0--0-->[B0]
/
| 0,2,3,4,5,6,7,8,9
| /--\
| | /
| V /
| /----->A1--1-->[B1]
|/
| 0,1,3,4,5,6,7,8,9
| /--\
| | /
| V /
| /----->A2--2-->[B2]
|/
| 0,1,2,4,5,6,7,8,9
| /--\
| | /
| V /
| /----->A3--3-->[B3]
|/
| 0,1,2,3,5,6,7,8,9
| /--\
| | /
| V /
| /----->A4--4-->[B4]
|/
/ 0,1,2,3,4,6,7,8,9
S-< /--\
\ | /
|\ V /
| \----->A5--5-->[B5]
|
| 0,1,2,3,4,5,7,8,9
| /--\
| | /
|\ V /
| \----->A6--6-->[B6]
|
| 0,1,2,3,4,5,6,8,9
| /--\
| | /
|\ V /
| \----->A7--7-->[B7]
|
| 0,1,2,3,4,5,6,7,9
| /--\
| | /
|\ V /
| \----->A8--8-->[B8]
|
| 0,1,2,3,4,5,6,7,8
| /--\
| | /
\ V /
\----->A9--9-->[B9]
Is this the simplest NFA or DFA for this language? Almost surely not. But it should work.

Expand rectangles as much as possible to cover another rectangle, minimizing overlap

Given a tiled, x- and y-aligned rectangle and (potentially) a starting set of other rectangles which may overlap, I'd like to find a set of rectangles so that:
if no starting rectangle exists, one might be created; otherwise do not create additional rectangles
each of the rectangles in the starting set are expanded as much as possible
the overlap is minimal
the whole tiled rectangle's area is covered.
This smells a lot like a set cover problem, but it still is... different.
The key is that each starting rectangle's area has to be maximized while still minimizing general overlap. A good solution keeps a balance between necessary overlaps and high initial rectangles sizes.
I'd propose a rating function such as that:
Higher is better.
Examples (assumes a rectangle tiled into a 4x4 grid; numbers in tiles denote starting rectangle "ID"):
easiest case: no starting rectangles provided, can just create one and expand it fully:
.---------------. .---------------.
| | | | | | 1 | 1 | 1 | 1 |
|---|---|---|---| |---|---|---|---|
| | | | | | 1 | 1 | 1 | 1 |
|---|---|---|---| => |---|---|---|---|
| | | | | | 1 | 1 | 1 | 1 |
|---|---|---|---| |---|---|---|---|
| | | | | | 1 | 1 | 1 | 1 |
·---------------· ·---------------·
rating: 16 * 1 - 0 = 16
more sophisticated:
.---------------. .---------------. .---------------.
| 1 | 1 | | | | 1 | 1 | 1 | 1 | | 1 | 1 | 2 | 2 |
|---|---|---|---| |---|---|---|---| |---|---|---|---|
| 1 | 1 | | | | 1 | 1 | 1 | 1 | | 1 | 1 | 2 | 2 |
|---|---|---|---| => |---|---|---|---| or |---|---|---|---|
| | | 2 | 2 | | 2 | 2 | 2 | 2 | | 1 | 1 | 2 | 2 |
|---|---|---|---| |---|---|---|---| |---|---|---|---|
| | | 2 | 2 | | 2 | 2 | 2 | 2 | | 1 | 1 | 2 | 2 |
·---------------· ·---------------· ·---------------·
ratings: (4 + 4) * 2 - 0 = 16 (4 + 4) * 2 - 0 = 16
pretty bad situation, with initial overlap:
.-----------------. .-----------------------.
| 1 | | | | | 1 | 1 | 1 | 1 |
|-----|---|---|---| |-----|-----|-----|-----|
| 1,2 | 2 | | | | 1,2 | 1,2 | 1,2 | 1,2 |
|-----|---|---|---| => |-----|-----|-----|-----|
| | | | | | 2 | 2 | 2 | 2 |
|-----|---|---|---| |-----|-----|-----|-----|
| | | | | | 2 | 2 | 2 | 2 |
·-----------------· ·-----------------------·
rating: (8 + 12) * 2 - (2 + 2 + 2 + 2) = 40 - 8 = 36
covering with 1 only:
.-----------------------.
| 1 | 1 | 1 | 1 |
|-----|-----|-----|-----|
| 1,2 | 1,2 | 1 | 1 |
=> |-----|-----|-----|-----|
| 1 | 1 | 1 | 1 |
|-----|-----|-----|-----|
| 1 | 1 | 1 | 1 |
·-----------------------·
rating: (16 + 2) * 1 - (2 + 2) = 18 - 4 = 16
more starting rectangles, also overlap:
.-----------------. .---------------------.
| 1 | 1,2 | 2 | | | 1 | 1,2 | 1,2 | 1,2 |
|---|-----|---|---| |---|-----|-----|-----|
| 1 | 1 | | | | 1 | 1 | 1 | 1 |
|---|-----|---|---| => |---|-----|-----|-----|
| 3 | | | | | 3 | 3 | 3 | 3 |
|---|-----|---|---| |---|-----|-----|-----|
| | | | | | 3 | 3 | 3 | 3 |
·-----------------· ·---------------------·
rating: (8 + 3 + 8) * 3 - (2 + 2 + 2) = 57 - 6 = 51
The starting rectangles may be located anywhere in the tiled rectangle and have any size (minimum bound 1 tile).
The starting grid might be as big as 33x33 currently, though potentially bigger in the future.
I haven't been able to reduce this problem instantiation to a well-problem, but this may only be my own inability.
My current approach to solve this in an efficient way would go like this:
if list of starting rects empty:
create starting rect in tile (0,0)
for each starting rect:
calculate the distances in x and y direction to the next object (or wall)
sort distances in ascending order
while free space:
pick rect with lowest distance
expand it in lowest distance direction
I'm unsure if this gives the optimal solution or really is the most efficient one... and naturally if there are edge cases this approach would fail on.
Proposed attack. Your mileage may vary. Shipping costs higher outside the EU.
Make a list of open tiles
Make a list of rectangles (dimension & corners)
We're going to try making +1 growth steps: expand some rectangle one unit in a chosen direction. In each iteration, find the +1 with the highest score. Iterate until the entire room (large rectangle) is covered.
Scoring suggestions:
Count the squares added by the extension: open squares are +1; occupied squares are -1 for each other rectangle overlapped.
For instance, in this starting position:
- - 3 3
1 1 12 -
- - 2 -
...if we try to extend rectangle 3 down one row, we get +1 for the empty square on the right, but -2 for overlapping both 1 and 2.
Divide this score by the current rectangle area. In the example above, we would have (+1 - 2) / (1*2), or -1/2 as the score for that move ... not a good idea, probably.
The entire first iteration would consider the moves below; directions are Up-Down-Left-Right
rect dir score
1 U 0.33 = (2-1)/3
1 D 0.33 = (2-1)/3
1 R 0.33 = (1-0)/3
2 U -0.00 = (0-1)/2
2 L 0.00 = (1-1)/2
2 R 0.50 = (2-1)/2
3 D 0.00 = (1-1)/2
3 L 0.50 = (1-0)/2
We have a tie for best score: 2 R and 3 L. I'll add a minor criterion of taking the greater expansion, 2 tiles over 1. This gives:
- - 3 3
1 1 12 2
- - 2 2
For the second iteration:
rect dir score
1 U 0.33 = (2-1)/3
1 D 0.33 = (2-1)/3
1 R 0.00 = (0-1)/3
2 U -0.50 = (0-2)/4
2 L 0.00 = (1-1)/4
3 D -1.00 = (0-2)/2
3 L 0.50 = (1-0)/2
Naturally, the tie from last time is now the sole top choice, since the two did not conflict:
- 3 3 3
1 1 12 2
- - 2 2
Possible optimization: If a +1 has no overlap, extend it as far as you can (avoiding overlap) before computing scores.
In the final two iterations, we will similarly get 3 L and 1 D as our choices, finishing with
3 3 3 3
1 1 12 2
1 1 2 2
Note that this algorithm will not get the same answer for your "pretty bad example": this one will cover the entire room with 2, reducing to only 2 overlap squares. If you'd rather have 1 expand in that case, we'll need a factor for the proportion of another rectangle that you're covering, instead of my constant value of 1.
Does that look like a tractable starting point for you?

Detect early tie in Connect 4

I am looking for an algorithm to detect an early tie in a Connect 4 game. As of now, I already check if the board is full and no wins have been detected but I would like to know as soon as the game can be deduced a tie.
For example, consider this game, where player B just played at position Row-5, Column-0:
|
v
5 | B | | | | | | |
4 | A | B | A | B | A | B | A |
3 | A | B | A | B | A | B | A |
2 | B | A | B | A | B | A | B |
1 | B | A | B | A | B | A | B |
0 | A | B | A | B | A | B | A |
0 1 2 3 4 5 6
Then, the game is not considered a tie, because there is still a way for player B to win. Though if player A plays at Row-5, Column-1:
|
v
5 | B | A | | | | | |
4 | A | B | A | B | A | B | A |
3 | A | B | A | B | A | B | A |
2 | B | A | B | A | B | A | B |
1 | B | A | B | A | B | A | B |
0 | A | B | A | B | A | B | A |
0 1 2 3 4 5 6
At this point, either player has no way to win: it going to be a tie. I would like the algorithm to notify the user of this right away.
Check all possible runs of 4-in-a-row on the board, horizontal, vertical and diagonal. If all of them contain at least one A and at least one B then it's going to be a tie. If there is even one that is made up of a combination of empty and A or empty and B (assuming there are no rows of 4-in-a-row A or 4-in-a-row B, in which case you already have a win), then a win by A or B is still possible.
You probably already have code to check for a win, so just adapt it to check for 4-in-a-row of A or empty, or 4-in-a-row of B or empty instead of 4-in-a-row of A, or 4-in-a-row of B. If it fails to detect a possible win then a tie is inevitable.
One wrinkle is when there are a small number of empty spaces on the board. In this case you need to work out how many remaining moves A and B have and only allow them that many number of empties in the calculation, e.g. check for 4-in-a-row of A and a maximum of 2 empties. For example if there are 5 empty spaces left and it's B's turn then A has 2 moves left and B has 3.
One case it won't handle is if there is a single empty column left - there might be enough space for one player to stack 4 in a row but they can't because the players have to alternate.

how to find maximum of a group of rows in a certain column which satisfies a condition in tibco spotfire

I want to find maximum of a group of rows in a certain column which satisfies a condition in TIBCO Spotfire. For example, consider the table below:
col 1|col 2|col 3
1 | 2 | y
1 | 3 | y
1 | 6 | y
1 | 8 | n
1 | 7 | n
1 | 6 | y
2 | 2 | y
2 | 10 | y
2 | 6 | y
2 | 9 | n
2 | 7 | y
2 | 6 | n
I want to group all the rows with [col 1] = 1, and find the max of col 2 considering only those rows that have [col 3] = "y".
My final table must look like:
col 1|col 2|col 3|col 4
1 | 2 | y | 6
1 | 3 | y | 6
1 | 6 | y | 6
1 | 8 | n | 6
1 | 7 | n | 6
1 | 6 | y | 6
2 | 2 | y | 10
2 | 10 | y | 10
2 | 6 | y | 10
2 | 9 | n | 10
2 | 7 | y | 10
2 | 6 | n | 10
Can some one please help me out with this?
First(case when [col 3]="y" then Max([col 2]) OVER ([col 1]) end) OVER ([col 1]) should do the trick (version 7.5).
Thanks!
I came up with something that sounds like what you already tried, but here goes.
Insert Calculated Column: CASE WHEN [col 3]="y" THEN Max([col 2]) OVER ([col 1]) END AS [calc]
Insert Calculated Column: Max([calc]) OVER ([col 1]) AS [col 4]
Those give me the value in [col 4] that you were looking for.
#monte_fisto in the similar case can we identify the min and max of a col2

world needs an algorithm to find numbers summing up to the same number in a 2d square

I have been watching a TV talent show and one guy just challenged the whole
country (!) to solve a problem. I feel like I can write a small script to solve
it but I still need to recognize the problem somehow. So the problem goes like
this:
+---+---+---+
| | | | -->
+---+---+---+
| | | | --> sum of
+---+---+---+ 3 rows
| | | | -->
+---+---+---+
| | | also sum of
v v v 2 diagonals
sum of
3 columns
Write numbers from 1 to 9 to the squares above to get the same sum accross all
marked lines (e.g. sum of 3 rows, 3 columns and 2 diagonals).
He then continued to show the solution to this instance of the problem by
temporarily extending the big square and writing numbers in the order as:
+---+
| 3 |
+---+---+---+
| 2 | | 6 |
+---+---+---+---+---+
| 1 | | 5 | | 9 |
+---+---+---+---+---+
| 4 | | 8 |
+---+---+---+
| 7 |
+---+
He then deleted the extra squares and placed the values in them to the
farthest empty squares respectively:
+---+---+---+
| 2 | 7 | 6 |
+---+---+---+
| 9 | 5 | 1 |
+---+---+---+
| 4 | 3 | 8 |
+---+---+---+
Then he got the sums:
rows:
2 + 7 + 6 = 15
9 + 5 + 1 = 15
4 + 3 + 8 = 15
columns:
2 + 9 + 4 = 15
7 + 5 + 3 = 15
6 + 1 + 8 = 15
diagonals:
2 + 5 + 8 = 15
6 + 5 + 4 = 15
So the problem is to solve this with a 100 by 100 square.
What problem is this?
Is it NP complete?
How can I solve this?
I may be misremembering some of the details but it's not on youtube yet
so feel free to suggest changes to the problem.
NOTE TV is awesome
It's called 'magic square', wikipedia gives a few examples of algorithms to generate one.
Such squares are called MAGIC SQUARES. Read about their construction at http://en.wikipedia.org/wiki/Magic_square#Method_for_constructing_a_magic_square_of_odd_order

Resources