Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- javaCoding
- ์๋ฐ ์ฝ๋ฉํ ์คํธ
- ์ฝํ
- ์ฝ๋ฉํ ์คํธ ์๋ฐ
- ModelViewPresenter
- ํ๋ฉด ํฌ๊ธฐ ๊ตฌํ๊ธฐ
- JavaCodingTest
- ์๋ฐ
- ์ฐํ ํ๊ธ๋ฐ
- Android
- ๋ฐฑ์ค
- kotlin
- ์ฝ๋ฉํ ์คํธ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ๋ทฐ๋ฐ์ธ๋ฉ
- ์ค๋ธ์
- viewpager2
- Java
- ์๋๋ก์ด๋
- ์ฝํ๋ฆฐ
- CodingTestJava
- programmers
- ScreenSize
- pattern
- ์ฝ๋ฉํ ์คํธ JAVA
- CodingTest
- baekjoon
- Coding-Test
- ์ฝํ ์๋ฐ
- BottomNavigation
Archives
- Today
- Total
KDLiam
[Programmers ์ฝ๋ฉ ๊ธฐ์ด ํธ๋ ์ด๋ : Java] ์ฃผ์ฌ์ ๊ฒ์ 3 ๋ณธ๋ฌธ
Problems(Java)/Programmers
[Programmers ์ฝ๋ฉ ๊ธฐ์ด ํธ๋ ์ด๋ : Java] ์ฃผ์ฌ์ ๊ฒ์ 3
KDLiam 2025. 10. 15. 14:46๐ฒ ์ฃผ์ฌ์ ๊ฒ์ 3 (ํ๋ก๊ทธ๋๋จธ์ค)
๋งํฌ : https://school.programmers.co.kr/learn/courses/30/lessons/181916
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
๋ฌธ์ ์์ฝ
- 4๊ฐ์ ์ฃผ์ฌ์ ์ซ์(a, b, c, d)๊ฐ ์ฃผ์ด์ง.
- ๊ท์น์ ๋ฐ๋ผ ์ ์ ๊ณ์ฐ:
- ๋ค ์ฃผ์ฌ์ ๋ชจ๋ ๊ฐ์ → 1111 × ์ฃผ์ฌ์ ์ซ์
- ๋ค ์ฃผ์ฌ์ ๋ชจ๋ ๋ค๋ฆ → ์ต์ ์ซ์
- ์ธ ์ฃผ์ฌ์ ๊ฐ๊ณ ํ๋ ๋ค๋ฆ → (10 × ๋์ผ ์ซ์ + ๋ค๋ฅธ ์ซ์)^2
- ๋ ์ฃผ์ฌ์ ๊ฐ๊ณ ๋๋จธ์ง ๋ ์ฃผ์ฌ์ ๊ฐ๊ฐ ๋ค๋ฆ → (๊ฐ์ ์ฃผ์ฌ์ ์ซ์ + ๋ค๋ฅธ ์ฃผ์ฌ์ ์ซ์) × |๊ฐ์ ์ฃผ์ฌ์ ์ซ์ - ๋ค๋ฅธ ์ฃผ์ฌ์ ์ซ์|
- ๋ ์ ๊ฐ์ ๊ฒฝ์ฐ → (์ฒซ ์ ์ซ์ + ๋ ๋ฒ์งธ ์ ์ซ์) × |์ฒซ ์ ์ซ์ - ๋ ๋ฒ์งธ ์ ์ซ์|
1๏ธโฃ ์ฒ์ ์์ฑํ ์ฝ๋
class Solution {
public int solution(int a, int b, int c, int d) {
int answer = 0; // ๋ธ๋ก ๋ฐ์์ ์ ์ธ
// 1. ๋ค ์ฃผ์ฌ์ ๋ชจ๋ ๊ฐ์
if(a == b && b == c && c == d) {
answer = 1111 * a;
// 2. ๋ค ์ฃผ์ฌ์ ๋ชจ๋ ๋ค๋ฆ
} else if(a!=b && a!=c && a!=d &&
b!=c && b!=d &&
c!=d) {
answer = Math.min(Math.min(a,b), Math.min(c,d));
// 3. ์ธ ์ฃผ์ฌ์๊ฐ ๊ฐ๊ณ ๋๋จธ์ง ํ๋ ๋ค๋ฆ
} else if((a==b && b==c) || (a==b && b==d) ||
(a==c && c==d) || (b==c && c==d)) {
if(a==b && b==c) answer = (int)Math.pow(10*a + d, 2);
else if(a==b && b==d) answer = (int)Math.pow(10*a + c, 2);
else if(a==c && c==d) answer = (int)Math.pow(10*a + b, 2);
else if(b==c && c==d) answer = (int)Math.pow(10*b + a, 2);
// 4. ๋ ์ฃผ์ฌ์๊ฐ ๊ฐ๊ณ ๋๋จธ์ง ๋ ์ฃผ์ฌ์๊ฐ ์๋ก ๋ค๋ฅด๊ฑฐ๋ ๋ ์ ๊ฐ์ ๊ฒฝ์ฐ
} else {
// ๋ ์ ๊ฐ์ ๊ฒฝ์ฐ
if(a==b && c==d) answer = (a + c) * Math.abs(a - c);
else if(a==c && b==d) answer = (a + b) * Math.abs(a - b);
else if(a==d && b==c) answer = (a + b) * Math.abs(a - b);
// ํ๋๋ง ์ค๋ณต
else if(a==b) answer = c * d;
else if(a==c) answer = b * d;
else if(a==d) answer = b * c;
else if(b==c) answer = a * d;
else if(b==d) answer = a * c;
else if(c==d) answer = a * b;
}
return answer;
}
}
์ฅ์
- ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ๋ช ์ → ๋ ผ๋ฆฌ ํ๋ฆ ์ง๊ด์
- ๊ฐ๋จํ if-else ๊ตฌ์กฐ๋ก ๋ฐ๋ก ์ดํด ๊ฐ๋ฅ
๋จ์
- ์กฐ๊ฑด๋ฌธ์ด ๊ธธ๊ณ ์ค๋ณต๋จ → ์ ์ง๋ณด์ ์ด๋ ค์
- ์ซ์๊ฐ ๋ง์์ง์๋ก ์กฐ๊ฑด์ด ํญ๋ฐ์ ์ผ๋ก ์ฆ๊ฐ
- ์ฐ์ฐ ๋ฐ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์ ๋นํจ์จ์
2๏ธโฃ ๊ฐ์ ํ ์ฝ๋ (Stream + ๋ฐฐ์ด ๊ธฐ๋ฐ)
import java.util.*;
class Solution {
private int findSingleIndex(int a, int b, int c, int d) {
if(a==b && b==c) return 3;
else if(a==b && b==d) return 2;
else if(a==c && c==d) return 1;
else return 0;
}
public int solution(int a, int b, int c, int d) {
int answer = 0;
// ๋ฐฐ์ด๋ก ์์ฑ
int[] nums = {a, b, c, d};
int[] distinctNums = Arrays.stream(nums)
.distinct() // ์ค๋ณต ์ ๊ฑฐ
.sorted() // ์ ๋ ฌ
.toArray();
// System.out.println(Arrays.toString(nums)); // ๋ฐฐ์ด ์ถ๋ ฅ
// 1. ๋ชจ๋ ๊ฐ์ ์ซ์ => ๋ฐฐ์ด ์ฌ์ด์ฆ 1
if(distinctNums.length == 1) {
answer = 1111*distinctNums[0];
// 2. ๋ชจ๋ ๋ค๋ฅธ ์ซ์ => ๋ฐฐ์ด ์ฌ์ด์ฆ 4
} else if(distinctNums.length == 4) {
answer = Math.min(Math.min(distinctNums[0], distinctNums[1]), Math.min(distinctNums[2], distinctNums[3]));
// 3. ์ธ ์ฃผ์ฌ์๊ฐ ๊ฐ์ ์ซ์ => ๋ฐฐ์ด ์ฌ์ด์ฆ 2
} else if (distinctNums.length == 2) {
int cnt1 = 0;
int cnt2 = 0;
// ๋ช๊ฐ๊ฐ ๊ฐ์์ง ํ๋ณ
for(int n: nums) {
if(n==distinctNums[0]) cnt1++;
else cnt2++;
}
// 3๊ฐ ๊ฐ๊ณ , 1๊ฐ ๋ค๋ฆ
if(cnt1==3 || cnt2==3) {
int p=0;
int q=0;
// ์ด๋ค ์ซ์๊ฐ 3๊ฐ์ฉ ๋์๋์ง ํ๋ณ
switch(findSingleIndex(a, b, c, d)) {
case 0: p=b; q=a; break;
case 1: p=a; q=b; break;
case 2: p=a; q=c; break;
case 3: p=a; q=d; break;
}
answer = (10*p + q) * (10 * p + q);
} else {
int p = distinctNums[0];
int q = distinctNums[1];
answer = (p+q) * Math.abs(p-q);
}
// 4. ๋ ์ฃผ์ฌ์๊ฐ ๊ฐ๊ณ , 2๊ฐ๊ฐ ๊ฐ๊ฐ ๋ค๋ฆ =>
} else {
// ๋ญ๊ฐ 2๋ฒ ๋์จ์ง ํ์ธ ํ ๋ค๋ฅธ๊ฒ ํฉํ๊ธฐ
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;
int q = 0;
int r = 0;
for(int n: nums) {
if(n == distinctNums[0]) cnt1++;
else if(n == distinctNums[1]) cnt2++;
else cnt3++;
}
if(cnt1 == 2) {
q = distinctNums[1];
r = distinctNums[2];
} else if (cnt2 == 2) {
q = distinctNums[0];
r = distinctNums[2];
} else {
q = distinctNums[0];
r = distinctNums[1];
}
answer = q*r;
}
return answer;
}
}
์ฅ์
- ๋ฐฐ์ด + Stream API ํ์ฉ → ์ซ์ ์ค๋ณต ์ ๊ฑฐ, ์ ๋ ฌ, ์ต์๊ฐ ํ์ธ ๊ฐํธ
- ์กฐ๊ฑด๋ฌธ์ด ๋จ์ํ → ์ ์ง๋ณด์ ์ฉ์ด
- ์ฐ์ฐ๋ ์ต์ํ (ํ์ํ ๊ฒฝ์ฐ์๋ง ๊ณ์ฐ)
๋จ์
- ์ฒ์ ๋ณด๋ ์ฌ๋์๊ฒ๋ Stream API์ distinct() ๋ฑ ์ต์ํ์ง ์์ ์ ์์
- switch๋ฌธ๊ณผ ๋ฐฐ์ด ์ธ๋ฑ์ค ์ฐ์ฐ์ด ์กฐ๊ธ ๋ณต์ก
3๏ธโฃ ๋ ์ฝ๋ ๋น๊ต
๊ตฌ๋ถ์ฒ์ ์ฝ๋๊ฐ์ ์ฝ๋
๊ฐ๋ ์ฑ | ์กฐ๊ฑด๋ฌธ ๊ธธ๊ณ ์ค๋ณต ๋ง์ | ๋ฐฐ์ด + Stream, ๊ฒฝ์ฐ ๋๋๊ธฐ ๊ฐ๊ฒฐ |
์ ์ง๋ณด์ | ์ด๋ ค์, ์กฐ๊ฑด ์ถ๊ฐ ์ ๋ณต์ก | ์ฉ์ด, ์ซ์๊ฐ ๋์ด๋๋ ๊ตฌ์กฐ ์ ์ง ๊ฐ๋ฅ |
์ฑ๋ฅ | ์์ ๊ฐ์๋ผ ํฐ ๋ฌธ์ ์์, ํ์ง๋ง ์กฐ๊ฑด๋ฌธ ๋ง์ | Stream๊ณผ distinct ์ฌ์ฉ → O(n) ๋ด์ธ, ์์ ์์๋ผ ์ฒด๊ฐ ์์ |
๋ณต์ก๋ | O(1) (ํญ์ 4๊ฐ ์ซ์) | O(1), Stream ์ฌ์ฉ์ผ๋ก ์ฝ๊ฐ ์ค๋ฒํค๋ ์์ง๋ง ์์ ์์ค |
4๏ธโฃ ์ต์ข ํ๊ฐ
- ์ฒ์ ์ฝ๋: ์ง๊ด์ ์ด์ง๋ง, ์กฐ๊ฑด๋ฌธ์ด ๊ธธ๊ณ ์ ์ง๋ณด์๊ฐ ์ด๋ ต๋ค.
- ๊ฐ์ ์ฝ๋: ๊ฐ๋
์ฑ, ์ ์ง๋ณด์, ๊ตฌ์กฐ์ ๋ช
ํ์ฑ์์ ์ฐ์.
- Stream + ๋ฐฐ์ด ์ ๊ทผ์ผ๋ก ์ค๋ณต ์ ๊ฑฐ, ์ ๋ ฌ, ์ต์๊ฐ ํ์์ด ๊ฐ๋จํด์ง.
- ์ค์ ์ฑ๋ฅ ์ฐจ์ด๋ ๋ฏธ๋ฏธํ์ง๋ง, ์ฝ๋ ํ์ฅ์ฑ๊ณผ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์ ํจ์ฌ ์ข์.
๐ก ๊ฒฐ๋ก : ํ์ ์ฝ๋๋ ํ์ ํ๊ฒฝ์์๋ ๊ฐ์ ์ฝ๋๋ฅผ ์ถ์ฒ