Saturday, April 27, 2019

Matrix


  1. If we need to update matrix, please don't update it first then find the next ones need to update. What we should do is to find all the items need to update and do it once.
  2. Find sub matrix
    1. It's O(n3) time complexity, O(n2)for space complexity, the outmost iteration is for getting all the possible sub matrixes. 
    2. In each sub matrix, use an array to store the sum(or other rule) of each row/column, and result is also putting in a map or other data structure for future use.
  3. Minimum/Maximum triangle path sum
    • Create an array that the length is equal to triangle's level, to store the sum from root to the node.
      if (j == i) {
       // First
       m[j] = m[j-1] + cur.get(j);
      } else if (j == 0) { 
       // Last
       m[j] = m[0] + cur.get(0);
      } else {
       // Others
       m[j] = Math.min(m[j-1], m[j]) + cur.get(j);
      }
    • After it's finished, find the max/min value in the array.
  4. Search in ordered matrix
    • Start from top right, the trace for searching 9 is [20 > 10 > 5 > 6 > 9]
      1    5    10   20
      2    6    11   30
      7    9    12   40
      8    15   31   41
  5. TODO..

No comments:

Post a Comment