move to 4kyu
This commit is contained in:
26
4kyu/smallest-possible-sum/description.md
Normal file
26
4kyu/smallest-possible-sum/description.md
Normal file
@@ -0,0 +1,26 @@
|
||||
Description
|
||||
Given an array X of positive integers, its elements are to be transformed by running the following operation on them as many times as required:
|
||||
|
||||
if X[i] > X[j] then X[i] = X[i] - X[j]
|
||||
|
||||
When no more transformations are possible, return its sum ("smallest possible sum").
|
||||
|
||||
For instance, the successive transformation of the elements of input X = [6, 9, 21] is detailed below:
|
||||
|
||||
X_1 = [6, 9, 12] # -> X_1[2] = X[2] - X[1] = 21 - 9
|
||||
X_2 = [6, 9, 6] # -> X_2[2] = X_1[2] - X_1[0] = 12 - 6
|
||||
X_3 = [6, 3, 6] # -> X_3[1] = X_2[1] - X_2[0] = 9 - 6
|
||||
X_4 = [6, 3, 3] # -> X_4[2] = X_3[2] - X_3[1] = 6 - 3
|
||||
X_5 = [3, 3, 3] # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3
|
||||
The returning output is the sum of the final transformation (here 9).
|
||||
|
||||
Example
|
||||
solution([6, 9, 21]) #-> 9
|
||||
Solution steps:
|
||||
[6, 9, 12] #-> X[2] = 21 - 9
|
||||
[6, 9, 6] #-> X[2] = 12 - 6
|
||||
[6, 3, 6] #-> X[1] = 9 - 6
|
||||
[6, 3, 3] #-> X[2] = 6 - 3
|
||||
[3, 3, 3] #-> X[1] = 6 - 3
|
||||
Additional notes:
|
||||
There are performance tests consisted of very big numbers and arrays of size at least 30000. Please write an efficient algorithm to prevent timeout.
|
||||
59
4kyu/smallest-possible-sum/solution.ts
Normal file
59
4kyu/smallest-possible-sum/solution.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
// idea: do not ever sort
|
||||
// simply keep looping from left to right and back to left
|
||||
// whenever a smaller number is found,
|
||||
// subtract as many multiples of that number from the rest of the array
|
||||
|
||||
const subtractMultiples = (a: number, b: number): number => {
|
||||
// subtract a from b as many times as possible
|
||||
const mod = b % a;
|
||||
if (mod === 0) {
|
||||
// figure out how many times a can be subtracted from b
|
||||
const div = b / a - 1;
|
||||
return b - a * div;
|
||||
} else {
|
||||
return mod;
|
||||
}
|
||||
}
|
||||
|
||||
const operate = (arr: number[]): boolean => {
|
||||
|
||||
let step = 1;
|
||||
let i = 0;
|
||||
let done = false;
|
||||
let hasChangedDuringLoop = false;
|
||||
while (!done) {
|
||||
if (i + step >= arr.length || i + step < 0) {
|
||||
// reverse direction
|
||||
step = -step;
|
||||
if (!hasChangedDuringLoop) {
|
||||
done = true;
|
||||
}
|
||||
hasChangedDuringLoop = false;
|
||||
}
|
||||
|
||||
const first = arr[i];
|
||||
const next = arr[i + step];
|
||||
|
||||
if (first > next) {
|
||||
arr[i] = subtractMultiples(next, first);
|
||||
hasChangedDuringLoop = true;
|
||||
} else if (first < next) {
|
||||
arr[i + step] = subtractMultiples(first, next);
|
||||
hasChangedDuringLoop = true;
|
||||
}
|
||||
|
||||
i += step;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function solution(numbers: number[]): number {
|
||||
let done = false;
|
||||
while (!done) {
|
||||
done = operate(numbers);
|
||||
}
|
||||
|
||||
// sum the numbers
|
||||
return numbers.reduce((acc, num) => acc + num, 0);
|
||||
}
|
||||
16
4kyu/smallest-possible-sum/tests.ts
Normal file
16
4kyu/smallest-possible-sum/tests.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// See https://www.chaijs.com for how to use Chai.
|
||||
import { assert } from "chai";
|
||||
|
||||
import { solution } from "./solution";
|
||||
|
||||
describe('Example test', function () {
|
||||
it('[6,9,12]', () => assert.strictEqual(solution([6, 9, 21]), 9));
|
||||
it('[1,21,55]', () => assert.strictEqual(solution([1, 21, 55]), 3));
|
||||
it('[3,13,23,7,83]', () => assert.strictEqual(solution([3, 13, 23, 7, 83]), 5));
|
||||
it('[4,16,24]', () => assert.strictEqual(solution([4, 16, 24]), 12));
|
||||
it('[30,12]', () => assert.strictEqual(solution([30, 12]), 12));
|
||||
it('[60,12,96,48,60,24,72,36,72,72,48]', () => assert.strictEqual(solution([60, 12, 96, 48, 60, 24, 72, 36, 72, 72, 48]), 132));
|
||||
it('[71,71,71,71,71,71,71,71,71,71,71,71,71]', () => assert.strictEqual(solution([71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71]), 71 * 13));
|
||||
it('[11,22]', () => assert.strictEqual(solution([11,22]), 22));
|
||||
it('[9]', () => assert.strictEqual(solution([9]), 9));
|
||||
});
|
||||
Reference in New Issue
Block a user