We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Ema's Supercomputer
Ema's Supercomputer
Sort by
recency
|
312 Discussions
|
Please Login in order to post a comment
It took me over a day figuring this out. I finally devised a working algorithm while I was thinking about it at work. These problems are all about solving the math (getting exactly how the algorithm works). Once I have the algorithm down, it took me just 1-2 hours for actual implementation. Trying to just code a solution to these problems without a working plan (which I tried initially) will be a disaster.
My solution uses brute force. It essentially tries all possible permutations and pick the best one. My approach is to use the center to slowly expand out and check for 'G'. It'll be too much to explain everything but if you're interested, you can take a look at the code. Coded in python:
Finally done in 3 submission. Like others, always struggling on finding a smarter way but brute force at the end.
Here's something I would like to share that I think it's a smarter way to find max possible pluses on each block. This is NOT to find 2 pluses with max product. It's only an idea of how to determine the largest plus if current cell is the center.
An easier way to find a plus is iterating on each cell and trying to count the connecting good cells on 4 directions. I saw many dissuasions were doing this. But form my understanding, this could lead to an O(n*m*(n+m)) runtime because each cell needs to look up the entire rows (n) and column (m), as well as there are n*m cells.
My idea is counting the maximum length while iterating on each cell. During the iteration, it would marks a count number on each cell continuously. If it is good, count + 1. When a bad cell is hit or the row/column is end, count = 0 and update previous 1/2 count of good cells. For example, if we have 6 good cells in a row, it would be marked as 1 2 3 4 5 6 after the iteration. When the iteration hit the 7th cell (a bad cell), it would update this row to 1 2 3 3 2 1. In this case, each number on the cell represents the max number of good cells on the right and left (including the current cell) if current cell is a center. By doing same thing horizontally, we can have numbers that represents max number of good cells on the top and bottom if current cell is center. Combining these two table together, we can have the possible max size of plus on each cell. In this method, the worst case runtime should be O(n*m).
.
If you fail only a few cases, consider smaller size pluses with the same center as they might not overlapse while the biggest one does.
Solution in java:
My answer in Typescript, explain in comment