Skip to content

Conversation

@unn04012
Copy link
Collaborator

@unn04012 unn04012 commented Jan 4, 2025

[배열]

📌 푼 문제


📝 간단한 풀이 과정

배열 자르기

  • for문을 이용하여 원본 배열의 인덱스로 접근하여 새로운 배열을 반환했습니다.
function solution(numbers, num1, num2) {
  const answer = [];

  for (let i = num1; i <= num2; i++) answer.push(numbers[i]);

  return answer;
}

배열 회전시키기

  • 배열의 인덱스를 통한 접근을 활용했습니다.
  • 회전을 시키기 위해 다음 인덱스의 원소를 현재 인덱스로 가져왔습니다.
// ...
if (direction === 'right') {
    for (let i = 0; i < numbers.length - 1; i++) {
      answer[i + 1] = numbers[i];
    }
    answer[0] = numbers[numbers.length - 1];
  } else {
    for (let i = numbers.length - 1; i >= 1; i--) {
      answer[i - 1] = numbers[i];
    }
    answer[numbers.length - 1] = numbers[0];
  }//...

제일 작은 수 제거하기

  • Math.min()메서드를 이용해 현재 배열의 가장 작은 원소를 찾았습니다.
const minNum = Math.min(...arr);

나누어 떨어지는 숫자 배열

  • 나머지 연산자인 %를 이용하였으며 오름차순 정렬을 위해 arr.sort() 메서드를 이용했습니다.
//...
 for (const e of arr) {
    if (e % divisor === 0) answer.push(e);
  }
  if (answer.length === 0) answer.push(-1);

  return answer.sort((a, b) => a - b);

행렬의 곱셈

  • 행렬의 특징을 참고하여 곱셈 순서를 정의했습니다. 행렬의 특징
  • Array.reduce() 메서드를 이용해 행렬 곱의 누적합을 계산하는 방식을 이용해봤습니다.
for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      const result = arr1[i].reduce((acc, cur, idx) => acc + cur * arr2[idx][j], 0);

      answer[i][j] = result;
    }
  }

Copy link
Collaborator

@oh-chaeyeon oh-chaeyeon 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.

현재 코드에서 for을 사용해도 작동은 하지만, 반복문은 모든 수를 하나씩 순회해야 하기 때문에, 이 문제에서도 불필요하다는 생각이 듭니다. 그런 면에서, slice메서드가 배열의 특정 기간을 쉽게 추출하는 기능을 제공해서 반복문 대신 return numbers.slice(num1, num2 + 1); 하시는것을 추천드립니다:)

Comment on lines +5 to +8
for (let i = 0; i < numbers.length - 1; i++) {
answer[i + 1] = numbers[i];
}
answer[0] = numbers[numbers.length - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

현재 for을 사용하여 각 숫자를 이동시키는 것도 좋지만, 혹시 메서드를 사용하면 코드가 단순해져서 이런 방법도 추천드립니다.

  1. numbers.pop()으로 배열의 마지막 요소를 제거하고,
  2. numbers.unshift()로 제거한 요소를 배열의 맨 앞에 추가.
const last = numbers.pop(); 
numbers.unshift(last);

Comment on lines +2 to +8
const answer = [];
const minNum = Math.min(...arr);

for (const e of arr) {
if (e === minNum) continue;
answer.push(e);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

새로운 배열 answer를 생성하고, for문을 이용해서 조건을 체크하고 숫자들을 추가하고 있지만, 이 부분에서 새로운 배열을 생성하면서 조건도 체크해주는 filter 메서드도 한번 사용해보시는 것도 좋을것 같습니다:)

Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 많이 헤맨 문제였는데...Array.from을 사용한 배열 초기화한 부분과 reduce활용으로 깔끔하게 잘 구현된 코드인것 같습니다👍👍

@JooKangsan JooKangsan merged commit 05c1622 into main Jan 9, 2025
3 checks passed
bona1122 pushed a commit to bona1122/AlgorithmStudy that referenced this pull request Jan 9, 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.

4 participants