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
32 changes: 32 additions & 0 deletions LeeBJ/src/FindingMinVal.java
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.

Java는 내림차순을 하려면 오름차순 정렬 후에 뒤집어야 하는군요ㅠ
비효율을 효율적으로 풀어내시려는 모습에 한 수 배워갑니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 프로그래머스 코딩테스트 문제 Lv.2 정답률 높은 순 기준
* 문제명 : 최솟값 만들기
* 날짜 : 2026 - 04 - 10
* 풀이 시간 : 12분
* 시간복잡도: O(n log n)
* 공간복잡도: O(1)
*/
import java.util.Arrays;

public class FindingMinVal {
public static void main(String[] args) {
int[] testA = {2, 7, 9, 120};
int[] testB = {11, 26, 250, 33};

int answer = solution(testA, testB);
System.out.println(answer);
}

static int solution(int[] A, int[] B) {
int answer = 0;

Arrays.sort(A);
Arrays.sort(B);

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

return answer;
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.

함수가 역순 정렬이 안좋다는 걸 생각하셔서 최대한 성능 좋게 구현하신 점이 좋네요 👍

}
}
40 changes: 40 additions & 0 deletions LeeBJ/src/RepeatedBinaryConversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 프로그래머스 코딩테스트 문제 Lv.2 정답률 높은 순 기준
* 문제명 : 이진 변환 반복하기
* 날짜 : 2026 - 04 - 13
* 풀이 시간 : 6분
* 시간복잡도: O(n)
* 공간복잡도: O(log n)
*/
import java.util.Arrays;

public class RepeatedBinaryConversion {
public static void main(String[] args) {
String str = "110010101001";

int[] answer = solution(str);
System.out.println(Arrays.toString(answer));
}

static int[] solution(String s) {
int tfCnt = 0; // 이진 변환 횟수
int rzCnt = 0; // 제거된 0의 갯수

// s가 "1"이 될때까지 반복 (이 문제의 조건에 1이 최소 하나는 있다라는 조건이 있기에 while문 사용이 좋아보임)
while (!s.equals("1")) {
int one = 0;
for (int i=0; i<s.length(); i++) {
if (s.charAt(i) == '1') {
one++;
} else {
rzCnt++;
}
}
s = Integer.toBinaryString(one); // Java에서 사용하는 이진 변환 함수(String 타입으로 리턴)
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.

주석까지.....🥲👍

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.

이진수로 바꿔주는 메서드군요!
혹시 4진수나 16진수 같은 건 어떻게 하나요?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

4진수는 따로 함수가 없고

8진수는 Integer.toOctalString(Int타입 변수)
16진수는 Integer.toHexString(Int타입 변수)

로 내장함수로 쉽게 변환이 가능합니다!

tfCnt++;
}

int[] answer = {tfCnt, rzCnt};
return answer;
}
}
Loading