Tuesday, August 21, 2018

Leetcode 119: Pascal's triangle II

August 21, 2018

Introduction


It is an easy level algorithm called pascal's triangle II. I had good time to work on the algorithm, I spent over 30 minutes to write a solution; And then I found out that optimal solution with minimum space only takes less than 10 lines of code.

My C# practice


My naive solution written and first submission is here.

The optimal solution using one array instead of two arrays - previous and current makes code so simple, easy to write.

Here is my C# code using the backward iteration.


My thoughts 


It is so surprising that there is so easy and quick solution for the algorithm called Pascal's triangle II. I just could not believe that  I did not find multiple solutions first, instead I chose to write one with the naive solution. 

I like to get smart next time. I like to practice easy level array algorithm and this algorithm really makes the practice so much fun. 

Here is the link on Leetcode discuss about the naive solution I wrote. 

Here is the link on Leetcode discuss about the optimal solution I wrote by copying the idea from most popular post.




No comments:

Post a Comment