Skip to content

Commit f2cede4

Browse files
author
Jakub Jirous
authored
Merge pull request #12 from jakubjirous/feature/array-change
17) Array Change
2 parents 2ab1784 + 8e2bce0 commit f2cede4

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ In preparation for technical interviews, I discovered this platform that provide
4949
14) [Alternating Sums](/src/arcade/intro/14-alternating-sums/INDEX.md)
5050
15) [Add Border](/src/arcade/intro/15-add-border/INDEX.md)
5151
16) [Are Similar](/src/arcade/intro/16-are-similar/INDEX.md)
52+
17) [Array Change](/src/arcade/intro/17-array-change/INDEX.md)
5253

5354
---
5455

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Array Change
2+
3+
You are given an array of integers.
4+
5+
On each move you are allowed to increase exactly one of its element by one.
6+
7+
Find the minimal number of moves required to obtain a strictly increasing sequence from the input.
8+
9+
---
10+
11+
### Example:
12+
13+
- For `inputArray = [1, 1, 1]`, the output should be `arrayChange(inputArray) = 3`.
14+
15+
### Input/Output:
16+
17+
- **[execution time limit]** 5 seconds (ts)
18+
19+
20+
- **[input]** array.integer `inputArray`
21+
- Guaranteed constraints:
22+
- $3 \le inputArray.length \le 10^{5}$
23+
- $-10^{5} \le inputArray[i] \le 10^{5}$
24+
25+
26+
- **[output]** integer
27+
- The minimal number of moves needed to obtain a strictly increasing sequence from `inputArray`.
28+
- It's guaranteed that for the given test cases the answer always fits signed `32`-bit integer type.
29+
30+
---
31+
32+
### Solution:
33+
34+
- [Code](/src/arcade/intro/17-array-change)
35+
- [Tests](/src/arcade/intro/17-array-change/test/array-change.test.ts)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type Input = number[];
2+
export type Output = number;
3+
4+
/**
5+
* Find the minimal number of moves required to obtain a strictly increasing sequence from the input.
6+
*
7+
* @param inputArray
8+
*/
9+
export const arrayChange = (inputArray: Input): Output => {
10+
return inputArray.reduce<Output>((acc, value, index, array) => {
11+
while (value >= array[index + 1]) {
12+
acc++;
13+
array[index + 1]++;
14+
}
15+
return acc;
16+
}, 0);
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { arrayChange, Input, Output } from "../array-change";
2+
3+
type Cases = [Input, Output][];
4+
5+
describe("Array Change", () => {
6+
const cases: Cases = [
7+
[[1, 1, 1], 3],
8+
[[-1000, 0, -2, 0], 5],
9+
[[2, 1, 10, 1], 12],
10+
[[2, 3, 3, 5, 5, 5, 4, 12, 12, 10, 15], 13],
11+
[[3122, -645, 2616, 13213, -8069], 25559],
12+
[
13+
[
14+
-787, -773, -93, 867, -28, 118, 372, 255, 355, 598, -179, -752, 794,
15+
961, -84, 296, -714, 14, 666, -265, -905, 942, -691, -379, -698, -650,
16+
637, 523, 709, -674, 574, -239, 805, -434, 597, -677, 664, 384, 726,
17+
-389, -387, 772, -603, 685, 249, 446, -631, 454, 983, 867, -158, 932,
18+
-440, 891, -12, 400, -916, 503, 185, -802, -255, 207, -952, -506, -689,
19+
425, 747, -907, -30, 102, 553, 981, -664, 75, -957, -42, 99, -750, -277,
20+
686, -884, -972, 470, 32, 439, 163, 887, 895, -555, -654, 793, 333, 143,
21+
73, 181, -512, -915, -68, 542, 799,
22+
],
23+
89510,
24+
],
25+
[
26+
[
27+
-28943, -29728, -24726, -15090, -2555, -9551, -11025, 36442, -23240,
28+
-46093, 48516, 44580, -21573, 39172, -38017, -19354, -13460, 38212,
29+
-35646, -22288, 36832, -33115, 39055, -15935, -19300, -10419, -18548,
30+
21742, -32032, 27988, -45323, 27454, -5683, -14209, -4168, 51188, 45552,
31+
9899, 49241, -25939, -8344, -25788, 6808, 6931, 6145, -30802, -518,
32+
-42362,
33+
],
34+
2020705,
35+
],
36+
[
37+
[
38+
9796, 1283, -2825, 3870, -6727, -8616, -10191, -7727, 7074, 1580, -4583,
39+
162, 2980, -3861, 9452, 8145, 1222, -1125, 5142, -5657, -974, -986,
40+
-9627, 5244, 8866, 3336, -9946, -5271, 10582, 3013, 8030, 4471, -3420,
41+
9496, -3533, -8030, 10007, 2549, -8195, 7119, 302, -5322, -3537, 209,
42+
-8134, -9176, 6336, -1771, 9851, 3644, 9629, -2603, 3988, 10579, 2221,
43+
1101, 1465, 5002, -6203, -8864, 596, 6005, 4554, 8526, 2178, -5447,
44+
-232, -9734, 7402, -3984, -7161, -2139, -3181, 10445, 4535, 6926, 7156,
45+
],
46+
737073,
47+
],
48+
[
49+
[
50+
-24875, -6401, 58256, 44456, 2244, -25333, -42389, -5975, 7650, -46343,
51+
-62011, -55366, 7802, -37699, 15461, 13309, -58664, 54557, 56324,
52+
-34397, -33024, -21934, -18861, -23196, 56542, -63986, 59833, -45610,
53+
-16948, 399, -7405, 54701, -57348, -32627, 65534, 615,
54+
],
55+
2199614,
56+
],
57+
];
58+
59+
test.each(cases)(
60+
"for inputArray = %j output should be %d",
61+
(firstArg, expectedResult) => {
62+
const result = arrayChange(firstArg);
63+
expect(result).toEqual(expectedResult);
64+
}
65+
);
66+
});

0 commit comments

Comments
 (0)