https://www.acmicpc.net/problem/10818
10818번: 최소, 최대
첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.
www.acmicpc.net
첫번째 풀이
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[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
System.out.println(arr[0] + " " + arr[arr.length-1]);
}
}
두번째 풀이
블로그 참고했던 예전 풀이 보아서 적었고, 배열이랑 정렬메서드를 사용 안해서 시간복잡도 상 효율적인 풀이이다.
public class Boj10818_최소최대 {
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 max = -10000001;
int min = 10000001;
for (int i = 0; i < n; i++) {
int temp = Integer.parseInt(st.nextToken());
if (temp > max) {
max = temp;
}
if (temp < min) {
min = temp;
}
}
System.out.println(min+ " " + max);
}
}

'알고리즘 > 백준' 카테고리의 다른 글
백준 13752 히스토그램 (0) | 2022.12.06 |
---|---|
백준 16673 고려대학교에는 공식 와인이 있다 (0) | 2022.12.06 |
백준 10951 A+B-4 (0) | 2022.03.02 |
백준 1924 2007년 (0) | 2022.03.02 |
백준 2745 진법변환 (Java) (0) | 2022.03.01 |