Skip to content

[WEEK04-1] 최준호#10

Merged
raejun92 merged 1 commit intomainfrom
raejun-week04
Mar 4, 2026
Merged

[WEEK04-1] 최준호#10
raejun92 merged 1 commit intomainfrom
raejun-week04

Conversation

@raejun92
Copy link
Collaborator

@raejun92 raejun92 commented Mar 2, 2026

이렇게 풀었어요

1. Ransom Note

  • 문제를 풀었어요.
  • 풀이 시간 : 21분

1) 복잡도 계산

시간 복잡도: O(n + m) — n은 magazine의 길이, m은 ransomNote의 길이


2) 접근 아이디어

  1. ransomNote에 있는 문자들의 개수를 Map에 저장한다.
  2. magazine을 탐색하면서 Map에 있는 문자들을 제거해나간다.
  3. 만약 Map이 비게 되면 true를 반환한다.

3) 회고

처음에 문자 개수를 상관하지 않아도 된다고 생각해서 Set으로 접근했다.
하지만, 같은 문자가 여러 개 있을 수 있기 때문에 Set으로는 풀이할 수 없었다. 그래서 Map으로 접근했다.



2. First Bad Version

  • 문제를 풀었어요.
  • 풀이 시간 : 22분

1) 복잡도 계산

시간 복잡도: O(log n) — n은 버전의 수


2) 접근 아이디어

  1. 1을 start로 n을 end로 두고 이진 탐색으로 풀이한다.
  2. mid가 bad version이면서 mid - 1이 bad version이 아니면 mid를 반환한다.
  3. mid가 bad version이면 end = mid, 아니면 start = mid + 1로 범위를 줄인다.

3) 회고

이진 탐색을 통해 특정한 값을 찾는 것에만 익숙해져 있어, 반복문 전에 start와 end가 isBadVersion이면 return을 하는 실수를 했다.
이진 탐색을 사용하는데 계속 시간 초과가 났다. start = mid에 +1을 해줘야 무한 루프에 빠지지 않는다는 것을 배웠다.
이진탐색에서 범위를 확실히 줄이려면 +1, -1을 해줘야 한다.



@github-actions github-actions bot requested review from doitchuu and sik9252 March 2, 2026 07:02
@raejun92 raejun92 changed the title [WEEK04] 최준호 [WEEK04-1] 최준호 Mar 2, 2026
Copy link
Member

@doitchuu doitchuu left a comment

Choose a reason for hiding this comment

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

잘 푸셨군용 👍 정석적인 풀이라 복습차원으로 다시 한 번 더 본 것 같아 좋았어요!

}
}

if (map.size === 0) return true;
Copy link
Member

Choose a reason for hiding this comment

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

early return 좋네요 👍

let start = 1;
let end = n;

while (start <= end) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

end = mid로 갱신하면서 <=를 사용하면 특정 상황에서 end가 감소하지 않아 무한루프에 빠질 수 있는 문제는 없을까요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

<=을 사용하면 특정 상황에 end가 감소하지 않을 거란 내용을 좀 더 설명해 주실 수 있나요?!

Copy link
Collaborator

@sik9252 sik9252 Mar 3, 2026

Choose a reason for hiding this comment

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

가정: firstBadVersion = 1 (즉, 1부터 전부 bad)

탐색이 진행되어 start=2, end=2 상태가 됐다고하면,

[반복 1회차]
mid = floor((2+2)/2) = 2
isBadVersion(2) = true
!isBadVersion(1) 확인
isBadVersion(1) = true → return 안됨
end = mid 실행 → end = 2 (변화 없음)
start도 그대로 2

=> 반복이 끝났는데 상태가 그대로 (start=2, end=2)

[반복 2회차]
mid = floor((2+2)/2) = 2
isBadVersion(2) = true
isBadVersion(1) = true → return 안됨
end = mid 실행 → end = 2 (변화 없음)
start도 그대로 2

=> 반복이 끝났는데 상태가 그대로 (start=2, end=2)

이렇게 무한 반복 상태가 될 수도 있을 것 같아서요(?)

그냥 단순하게, 특정 상황에 end가 감소하지 않을 거란 얘기는 아래처럼 구분해서 사용해야될것 같다는 말이였습니다!
while (start <= end) 사용할거면 end = mid - 1
while (start < end) 사용할거면 end = mid

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

start가 1에서 시작하고 start가 커지는 조건(isBadVersion이 아니다)을 생각하면 불가능한 조건이 될 것 같아요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

어어엇 일단 제가 든 예시 상에서는 준호님 말이 맞는거같기도 하고요 ㅋㅋ 계속 말하다보니 더 헷갈리네요 submit해서 통과했음 장땡이죳!!

@raejun92 raejun92 merged commit 9446fce into main Mar 4, 2026
1 check passed
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