This is my answer use ruby, but it not accept, could someone fix it.


  • -1
    J
    def longest_increasing_path(matrix)
      row = matrix.size
      return 0 if row == 0
      column = matrix.first.size
      count = 0
      matrix.each_with_index do |m, i|
        m.each_with_index do |tip, j|
          s = r_seek(matrix, row, column, matrix[i][j],i, j)
          count = (count > s ?  count : s )
        end
      end
      return count
    end
    
    def r_seek(matrix, row, column, v, i, j)
      a = 1
      if (left = j-1) > -1
        now = matrix[i][left]
        if now > v
          r = 1 + r_seek(matrix, row, column, now, i, left)
          a = a > r ? a : r
        end
      end
      if (up = i-1) > -1
        now = matrix[up][j]
        if now > v
          r = 1 + r_seek(matrix, row, column, now, up, j)
          a = a > r ? a : r
        end
      end
      if (right=j+1) < column
        now = matrix[i][right]
        if now > v
          r = 1 + r_seek(matrix, row, column, now, i, right)
          a = a > r ? a : r
        end
      end
      if (down=i+1) < row
        now = matrix[down][j]
        if now > v
          r = 1 + r_seek(matrix, row, column, now, down, j)
          a = a > r ? a : r
        end
      end
      return a
    end

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.