Skip to content

[JustDevRae] 25.01.09#15

Merged
JooKangsan merged 14 commits intomainfrom
JustDevRae
Jan 16, 2025
Merged

[JustDevRae] 25.01.09#15
JooKangsan merged 14 commits intomainfrom
JustDevRae

Conversation

@JustDevRae
Copy link
Collaborator

📌 푼 문제


📝 간단한 풀이 과정

문자열 뒤집기

스프레드 문법을 사용해 문자열을 배열로 변환한 후, for 반복문을 통해 배열을 순회하면서 pop 메서드를 사용해 배열의 마지막 요소를 꺼내 answer에 추가하였습니다.

function solution(my_string) {
  var answer = "";
  const string = [...my_string];

  for (let i = 0; i < my_string.length; i++) {
    answer += string.pop();
  }

  return answer;
}

컨트롤 제트

split 메서드로 문자열을 공백 기준으로 나눠 배열로 변환한 뒤, 배열을 순회하며 각 요소가 'Z'인 경우 이전 값을 answer에서 빼고, 숫자인 경우 answer에 더하며 값을 갱신하도록 했습니다.

function solution(s) {
  var answer = 0;
  let Z = 0;
  const controlArray = s.split(" ");
  for (let i = 0; i < controlArray.length; i++) {
    if (controlArray[i] == "Z") {
      answer -= Z;
      continue;
    }
    answer += Number(controlArray[i]);
    Z = Number(controlArray[i]);
  }

  return answer;
}

크레인 인형뽑기 게임

moves 배열에 따라 board에서 인형을 하나씩 꺼내 stack 배열에 저장하고, 같은 인형이 연속으로 쌓이면 stack에서 제거하며 점수를 2점씩 추가하도록 했습니다.

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

  for (let move of moves) {
    for (let i = 0; i < board.length; i++) {
      if (board[i][move - 1] !== 0) {
        let doll = board[i][move - 1];
        board[i][move - 1] = 0;

        if (stack.length > 0 && stack[stack.length - 1] === doll) {
          stack.pop();
          answer += 2;
        } else {
          stack.push(doll);
        }
        break;
      }
    }
  }

  return answer;
}

올바른 괄호

문자열을 순회하며 조건문을 통해 여는 괄호 '('는 stack 배열에 추가하고, 닫는 괄호 ')'는 stack 배열에서 제거하며 짝을 맞추는 방식으로 처리하였고, 순회가 끝난 후 stack이 비어 있으면 true로 반환하도록 했습니다.

function solution(s) {
  const stack = [];
  for (const c of s) {
    if (c === "(") {
      stack.push(c);
    } else if (c === ")") {
      if (stack.length === 0) {
        continue;
      } else {
        stack.pop();
      }
    }
  }

  return stack.length === 0;
}

다트 게임

정규식을 이용해 매개변수로 받은 문자열을 숫자, 문자, 기호들을 각각 나눈 배열을 반환하고, 배열을 순회하면서 각 요소가 문자나 기호일 때 해당 조건에 맞는 연산을 하도록 했습니다.

function solution(dartResult) {
  var answer = 0;
  const dartResultArray = dartResult.match(/\d+|[SDT]|\#|\*/g);
  const stack = [];
  let value;
  
  for (str of dartResultArray) {
    if (str === "S") {
      value = stack.pop();
      stack.push(value);
    } else if (str === "D") {
      value = stack.pop();
      stack.push(value * value);
    } else if (str === "T") {
      value = stack.pop();
      stack.push(value * value * value);
    } else if (str === "*") {
      let firtPopValue = stack.pop();
      firtPopValue *= 2;
      let secondPopValue = stack.pop();
      if (secondPopValue === undefined) {
        stack.push(firtPopValue);
      } else {
        secondPopValue *= 2;
        stack.push(secondPopValue);
        stack.push(firtPopValue);
      }
    } else if (str === "#") {
      value = stack.pop();
      stack.push(value * -1);
    } else {
      stack.push(Number(str));
    }
  }

  for (element of stack) {
    answer += element;
  }

  return answer;
}

Copy link
Collaborator

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

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

전체적으로 설명이 잘 되어있어서 읽기 편했던거 같아요!! 수고하셨습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

매 반복마다 pop() 연산을 수행하므로 O(n) 시간 복잡도를 가집니다!!
reverse()메서드를 사용해 보는 건 어떨까요??

Copy link
Collaborator

@Moonjonghoo Moonjonghoo left a comment

Choose a reason for hiding this comment

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

강산님이 리뷰를 잘해주셔서 떠오르는게없었습니다
한주간 고생하셨어요!

@JooKangsan JooKangsan merged commit 4daf7e4 into main Jan 16, 2025
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