Skip to content
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

21.02.07 - [BOJ] 3273. 두 수의 합 #9

Closed
suhyunsim opened this issue Feb 14, 2021 · 0 comments
Closed

21.02.07 - [BOJ] 3273. 두 수의 합 #9

suhyunsim opened this issue Feb 14, 2021 · 0 comments
Assignees
Labels
실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제 정렬 정렬하기

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 14, 2021

문제

핵심 아이디어

어려운 점, 실수

  • 시간복잡도
  • 이중 for문 썼는데 시간 초과남. 시간 줄이려면 while로 하고 if로 앞 뒤를 줄여가면서 해야 함.
  • Arrays.parallelSort(list); 로 정렬하기
  • BufferedReader 사용하기

풀이

package main.java.com.poogle.BOJ.Q3273;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] list = new int[n];
        for (int i = 0; i < n; i++) {
            list[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.parallelSort(list);
        int x = Integer.parseInt(br.readLine());

        int cnt = 0;
        int start = 0;
        int end = n - 1;
        int sum = 0;
        while (start < end) {
            sum = list[start] + list[end];
            if (sum == x) {
                cnt++;
            }
            if (sum <= x) {
                start++;
            } else {
                end--;
            }
        }

        System.out.println(cnt);
    }
}
@suhyunsim suhyunsim self-assigned this Feb 14, 2021
@suhyunsim suhyunsim added this to the 2월 1주 차 milestone Feb 14, 2021
@suhyunsim suhyunsim changed the title 21.02.09 - [BOJ] 3273. 두 수의 합 21.02.07 - [BOJ] 3273. 두 수의 합 Feb 14, 2021
@suhyunsim suhyunsim added 실버 BOJ - 실버 정렬 정렬하기 실패 시도했지만 맞지 못한 문제 labels Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제 정렬 정렬하기
Projects
None yet
Development

No branches or pull requests

1 participant