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
34 changes: 34 additions & 0 deletions raejun/이진변환반복하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function solution(s) {
let zero = 0;
let count = 0;

while (s !== "1") {
const len = s.length;

s = s.replaceAll("0", "");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

와 replaceAll은 생각지도 못했네요

const oneCount = s.length;

s = oneCount.toString(2);

zero += len - oneCount;
count++;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

변수 가독성이 좋네요 😄

}

return [count, zero];
}

/*
풀이 시간: 10분

시간 복잡도는 O(n log n)이다.
공간 복잡도는 O(1)이다.

문자열 s가 "1"이 될 때까지 반복하면서, 문자열 s에서 "0"을 제거하고, 남은 "1"의 개수를 구했다.
그 다음에, "1"의 개수를 이진수 문자열로 변환했다.
반복할 때마다 제거한 "0"의 개수를 zero에 더했고, 반복한 횟수를 count에 더했다.
문자열 s가 "1"이 되면, [count, zero]를 반환했다.

문제에서 요구사항을 그대로 구현하면 되는 문제였다.
문자열 메서드를 오랜만에 사용해서 조금 헷갈렸다.
*/
25 changes: 25 additions & 0 deletions raejun/최솟값만들기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function solution(A, B) {
var answer = 0;

A.sort((a, b) => a - b);
B.sort((a, b) => b - a);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

javascript sort함수로 역순을 이렇게 표현할 수 있군요

for (let i = 0; i < A.length; i++) {
answer += A[i] * B[i];
}

return answer;
}

/*
풀이 시간: 4분

시간 복잡도는 O(n log n)이다.
공간 복잡도는 O(1)이다.

배열 A는 오름차순으로 정렬하고, 배열 B는 내림차순으로 정렬했다.
그 다음에, 배열 A와 배열 B의 요소들을 순회하면서, 각 요소들을 곱해서 answer에 더했다.

눈치껏 최솟값과 최댓값을 곱하는 방식으로 풀이했다.
근데 왜 그게 최솟값이 되는지는 잘 모르겠다.
*/
Loading