function hourglassSum(arr) {
let sumHourGlass = [];
for (let i = 0; i < arr.length - 2; i++) {
for (let j = 0; j < arr[i].length; j++) {
const pos1 = arr[i][j];
const pos2 = arr[i][j + 1];
const pos3 = arr[i][j + 2];
const pos4 = arr[i + 1][j + 1];
const pos5 = arr[i + 2][j];
const pos6 = arr[i + 2][j + 1];
const pos7 = arr[i + 2][j + 2];
const sum = pos1 + pos2 + pos3 + pos4 + pos5 + pos6 + pos7;
if (!Number.isNaN(sum)) {
sumHourGlass.push(sum);
}
}
}
return Math.max(...sumHourGlass);
}
Last active
August 31, 2023 17:54
-
-
Save rogigs/8c13a647500aba492b8adac19f334889 to your computer and use it in GitHub Desktop.
HackerRank Solutions
function countingValleys(steps, path) {
const arrPath = path.split("");
let position = 0;
let valley = 0;
for (let i = 0; i < steps; i++) {
if (arrPath[i] === "U") {
position += 1;
if (position === 0) {
valley += 1;
}
} else {
position -= 1;
}
}
return valley;
}
That solution can be used to do movimentation of characters on games. The DVD splash screen is a famous example.
The same logic of Minimum Swap
- Need review
function minimumBribes(q) {
let minBribes = 0;
for (let i = 0; i < q.length; i++) {
if (q[i] - (i + 1) > 2) {
console.log("Too chaotic");
return;
} else {
for (let j = Math.max(0, q[i] - 2); j < i; j++) {
if (q[j] > q[i]) {
minBribes++;
}
}
}
}
console.log(minBribes);
}
function repeatedString(s, n) {
const stringArr = s.split("");
const qntElements = stringArr.filter((el) => el === "a").length;
const qntStringShowOnArr = n / stringArr.length;
if (Number.isInteger(qntStringShowOnArr)) {
return +(qntElements * qntStringShowOnArr);
}
const qntStringShowOnArrInteger =
Math.floor(qntStringShowOnArr) * stringArr.length;
const howPositionsMissed = n - qntStringShowOnArrInteger;
let toSum = 0;
for (let i = 0; i < howPositionsMissed; i++) {
if (stringArr[i] === "a") {
toSum++;
}
}
return +(qntElements * Math.floor(qntStringShowOnArr) + toSum);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment