λ°μν
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
- λ°±μ€
- PCCP
- 59412
- μλ£
- 165672
- 10κΈ°
- softeer
- SSAFY
- νΌμ¦ μ‘°κ° μ±μ°κΈ°
- μ§λ£κ³Όλ³ μ΄ μμ½ νμ μΆλ ₯νκΈ°
- μΈνΌ
- MySQL
- SQL
- 59409
- 쑰건μ λΆν©νλ μ€κ³ κ±°λ μν ꡬνκΈ°
- μ€λΈμ
- 12930
- μ€λͺ
- 132202
- ν΅μ¬
- 142085
- μνν°μ΄
- 146355
- μ κΈ° μ½λ© μΈμ¦νκ°
- ν°μ€ν 리μ±λ¦°μ§
- ν΄κ²°
- νκΈ°
- 14942
- νλ‘κ·Έλλ¨Έμ€
- java
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};
}
}
λ°μν