Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions raejun/FirstBadVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/

/**
* @param {function} isBadVersion()
* @return {function}
*/
var solution = function (isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function (n) {
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해서 통과했음 장땡이죳!!

const mid = Math.floor((start + end) / 2);

if (isBadVersion(mid)) {
if (!isBadVersion(mid - 1)) return mid;

end = mid;
} else start = mid + 1;
}
};
};

/*
22분 걸림.

시간 복잡도는 O(log n)이다. n은 버전의 수이다.

1을 start로 n을 end로 두고 이진 탐색으로 풀이했다.

이진 탐색을 통해 특정한 값을 찾는 것만에 익숙해져 있어, 반복문 전에 start와 end가 isBadVersion이면 return을 하는 실수를 했다.
이진 탐색을 사용하는데 계속 시간 초과가 났다. 지피티한테 물어보니 start = mid에 +1을 해줘야 한다고 한다.
만약 start = 1, end = 2일 때 mid = 1이 된다. 만약 1이 bad version이라면 end = mid가 되어서 end도 1이 된다. 그러면 start와 end가 둘 다 1이 되어서 무한 루프에 빠지는 것이다.
이진탐색에서 범위를 확실히 줄이려면 +1, -1을 해줘야 한다.
*/
45 changes: 45 additions & 0 deletions raejun/RansomNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function (ransomNote, magazine) {
const map = new Map();

for (const char of ransomNote) {
if (map.has(char)) {
map.set(char, map.get(char) + 1);
} else {
map.set(char, 1);
}
}

for (let i = 0; i < magazine.length; i++) {
const char = magazine[i];

if (map.has(char)) {
if (map.get(char) === 1) {
map.delete(char);
} else {
map.set(char, map.get(char) - 1);
}
}

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 좋네요 👍

}

return false;
};

/*
21분 걸림.

시간 복잡도는 O(n)이다. n은 magazine의 길이이다.

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

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