정수 삼각형

function solution(triangle) {
  while (triangle.length > 1) {
    const shifted = triangle.shift();
    const next = triangle[0];
    for (let i = 0; i < next.length; i++) {
      if (i == 0) {
        next[i] += shifted[i];
        continue;
      }
      if (i == next.length - 1) {
        next[i] += shifted[i - 1];
        continue;
      }
      const leftParent = shifted[i - 1];
      const rightParent = shifted[i];
      next[i] +=
        next[i] + leftParent >= next[i] + rightParent
          ? leftParent
          : rightParent;
    }
  }
  return Math.max(...triangle[0]);
}

0개의 댓글