Post

[Summer/Winter Coding(~2018)] 숫자 게임

[Summer/Winter Coding(~2018)] 숫자 게임

🙋‍♂️ 들어가며

이번 문제는 그리디를 생각해보았는데 그게 아니더라

그리디가 아닌 이유로 반례는 다음과 같다

1
2
3
A = {8, 8, 2, 2};
B = {1, 1, 8, 8};
res = 2;

이건 two-pointer 로 풀 수 있겠다

✅ 코드

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
import java.util.Arrays;

class Solution {
    public int solution(int[] A, int[] B) {
        int answer = 0;
        
        Arrays.sort(A);
        Arrays.sort(B);
        int N = B.length;
        
        // A 시작점
        // B 시작점
        int s1 = 0;
        int s2 = 0;
        while (true) {
            if (s1 >= N) break;
            if (s2 >= N) break;
            
            if (A[s1] >= B[s2]) s2++;
            else if (A[s1] < B[s2]) {
                s1++;
                s2++;
                answer++;
            }
        }
        return answer;
    }
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?