[JavaScript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)

Posted GoldenaArcher

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[JavaScript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)相关的知识,希望对你有一定的参考价值。

[javascript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)

这是一个关于矩阵的题,也不是特变难,主要还是需要注意题目说的是什么意思。

题目地址:Matrix Elements Sum

题目

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there’s a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don’t appear below a 0).

Example

  • For

    matrix = [[0, 1, 1, 2],
            [0, 5, 0, 0],
            [2, 0, 3, 3]]
    

    the output should be

    matrixElementsSum(matrix) = 9.

    There are several haunted rooms, so we’ll disregard them as well as any rooms beneath them. Thus, the answer is 1 + 5 + 1 + 2 = 9.

  • For

      matrix = [[1, 1, 1, 0],
            [0, 5, 0, 1],
            [2, 1, 3, 10]]
    

    the output should be

    matrixElementsSum(matrix) = 9.

    Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.

Input/Output:

  • [execution time limit] 4 seconds (js)

  • [input] array.array.integer matrix

    A 2-dimensional array of integers representing the cost of each room in the building. A value of 0 indicates that the room is haunted.

    Guaranteed constraints:

    1 ≤ matrix.length ≤ 5,

    1 ≤ matrix[i].length ≤ 5,

    0 ≤ matrix[i][j] ≤ 10.

  • [output] integer

    The total price of all the rooms that are suitable for the CodeBots to live in.

解题思路

两个重点:

  1. 有鬼(房间为 0) 的房间是不可以住的

  2. or any of the rooms below any of the free rooms

    有鬼的房间正下方的所有房间,都可以视为有鬼的房间。

以第一个例子来说:

[
    [1, 1, 1, 0],
    [0, 5, 0, 1],
    [2, 1, 3, 10]
]

可以等同于这个结构:

[
    [1, 1, 1, 0],
    [0, 5, 0, 0],
    [0, 1, 0, 0]
]

注意到 0 下方的房间都可以使用 0 去替换,使得最后的结果变成 1 + 5 + 1 + 2 = 9

使用 JavaScript 解题

function matrixElementsSum(matrix) {
  let sum = 0;
  for (let row = 0; row < matrix.length; row++) {
    const currRow = matrix[row];
    for (let col = 0; col < currRow.length; col++) {
      // they refuse to stay in any of the free rooms
      if (currRow[col] === 0) {
        // or any of the rooms below any of the free rooms.
        if (row + 1 < matrix.length) {
          matrix[row + 1][col] = 0;
        }
      } else {
        sum += currRow[col];
      }
    }
  }
  return sum;
}

以上是关于[JavaScript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)的主要内容,如果未能解决你的问题,请参考以下文章

[JavaScript 刷题] Code Signal - 形状面积(shapeArea)

[JavaScript 刷题] Code Signal - 幸运数(is lucky)

[JavaScript 刷题] Code Signal - 翻转括号内字符(reverseInParentheses)

[JavaScript 刷题] Code Signal - 相似数组(Are Similar?)

[JavaScript 刷题] Code Signal - 共用字符数(commonCharacterCount)

[JavaScript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)