Skip to content

[김보경] 06-03 크레인 인형뽑기 #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bokeeeey
Copy link
Contributor

✔ 문제 제목: 크레인 인형뽑기

✔ 문제 유형: stack

✔ 문제 풀이 날짜: 09.13

💡 문제 분석 요약

  • 이차원배열 순회와 stack을 사용

💡 알고리즘 설계

  • 이중 for문으로 이차원 배열 순회

💡코드

function solution(board, moves) {
  let answer = 0;
  let stack = [];
  let array = [...board];

  for (let i of moves) {
    for (let arr of array) {
      let val = arr[i - 1];
      if (val) {
        if (stack[stack.length - 1] === val) {
          stack.pop();
          answer += 2;
        } else {
          stack.push(arr[i - 1]);
        }
        arr[i - 1] = 0;
        break;
      }
    }
  }

  return answer;
}

let a = [
  [0, 0, 0, 0, 0],
  [0, 0, 1, 0, 3],
  [0, 2, 5, 0, 1],
  [4, 2, 4, 4, 2],
  [3, 5, 1, 3, 1],
];

let b = [1, 5, 3, 5, 1, 2, 1, 4];
console.log(solution(a, b));

💡 틀린 이유와 틀린 부분 수정 or 다른 풀이

  • for문을 한번만 사용해서 풀수 있는데 코드가 잘 이해가 안되네요
const transpose = matrix =>
    matrix.reduce(
        (result, row) => row.map((_, i) => [...(result[i] || []), row[i]]),
        []
    );

const solution = (board, moves) => {
    const stacks = transpose(board).map(row =>
        row.reverse().filter(el => el !== 0)
    );
    const basket = [];
    let result = 0;

    for (const move of moves) {
        const pop = stacks[move - 1].pop();
        if (!pop) continue;
        if (pop === basket[basket.length - 1]) {
            basket.pop();
            result += 2;
            continue;
        }
        basket.push(pop);
    }

    return result;
};

💡 느낀 점 or 기억할 정보

  • 정봉찬은 카와이하다

Copy link
Contributor

@devwqc devwqc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요~ 🪆

저는 stackbascket을 각각 배열로 선언해서 풀었는데 그냥 board0 처리를 하면 됐군요.
잘 푸신 거 같아요. 👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯👏💯

Copy link
Contributor

@ayoung-iya ayoung-iya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 풀이의 예시는 주어진 보드를 재배치 한 거 같아요~

[
  [0, 0, 0, 0, 0],
  [0, 0, 1, 0, 3],
  [0, 2, 5, 0, 1],
  [4, 2, 4, 4, 2], 
  [3, 5, 1, 3, 1],  
]; 
        ⬇️
[
  [0, 0, 0, 4, 3], 
  [0, 0, 2, 2, 5],
  [0, 1, 5, 4, 1], 
  [0, 0, 0, 4, 3],
  [0, 3, 1, 2, 1]
]
         ⬇️
[
  [4, 3], 
  [2, 2, 5],
  [1, 5, 4, 1], 
  [4, 3],
  [3, 1, 2, 1]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants