Showing posts with label maximum value. Show all posts
Showing posts with label maximum value. Show all posts

Saturday, December 31, 2016

Code Review - Find biggest basin

Dec. 31, 2016

Find biggest basin


Julia's favorite review: 

http://codereview.stackexchange.com/a/49477/123986

Julia, write a C# practice as well.

BFS/ DFS algorithms' Review

Julia, please review BFS/ DFS algorithms practiced this year.

Search blog using keyword: BFS

1. Connected Cell in a Grid - Warm up with Five Practices

Use Queue/ Stack/ Recursive function, get familiar with all five algorithms.

2. Leetcode 317: Shortest distance from all buildings



Monday, June 20, 2016

Leetcode 329: Longest increasing path in matrix

June  20, 2016


problem statement:
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
329. Longest Increasing Path in a matrix

Example 1:
nums = [
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
Return 4
The longest increasing path is [1, 2, 6, 9].
Study the code written in Java:
1st practice using C#: 
Question and answer:
1. How is the practice? 
The study was very good. DFS - using recursive call, and then, tricky part is to use memorization to 
avoid duplicated calculation. 

2. Talk more with an easy example, therefore, next time you will not forget the problem after 
a few months. 
Answer: Will write something here. 



1. Talk about node row = 2, col =1, value is 1, denoted as start1,  3 neigbors, left (value 2), right (value 1), up(value 6)
2. Try to avoid loops - base case checking, always go to the node value >= current value >= start node '1'
3. Also, if neighbour node left (value 2) has maximum increasing path in matrix n, then, what we can know:
   value 1 < value 2, then, start1's longest path at least 1 + value2's longest increasing path. Need to check other 2 
  neighbors to see if it is larger value

    left neighbor (value 2)'s longest increasing path in matrix 2->6->9, length is 3;
    right neighbor (value 1)'s longest increasing path in matrix 1->8, length is 2; 
    upper neighbor (value 6)'s longest increasing path in matrix (actually 2 of them, 6->9 or 6->8), length is 2. 

4. DFS algorithm can be set up using recursive function; also, there are total 3*3 = 9 nodes in the above matrix, 
    each node's increasing path in matrix should not count more than once. 
    For example, nums[0,0] = 9, is maximum value of matrix, so the longest path = 1. 

    Let us put a sentence together. How about the following:
    Be greedy, check your neighbors (at most 4), and find maximum one with longest increasing path in matrix, and then use the path
to build your own. The value is 1 + that neighbor's problem, where the recursive function is constructed. 

5. The design of algorithm - brute force solution - how many paths in the matrix - n^4 = n x n x n x n; filter out non-
increasing path, then find maximum value - not efficient
6. For each node in matrix, find its longest increasing path in matrix, nxn nodes, each node, DFS algorithm is applied. 
    at most nxn node is checked about increasing order. <- try to reason the time complexity -> ...
    Will be less than n^4. 

7. Prepare a check list for the design:
    1. Run time issue - index out of range - array - boundary check 
        loops - always go to bigger value - no loop 
    2. time complexity - nxn node, each one does DFS; each DFS, at most 4 n^2 comparison of value. 
    3. space complexity - use extra array nxn to store bool value - memorization  
    4. value is bigger/ smaller / 0, 0 - not possible, at least 1, miss count - recursive, should be easy. 
    5. Brute force solution and its issues - more time consuming etc., duplication calculation    

Most important discussion: 
find the idea to store in extra array: 
1. Extra array to store the length of "longest increasing path in matrix" for each node in the array - (working idea)
2. Extra array to mark the node is visited or not -  (not enough for calculation!)

 denote DFS function to calculate the longest increasing path in matrix, then starting from start1, the function will be

left->left->up->up, in other words, from 1->2->6->9, and then, 
up->right,    1->6->8   
up-> up,       1->6->9, 
the visiting order of neighbors is left, down, right, up.
anti-clockwise, left, down, right, up  - line 116 - 120
So, the cache value of (2, 1) is 4, longest path is 4 (1->2->6->9); and 6 nodes in matrix are calculated and saved with 
cache value - 2 matrix(2,0), 6(1,0), 9(0,0), 6(1,1), 8(1,2), 9(0,1), the order of saving is 
9(0,0),          - 0 neighbor
6(1,0),          - 1 neighbor
2 matrix(2,0) - 1neighbor, value 1
6(1,1)
8(1,2)            
9(0,1)
6(1,1)   - 2 neighbors, comparison 1 vs 1 
1(2,1)   - 2 neighbors, comparison 3 vs 2

Use stack to help to track the order: 
start1 node, 2 neighbors
go to left neighbor first, 
      push to stack, 2, 6, 9
no down neighbor, skip right
then go to up neighbour, 
     ...


Statistics: 
Time to work out order of calculation cache value in the above list takes more than 10 minutes. 

Two motivations to work on reasoning and analysis:
1. Leetcode 329 is hard
2. Being able to write down bug free, executable code in 20 minutes. 

  3. Study more solutions from others, and then, practice it using C#. Study one using C++. 
C++ solution:


To be continued. 

Wednesday, May 11, 2016

HackerRank – Connected Cell in a Grid - Warm up with Five Practices

May 11, 2016

Being a software programmer, it is easy to spend hours to read and catch up technologies, work on new algorithm, but no coding, one day, or one week, even half month/ month. 

So, warm up like sports. Julia chose the algorithm - Connected Cell In a Grid to warm up for a few hours. 

Last time, less than 1 month ago, Julia did work on this algorithm – connected cell in a grid. And then, she started to warm up again.

Here is one of blogs last practice on April 16, 2016.

First practice, it takes her close to 60 minutes to write, fix issues.  Use dimensional array, use queue to do BFS – breadth first search.


Here are mistakes:
      1. Forget to add boundary check function, do boundary check (source code: line 108)

      2. Forget to introduce neighbor_X, neighbor_Y  (source code: line 90, 91)

      3. Neighbor_X is mistakenly written as neighbor_Y, so wrong answer;
          Debug the code and find the issue. It takes more than 20 minutes, a lot of stress. (source code: line 96)

So, it is excellent chance to learn and improve the performance.

Write a small function to debug the code, figure out the wrong answer issue – testRoutine, source code: line 36.


Second practice, using queue, but use jagged array:  (20 minutes to write)

          

Third practice, use DFS – recursive function, which also returns the count.

         

Fourth practice, using DFS – recursive function, but use an argument – reference int to track value


Fifth practice, using stack instead of recursive function, implement the DFS algorithm:



Question and answer:

      1. What do you learn through the warm up? Do you learn some better ways to fix the bugs?
It is better to write down the functions needed to help the task, this way, you will be more efficient.

Here are 4 tasks:
     1. Calculate the key
     2. Boundary check 
     3. Maximum value search
     4. Using queue to do search

     2.  Why do you do warm up this time? What are the advantages?

Julia still remembers the favorite tip to work on the tasks:
1.    Just mark the visited node as 0 from value 1
2.    Update node value from 1 to 0 before it is added to the queue
3.    Use key = row * 10 + col, since row < 10, col < 10 to track each node in the queue

Julia likes to write code and do some warm up, therefore, she can get more experience; she tries to improve performance to 20 minutes for this kind of DFS, BFS, matrix, search algorithm.

3. Do you reproduce the experience of high stress to trouble shooting and work on bug fix? 

Julia reproduced the issue of high stress, she could not fix the bug on her first practice. So, she wrote a small debug function try to figure out; actually, it is a mistake in writing. 

Next time, reexamine every line of code, every variable, every executable path, when the code is executed. Do not depend on debugging, running the code, because stress level is high.