-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK10] 이배진 #45
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
[WEEK10] 이배진 #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수가 역순 정렬이 안좋다는 걸 생각하셔서 최대한 성능 좋게 구현하신 점이 좋네요 👍 |
||
| } | ||
| } | ||
| 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 타입으로 리턴) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주석까지.....🥲👍
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이진수로 바꿔주는 메서드군요!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4진수는 따로 함수가 없고 8진수는 Integer.toOctalString(Int타입 변수) 로 내장함수로 쉽게 변환이 가능합니다! |
||
| tfCnt++; | ||
| } | ||
|
|
||
| int[] answer = {tfCnt, rzCnt}; | ||
| return answer; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java는 내림차순을 하려면 오름차순 정렬 후에 뒤집어야 하는군요ㅠ
비효율을 효율적으로 풀어내시려는 모습에 한 수 배워갑니다!