code

 avatar
user_1671475754
javascript
2 years ago
5.0 kB
5
Indexable
Never
console.log('-----------lucky numbers----------')
//tldr: find smallest element in row, from column index position of that smallest element, check if the element is the biggest in the column.


function luckyNumbersOriginal(matrix) {
  for(let row_index=0; row_index<matrix.length; row_index++){
    // find smallest number in the row (aka column)
    let row = matrix[row_index];
    let smallest_num_in_row = matrix[row_index][0] //i.e. matrix[row_index][0] === 5;
    let col_smallest_num_index = 0;
    for(let col_index=0; col_index<row.length; col_index++){
      let current_num = matrix[row_index][col_index] // i.e. matrix[0][0] === 5;
      if(current_num < smallest_num_in_row){
        smallest_num_in_row = current_num;
        col_smallest_num_index = col_index;
      }
    }
    // now that we found the smallest number for the column, we want to see if there's a number that is bigger than our current one in the same column.
    let biggest_num_in_column = smallest_num_in_row;
    let row_biggest_num_index = row_index;
    for(let row_index_2 =0; row_index_2 <matrix.length; row_index_2++){
      let current_num_to_check = matrix[row_index_2][col_smallest_num_index];
      if(current_num_to_check > biggest_num_in_column){
        biggest_num_in_column = current_num_to_check;
        row_biggest_num_index = row_index_2;
      }
    }

    // if our smallest_num_in_row is now still also the biggest_num_in_column, then return it.
    if(smallest_num_in_row === biggest_num_in_column) return smallest_num_in_row;
  }
}


console.log("-----lucky number w/ helpers------")
// 1) find smallest helper:
function find_smallest_number_index(array){
  let current_smallest_index = 0;
  for(let i=0; i<array.length; i++){
    if (array[i] < array[current_smallest_index]) current_smallest_index = i;
  }
  return current_smallest_index;
}
// console.log(find_smallest_number_index([ 5,  9, 21]))

// 2) check if given number is biggest in its column helper:
function is_biggest_in_column(row_index, col_index, matrix){
  // col_index ex: 0
  //  [[ 5,  9, 21],
  //   [ 9, 19,  6],
  //   [12, 14, 15]]
  let column_height = matrix.length; //ex: 3
  let our_number = matrix[row_index][col_index]; //ex: 5
  let biggest_number = matrix[row_index][col_index]; //ex: initial biggest = 5;

  for(let i=0; i<column_height; i++){ //0th row, 1st row, 2nd row
    let current_num = matrix[i][col_index];//ex: 5,9,12
    if(current_num > biggest_number) biggest_number = current_num;
  }
  // nothing changed, if our original number is still the biggest number, then return true.
  let true_or_false = our_number === biggest_number
  return true_or_false;
}
// console.log("is biggest: ", is_biggest_in_column(0,0,[[ 5,  9, 21], [ 9, 19,  6], [12, 14, 15]]))


// matrix = [[ 5,  9, 21],
//           [ 9, 19,  6],
//           [12, 14, 15]]

function luckyNumbers(matrix) {
  // 1st helper: find smallest number for each current row, return indices
  for(let row_index=0; row_index<matrix.length; row_index++){
    let row_array = matrix[row_index]; //0th row: [5,10,8,6]

    let col_index_smallest_num = find_smallest_number_index(row_array); //we know what column the smallest number of that row is in now.
    let row_index_smallest_num = row_index;

    // 2nd helper: given indices of the smallest number, if the smallest number is also the biggest number in its column, then return true
    // if 2nd helper with the current number returns true, then return the value answer
    if(is_biggest_in_column(row_index_smallest_num, col_index_smallest_num, matrix)){
      let our_answer = matrix[row_index_smallest_num][col_index_smallest_num]
      return our_answer;
    }
  }
}


matrix = [[ 5,  9, 21],
          [ 9, 19,  6],
          [12, 14, 15]]

console.log(luckyNumbers(matrix)); // [12]

matrix = [[ 5, 10,  8,  6],
          [10,  2,  7,  9],
          [21, 15, 19, 10]]

console.log(luckyNumbers(matrix)); // [10]


console.log("-----spiral matrix------")
let spiralOrder = function(matrix) {
  let res = []
  while(matrix.length>0) {  // cut the top->right->bottom->left sides until matrix is empty
      let top = matrix.shift()

      let bottom;
      if(matrix.pop() === undefined){
        bottom = [];
      }
      else{
        bottom = matrix.pop().reverse();
      }

      let left = [], right = []
      for (let i=0; i<matrix.length; i++) {
          if (matrix[i].length>0) right.push( matrix[i].pop() )
          if (matrix[i].length>0) left.unshift( matrix[i].shift())
      }
      res.push(...top,...right,...bottom,...left)
  }
  return res
};

matrix = [[ 1, 2, 3],
          [ 4, 5, 6],
          [ 7, 8, 9]]

console.log(spiralOrder(matrix)); // [1,2,3,6,9,8,7,4,5]

matrix = [[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9,10,11,12]]


console.log(spiralOrder(matrix)); // [1,2,3,4,8,12,11,10,9,5,6,7]