8000 Merge pull request #431 from HyeonJaePark/0x13-14921 · Korcp/basic-algo-lecture@317dcac · GitHub
[go: up one dir, main page]

Skip to content

Commit 317dcac

Browse files
Merge pull request encrypted-def#431 from HyeonJaePark/0x13-14921
Update 14921.cpp
2 parents 8b26bd8 + 8bf17be commit 317dcac

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

0x13/solutions/14921.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : HJPark
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/3b73c9a75b7c4d44be56fd6af6fc9fed
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int main(void){
7+
int n;
8+
int a[100005];
9+
int ans = 1e9 + 1;
10+
11+
int main(void) {
812
ios::sync_with_stdio(0);
913
cin.tie(0);
10-
11-
}
14+
15+
cin >> n;
16+
for (int i = 0; i < n; i++) cin >> a[i];
17+
18+
int st = 0, en = n - 1;
19+
while (st < en) {
20+
int val = a[st] + a[en];
21+
if (abs(val) < abs(ans)) ans = val;
22+
if (val < 0) st++;
23+
else en--;
24+
}
25+
26+
cout << ans;
27+
}
28+
29+
/*
30+
투포인터를 이용한 풀이
31+
st, en이 입력된 배열의 양 끝을 가르킨다.
32+
a[st]와 a[en]을 혼합한 특성값의 절대값이 ans의 절대값보다 낮을 경우 ans를 갱신한다.
33+
만약 혼합한 특성값이 0보다 작은 경우 st를 1만큼 증가시키고
34+
0보다 큰 경우 en을 1만큼 감소시킨다.
35+
*/

0 commit comments

Comments
 (0)
0