Skip to content

[b41-41] WEEK 01 solutions #1161

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

Merged
merged 7 commits into from
Apr 5, 2025
Merged

[b41-41] WEEK 01 solutions #1161

merged 7 commits into from
Apr 5, 2025

Conversation

b41-41
Copy link
Contributor

@b41-41 b41-41 commented Mar 31, 2025

답안 제출 문제

작성자 체크 리스트

  • 우측 메뉴에서 PR을 Projects에 추가해주세요.
  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@b41-41 b41-41 requested a review from HoonDongKang March 31, 2025 16:29
@b41-41 b41-41 self-assigned this Mar 31, 2025
@github-actions github-actions bot added the ts label Mar 31, 2025
@b41-41 b41-41 removed the request for review from HoonDongKang March 31, 2025 16:30
@JANGSEYEONG JANGSEYEONG self-requested a review March 31, 2025 16:35
@b41-41 b41-41 moved this from Solving to In Review in 리트코드 스터디 4기 Apr 4, 2025
function containsDuplicate(nums: number[]): boolean {
const numSet = new Set();

for(let num of nums) {
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 반복문으로 구현하셔도 충분히 잘 작동하지만, 조금 더 간단한 방법을 제안드려요!

초반에 nums 배열을 바로 Set으로 변환하면 중복 숫자가 자동으로 제거되니 원본 배열 길이와 Set의 크기가 다르다면 중복 숫자가 있었다는 의미가 됩니다!

요 방식을 사용하면 코드가 더 간결해질 수 있을 것 같아요! 😊

  const numsSet = new Set(nums);
  return nums.length !== numsSet.size;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Set 객체 사용하면 중복 제거가 되는데,
for문을 다시 돌렸네요.. 🫠

감사합니다!!

@@ -0,0 +1,24 @@
function longestConsecutive(nums: number[]): number {
const sortedNums = [...new Set(nums.sort((a, b) => a - b))];
Copy link
Contributor

Choose a reason for hiding this comment

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

자바스크립트의 sort 함수는 O(n log n)의 시간복잡도를 가지는데, 문제에서는 'You must write an algorithm that runs in O(n) time'라고 명시되어 있었어요! (근데 저도 n log n으로 풀어봤었는데 통과는 되더라는 .... )

Set을 활용한 O(n) 접근법을 고려해보시면 좋을 것 같습니다. 연속 수열의 시작점을 찾는 방식은 정렬 없이도 문제를 해결할 수 있더라구요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Set과 Set.has를 사용하니 정말 쉽게 풀리네요!
'You must write an algorithm that runs in O(n) time'문구를 놓치고 문제를 풀었습니다.....

설명 감사합니다!!

// 2차 시도
function twoSum(nums: number[], target: number): number[] {
for (const index in nums) {
for (let index2 = Number(index) + 1; index2 < nums.length; index2++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

현재 구현하신 방식은 이중 반복문을 사용하여 O(n²) 시간 복잡도를 가지는 것으로 보여요!
배열 크기가 클수록 성능 차이가 많이 날 수 있다고 생각됩니다.

Map 자료구조를 활용하면 한 번의 순회만으로 O(n) 시간 복잡도로 문제를 해결할 수 있는데, 각 숫자를 확인하면서 '현재 값 + x = target'이 되는 x를 찾는 대신, 'target - 현재 값 = x'를 Map에 저장해두는 방식도 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

말씀하신 방법으로 구현해 보니 더 깔끔하고,
반복도 줄일 수 있어서 좋네요.

감사합니다!

@b41-41 b41-41 merged commit f66e759 into DaleStudy:main Apr 5, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 4기 Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants