Skip to content

Commit bf5ea2b

Browse files
author
Jakub Jirous
committed
8) matrix elements sum
1 parent 578357f commit bf5ea2b

File tree

14 files changed

+164
-33
lines changed

14 files changed

+164
-33
lines changed

src/arcade/intro/01-add/test/add.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { add, Output, Param1, Param2 } from '../add';
22

33
type Cases = [Param1, Param2, Output][];
44

5-
/**
6-
* Testing a function that returns the sum of two numbers.
7-
*/
85
describe("Add", () => {
96
const cases: Cases = [
107
[1, 2, 3],

src/arcade/intro/02-century-from-year/INDEX.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The first century spans from the year 1 up to and including the year 100, the se
88

99
### Example:
1010

11-
- For `year = 1905`, the output should be `centuryFromYear(year) = 20`
12-
- For `year = 1700`, the output should be `centuryFromYear(year) = 17`
11+
- For `year = 1905`, the output should be `centuryFromYear(year) = 20`.
12+
- For `year = 1700`, the output should be `centuryFromYear(year) = 17`.
1313

1414
### Input/Output:
1515

src/arcade/intro/02-century-from-year/test/century-from-year.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { centuryFromYear, Input, Output } from '../century-from-year';
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Given a year, return the century it is in.
7-
*/
85
describe("Century From Year", () => {
96
const cases: Cases = [
107
[1905, 20],

src/arcade/intro/03-check-palindrome/INDEX.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Given the string, check if it is a palindrome.
66

77
### Example:
88

9-
- For `inputString = "aabaa"`, the output should be `checkPalindrome(inputString) = true`
10-
- For `inputString = "abac"`, the output should be `checkPalindrome(inputString) = false`
11-
- For `inputString = "a"`, the output should be `checkPalindrome(inputString) = true`
9+
- For `inputString = "aabaa"`, the output should be `checkPalindrome(inputString) = true`.
10+
- For `inputString = "abac"`, the output should be `checkPalindrome(inputString) = false`.
11+
- For `inputString = "a"`, the output should be `checkPalindrome(inputString) = true`.
1212

1313
### Input/Output:
1414

src/arcade/intro/03-check-palindrome/test/check-palindrome.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { checkPalindrome, Input, Output } from '../check-palindrome';
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Test if it is a palindrome.
7-
*/
85
describe("Check Palindrome", () => {
96
const cases: Cases = [
107
["aabaa", true],

src/arcade/intro/04-adjacent-elements-product/test/adjacent-elements-product.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { adjacentElementsProduct, Input, Output } from '../adjacent-elements-pro
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Given an array of integers, test the pair of adjacent elements that has the largest product and return that product.
7-
*/
85
describe("Adjacent Elements Product", () => {
96
const cases: Cases = [
107
[[3, 6, -2, -5, 7, 3], 21],

src/arcade/intro/05-shape-area/INDEX.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ An `n`-interesting polygon is obtained by taking the `n - 1`-interesting polygon
1111

1212
### Example:
1313

14-
- For `n = 2`, the output should be `shapeArea(n) = 5`
15-
- For `n = 3`, the output should be `shapeArea(n) = 13`
14+
- For `n = 2`, the output should be `shapeArea(n) = 5`.
15+
- For `n = 3`, the output should be `shapeArea(n) = 13`.
1616

1717
### Input/Output:
1818

src/arcade/intro/05-shape-area/test/shape-area.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { Input, Output, shapeArea } from '../shape-area';
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Test the area of a polygon for a given n.
7-
*/
85
describe("Shape Area", () => {
96
const cases: Cases = [
107
[2, 5],

src/arcade/intro/06-make-array-consecutive/test/make-array-consecutive.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { Input, makeArrayConsecutive, Output } from '../make-array-consecutive';
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Tests the minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.
7-
*/
85
describe("Make Array Consecutive", () => {
96
const cases: Cases = [
107
[[6, 2, 3, 8], 3],

src/arcade/intro/07-almost-increasing-sequence/INDEX.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Given a sequence of integers as an array, determine whether it is possible to ob
1010

1111
### Example:
1212

13-
- For `sequence = [1, 3, 2, 1]`, the output should be `almostIncreasingSequence(sequence) = false`
13+
- For `sequence = [1, 3, 2, 1]`, the output should be `almostIncreasingSequence(sequence) = false`.
1414
- There is no one element in this array that can be removed in order to get a strictly increasing sequence.
1515

1616

17-
- For `sequence = [1, 3, 2]`, the output should be `almostIncreasingSequence(sequence) = true`
17+
- For `sequence = [1, 3, 2]`, the output should be `almostIncreasingSequence(sequence) = true`.
1818
- You can remove `3` from the array to get the strictly increasing sequence `[1, 2]`.
1919
- Alternately, you can remove `2` to get the strictly increasing sequence `[1, 3]`.
2020

@@ -31,11 +31,11 @@ Given a sequence of integers as an array, determine whether it is possible to ob
3131

3232
- **[output]** boolean
3333
- Return `true` if it is possible to remove one element from the array in order to get a strictly increasing sequence.
34-
- Otherwise return `false`.
34+
- Otherwise, return `false`.
3535

3636
---
3737

3838
### Solution:
3939

40-
- [Code](/src/arcade/intro/06-make-array-consecutive/make-array-consecutive.ts)
41-
- [Tests](/src/arcade/intro/06-make-array-consecutive/test/make-array-consecutive.test.ts)
40+
- [Code](/src/arcade/intro/07-almost-increasing-sequence/almost-increasing-sequence.ts)
41+
- [Tests](/src/arcade/intro/07-almost-increasing-sequence/test/almost-increasing-sequence.test.ts)

src/arcade/intro/07-almost-increasing-sequence/test/almost-increasing-sequence.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { almostIncreasingSequence, Input, Output } from '../almost-increasing-se
22

33
type Cases = [Input, Output][];
44

5-
/**
6-
* Tests whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
7-
*/
85
describe("Almost Increasing Sequence", () => {
96
const cases: Cases = [
107
[[1, 3, 2, 1], false],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Matrix Elements Sum
2+
3+
After becoming famous, the CodeBots decided to move into a new building together.
4+
5+
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.
6+
7+
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`).
8+
9+
---
10+
11+
### Example:
12+
13+
- For `matrix`:
14+
- ```
15+
[
16+
[0, 1, 1, 2],
17+
[0, 5, 0, 0],
18+
[2, 0, 3, 3]
19+
]
20+
```
21+
- The output should be `matrixElementsSum(matrix) = 9` .
22+
- There are several haunted rooms, so we'll disregard them as well as any rooms beneath them.
23+
- Thus, the answer is `1 + 5 + 1 + 2 = 9`.
24+
25+
26+
- For `matrix`:
27+
- ```
28+
[
29+
[1, 1, 1, 0],
30+
[0, 5, 0, 1],
31+
[2, 1, 3, 10]
32+
]
33+
```
34+
- The output should be `matrixElementsSum(matrix) = 9`.
35+
- The free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it).
36+
- Thus, the answer is `1 + 1 + 1 + 5 + 1 = 9`.
37+
38+
39+
### Input/Output:
40+
41+
- **[execution time limit]** 5 seconds (ts)
42+
43+
44+
- **[input]** array.array.integer `matrix`
45+
- A 2-dimensional array of integers representing the cost of each room in the building.
46+
- A value of `0` indicates that the room is haunted.
47+
- Guaranteed constraints:
48+
- $1 \le matrix.length \le 5}$
49+
- $1 \le matrix[i].length \le 5}$
50+
- $0 \le matrix[i][j] \le 10$
51+
52+
53+
- **[output]** integer
54+
- The total price of all the rooms that are suitable for the CodeBots to live in.
55+
56+
---
57+
58+
### Solution:
59+
60+
- [Code](/src/arcade/intro/08-matrix-elements-sum/matrix-elements-sum.ts)
61+
- [Tests](/src/arcade/intro/08-matrix-elements-sum/test/matrix-elements-sum.test.ts)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export type Input = number[][];
2+
export type Output = number;
3+
4+
/**
5+
* After becoming famous, the CodeBots decided to move into a new building together.
6+
* 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.
7+
*
8+
* @param matrix
9+
*/
10+
export const matrixElementsSum = (matrix: Input): Output => {
11+
let ghosts: number[] = [];
12+
let cost = 0;
13+
14+
for (let i = 0; i < matrix.length; i++) {
15+
for (let j = 0; j < matrix[i].length; j++) {
16+
const val = matrix[i][j];
17+
18+
if (val === 0) {
19+
ghosts.push(j);
20+
}
21+
22+
if (val !== 0 && !ghosts.includes(j)) {
23+
cost += val;
24+
}
25+
}
26+
}
27+
28+
return cost;
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { matrixElementsSum, Input, Output } from "../matrix-elements-sum";
2+
3+
type Cases = [Input, Output][];
4+
5+
describe("Matrix Elements Sum", () => {
6+
const cases: Cases = [
7+
[
8+
[
9+
[0, 1, 1, 2],
10+
[0, 5, 0, 0],
11+
[2, 0, 3, 3],
12+
],
13+
9,
14+
],
15+
[
16+
[
17+
[1, 1, 1, 0],
18+
[0, 5, 0, 1],
19+
[2, 1, 3, 10],
20+
],
21+
9,
22+
],
23+
[
24+
[
25+
[1, 1, 1],
26+
[2, 2, 2],
27+
[3, 3, 3],
28+
],
29+
18,
30+
],
31+
[[[0]], 0],
32+
[[[1], [5], [0], [2]], 6],
33+
[[[1, 2, 3, 4, 5]], 15],
34+
[
35+
[
36+
[1, 0, 3],
37+
[0, 2, 1],
38+
[1, 2, 0],
39+
],
40+
5,
41+
],
42+
[[[2], [5], [10]], 17],
43+
[
44+
[
45+
[4, 0, 1],
46+
[10, 7, 0],
47+
[0, 0, 0],
48+
[9, 1, 2],
49+
],
50+
15,
51+
],
52+
[[[1]], 1],
53+
];
54+
55+
test.each(cases)(
56+
"for matrix %j output should be %d",
57+
(firstArg, expectedResult) => {
58+
const result = matrixElementsSum(firstArg);
59+
expect(result).toEqual(expectedResult);
60+
}
61+
);
62+
});

0 commit comments

Comments
 (0)