Showing posts with label From matrix region sum to jigsaw. Show all posts
Showing posts with label From matrix region sum to jigsaw. Show all posts

Thursday, July 5, 2018

Group discussion

July 5, 2018

Introduction


It is very pleasant to see myself to involve a small group and then we  had good discussion about algorithm together this Tuesday from 8:30 PM to 10:00 PM. This past Tuesday 8:30 PM, we had three people taking turn to discuss one algorithm a time. I had chance to give out an algorithm Jigsaw algorithm and lead the discussion with other two.

Discussion 


I always think about one algorithm last two weeks. I need more challenging thing to do. Last long weekend I worked on seven hard level algorithms. But I still thought about same algorithm again and again. My brain is working this way. So amazing! I better do not waste my energy on the same algorithm. 

Here is the discussion transcript. 

I also like to continue to search and find some interesting to read related to how to design API. 


Actionable Item


It is time for me to do some study about how a person can develop strong interest on algorithm problem solving. 

I used to read mathematics for computer science book. I found that it is very interesting to read. 

How to motivate myself to write 100 lines of code everyday?

July 5, 2018

Introduction


I spent over one hour to write a hard level algorithm called Maximum gap today. I noticed that it is too slow compared to those algorithms I wrote more than 20 times, for example, I can write Sudoku Solver in less than 30 minutes, and I even can remember how many variables I need and how many statements I need to write down, less than 40 lines of code.

I think that I need to train myself to write 100 lines of code against Leetcode online judge every day. There are 128 lines of code for the algorithm Maximum gap including empty line.

If I can write 100 lines of code, and also use Leetcode online judge to train myself to think more carefully about edge cases, I also learn to implement the idea. As a programmer, the most important part is to be able to write perfectly working code in limited time.

Leetcode algorithms


I only submitted Leetcode through online judge for 97 algorithms since 2015. I cannot compete with people with more than 500 algorithm submissions.

Actionable Items


I always think about how to solve jigsaw algorithm recently. One thing I like to do is to ask myself before I write code, can I make my idea more advanced in terms of time complexity, can I scale the problem to millions of row, do I have time out issue or run-out-of-space concern?


Saturday, June 30, 2018

Design a function from matrix region sum to jigsaw problem

June 30, 2018

Introduction


It is my most favorite algorithm back in 2015. I had this algorithm exactly same one in 2015. It is called to calculate the subarea sum of matrix.

Here are a few resources.

1. matrix region sum
2. My practice on June 25, 2015, matrix region sum
3. My blog about the algorithm, June 25, 2015. The blog is here. I made a few corrections to make the blog more readable.
4. My blog on April 26, 2016, here is the link.
5. Leetcode 304 - Range Sum query 2D - Immutable


Jigsaw problem 


I recently worked on the design of a jigsaw problem. And also I had discussion with two peers. We tried to identify the problem as a classical algorithm. What is the brute force solution? what can we do better.

I read my solution and I think that it is still a brute force solution. If the function is called a lot of times, we need to reduce the time complexity.

Here is my code written less than a month.

What is the time complexity of my solve function? I put preprocessing function inside the solve function. That is a mistake since it defines time complexity to O(row * row * column * column).

The code was written but time complexity is defined by preprocessing function, which is the bigger one.
The solution I think will be better if I remove the preprocessing and build a dictionary outside the function. We only need to do it once. Therefore the time complexity of solve function can be lowered to O(rows * columns) since it takes O(1) time to get every piece's four neighbors.

Actually the second for loop on line 30 should include all pieces except itself.