Skip to content

Commit 5c50555

Browse files
committed
[LEET] Two Sum (Easy)
1 parent 560d7a4 commit 5c50555

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

test

33.6 KB
Binary file not shown.

test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//https://www.acmicpc.net/problem/2805
2+
3+
#include <iostream>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
int N, M, ans;
8+
int height[1000001];
9+
int maxheight = 0;
10+
11+
void binary(){
12+
int left = 0;
13+
int right = maxheight; // 절단기 최대 높이
14+
15+
while(left <= right) {
16+
int mid = (left + right) / 2; // 현재 절단기 높이
17+
18+
long long sum = 0;
19+
for(int i = 0; i < N; i++){
20+
if (height[i] - mid > 0) {
21+
sum += height[i] - mid;
22+
}
23+
}
24+
if (sum < M) {
25+
right = mid - 1;
26+
} else if (sum >= M) {
27+
left = mid + 1;
28+
ans = max(mid, ans);
29+
}
30+
}
31+
return;
32+
}
33+
34+
int main() {
35+
ios_base::sync_with_stdio(false);
36+
cin.tie(NULL); cout.tie(NULL);
37+
38+
cin >> N >> M;
39+
for(int i = 0; i < N; i++){
40+
cin >> height[i];
41+
maxheight = max(maxheight, height[i]);
42+
}
43+
44+
binary();
45+
cout << ans;
46+
47+
return 0;
48+
}
49+

박예진/4주차/260119.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
map<int, int> m; // num, idx
5+
6+
for(int i = 0; i < nums.size(); i++){
7+
int need = target - nums[i];
8+
if (m.count(need)) {
9+
return {i, m[need]};
10+
}
11+
m[nums[i]] = i;
12+
}
13+
14+
return {};
15+
}
16+
};

0 commit comments

Comments
 (0)