-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.js
More file actions
33 lines (27 loc) · 728 Bytes
/
solution.js
File metadata and controls
33 lines (27 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use strict";
/**
*
* @param {number} column
* @param {string[][]} matrix
*/
function isColumnSorted(column, matrix) {
for (let i = 1; i < matrix.length; i++)
if (matrix[i][column] <= matrix[i - 1][column]) return false;
return true;
}
/**
*
* @param {string[][]} matrix
*/
function solve(matrix) {
let count = 0;
for (let i = 0; i < matrix[0].length; i++)
if (!isColumnSorted(i, matrix)) count++;
return count;
}
const matrix1 = [["c", "b", "a"], ["d", "a", "f"], ["g", "h", "i"]];
const matrix2 = [["a", "b", "c", "d"]];
const matrix3 = [["z", "y", "x"], ["w", "v", "u"], ["t", "s", "r"]];
console.log(solve(matrix1));
console.log(solve(matrix2));
console.log(solve(matrix3));