λ°μν
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 |
Tags
- μνν°μ΄
- μ«μ μ΄μ΄ λΆμ΄κΈ°
- SQL
- ν°μ€ν 리μ±λ¦°μ§
- μ° λͺ¨μ νμΌλ§
- 10κΈ°
- MySQL
- ν΅μ¬
- 14942
- νκΈ°
- μ€λΈμ
- 24955
- νΌμ¦ μ‘°κ° μ±μ°κΈ°
- μΈν΄μ
- λ°±μ€
- μ€λͺ
- SSAFY
- μΉ΄μΉ΄μ€μ½λ λ³Έμ
- μΈνΌ
- softeer
- μλ£
- ν΄κ²°
- PCCP
- 142085
- μΉ΄μΉ΄μ€
- java
- λ°°μ΄ λ리기 5
- λ±μ°μ½μ€ μ νκΈ°
- νλ‘κ·Έλλ¨Έμ€
- μ κΈ° μ½λ© μΈμ¦νκ°
Archives
- Today
- Total
κ°λ° μ₯¬μ€
[λ°±μ€/Java] 2470 λ μ©μ‘ λ³Έλ¬Έ
λ°μν
π λ¬Έμ λ§ν¬: https://www.acmicpc.net/problem/2470
π ν΄κ²° κ³Όμ
μλ‘ ν©νμ λ κ·Έ μ λκ°μ΄ 0μ μ μΌ κ°κΉμ΄ λ μ©μ‘μ μ°Ύμλ΄λ κ²μ΄ λ¬Έμ μ ν΅μ¬μ΄λ―λ‘ ν¬ ν¬μΈν° λ°©μμ νμ©(μκ° λ³΅μ‘λ: O(N), N: μ©μ‘μ κ°μ)νμμ΅λλ€.
λ μ©μ‘μ μ λκ°μ λν μ²λ¦¬μ λ μ©μ‘μ μλ‘ μ μ₯νλ κ³Όμ μ μμ΄μ κ³ λ―Όμ μ’ νμλ λ¬Έμ μ λλ€.
βοΈ μ½λ
import java.io.*;
import java.util.Arrays;
public class Main {
private static final String SPACE = " ";
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[] liquids = Arrays.stream(br.readLine().split(SPACE))
.mapToInt(Integer::parseInt)
.toArray();
Arrays.sort(liquids);
int[] result = getResult(liquids, n);
bw.write(result[0] + SPACE + result[1]);
bw.flush();
bw.close();
}
private static int[] getResult(int[] liquids, int n) {
int leftIdx = 0;
int rightIdx = n - 1;
int resultLeft = -1;
int resultRight = -1;
int tempSum = Integer.MAX_VALUE;
while (leftIdx < rightIdx) {
int curSum = liquids[leftIdx] + liquids[rightIdx];
if (Math.abs(curSum) < Math.abs(tempSum)) {
tempSum = curSum;
resultLeft = liquids[leftIdx];
resultRight = liquids[rightIdx];
}
if (curSum < 0) {
++leftIdx;
} else {
--rightIdx;
}
}
return new int[]{resultLeft, resultRight};
}
}λ°μν