Skip to content

Instantly share code, notes, and snippets.

@rogigs
Last active August 31, 2023 17:54
Show Gist options
  • Save rogigs/8c13a647500aba492b8adac19f334889 to your computer and use it in GitHub Desktop.
Save rogigs/8c13a647500aba492b8adac19f334889 to your computer and use it in GitHub Desktop.
HackerRank Solutions

Solution 2D Array - DS

Screenshot 2023-08-31 132447

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);

}

Solution Arrays: Left Rotation

Screenshot 2023-08-30 192714

function rotLeft(a, d) {
  let auxArr = a;
  for (let i = 0; i < d; i++) {
    auxArr.push(auxArr[0]);
    auxArr.shift();
  }

  return auxArr;
}

Solution Counting Valleys

Screenshot 2023-08-29 011615

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;
}

Aplicability

That solution can be used to do movimentation of characters on games. The DVD splash screen is a famous example.

Screenshot 2023-08-29 011153

Solution Two Strings

Screenshot 2023-08-31 145302

function checkMagazine(magazine, note) {
  for (const word of note) {
    if (!magazine.includes(word)) {
      console.log("No");
      return;
    }

    magazine.splice(magazine.indexOf(word), 1);
  }

  console.log("Yes");
}

Solution Jumping On The Cloud

Screenshot 2023-08-30 182250

Review this solution

function jumpingOnClouds(c) {
  let qntJump = 0;
  let i = 0;
  while (i < c.length) {
    if (c[i] === 1) {
      i++;
    } else {
      if (c[i + 2] === 0) {
        i = i + 2;
      } else {
        i++;
      }

      qntJump++;
    }
  }

  return qntJump - 1;
}

Solution Minimum Swaps 2

Screenshot 2023-08-31 134516

function minimumSwaps(arr) {
  let swap = 0;
  for (let i = 0; i < arr.length; i++) {
    while (arr[i] !== i + 1) {
      const aux = arr[i];
      arr[i] = arr[aux - 1];
      arr[aux - 1] = aux;
      swap++;
    }
  }

  return swap;
}

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);
}

Solution Repeated String

Screenshot 2023-08-28 225955

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);
}

Solution Sales By Match

Screenshot 2023-08-30 185532

function sockMerchant(n, ar) {
   let arrAux = [];

  for (let index = 0; index < n; index++) {
    if (!arrAux.includes(index)) {
      for (let index2 = index + 1; index2 < n; index2++) {
        if (ar[index] === ar[index2]) {
          arrAux.push(index2);
          break;
        }
      }
    }
  }

  return arrAux.length;
}

Solution Two Strings

Screenshot 2023-08-31 144456

function twoStrings(s1, s2) {
  for (const value of s1) {
    if (s2.includes(value)) {
      return "YES";
    }
  }
  return "NO";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment