๋ฌธ์ :
๋ถ์ :
์ด ๋ฐฉ์์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค. ์ฐ์ฑ๊ณผ ์์นผ๋ฆฌ ์ฉ์ก์ ๊ตฌ๋ถํด ๋ฐฐ์ด์ ๋ด์๊ณ ๊ฐ ๋ฐฐ์ด์ด ๋๋ ๋๊น์ง O(n^2)์ผ๋ก ํ์ํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋๋ฐ ๊ฐ์ฅ ๋นํจ์จ์ ์ผ๋ก ๋ฌธ์ ๋ฅผ ํผ๊ฑฐ ๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฑํ map ํจ์๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋์๋๋ฐ ์ธ๋ฐ ์๋ ์ ์ธ์ฌ๋ฆฌ๊ฐ ๋ง์ ๋๋ ..
<๋์ฉ์ก> ๋ฌธ์ ์์ ๋ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ ์ฐพ์ ํด๊ฒฐํ์๋ค.
C++ ์ฝ๋ :
//
// 2467_solution.cpp
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2021/02/05.
//
#include <stdio.h>
#include <iostream>
#include <unordered_map>
#include <math.h>
#include <string>
using namespace std;
unordered_map<string, int> solution(int arr[],int n){
int acidArr[n];
int alkaliArr[n];
fill_n(acidArr, n, -1);
fill_n(alkaliArr, n, -1);
int acidCount = 0;
int alkaliCount = 0;
unordered_map<string, int> mapping;
mapping["acid"] = 0;
mapping["alkali"] = 0;
for(int i =0; i<n; i++){
if(arr[i] < 0){ //put in acidArr
acidArr[acidCount] = arr[i];
acidCount++;
}else if (arr[i] > 0){
alkaliArr[alkaliCount] = arr[i];
alkaliCount++;
}
}
int initDiff = 0xffff;
for(int acidJ = 0; acidArr[acidJ] != -1; acidJ++){
for(int alkaliJ = 0; alkaliArr[alkaliJ] != -1; alkaliJ++){
int diff = (acidArr[acidJ] + alkaliArr[alkaliJ]) - 0;
if(abs(diff) < abs(initDiff)){
initDiff = diff;
mapping["acid"] = acidArr[acidJ];
mapping["alkali"] = alkaliArr[alkaliJ];
}
}
}
return mapping;
}
'Algorithm๐ฐ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2252 ์ค์ธ์ฐ๊ธฐ (0) | 2021.02.14 |
---|---|
[๋ฐฑ์ค] 10828 stack (0) | 2021.02.06 |
[๋ฐฑ์ค] 1915 ๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ DP (0) | 2021.02.05 |
[๋ฐฑ์ค] 1987 ์ํ๋ฒณ DFS (0) | 2021.02.05 |
[๋ฐฑ์ค] 2470 ๋์ฉ์ก ์ด๋ถํ์ (0) | 2021.02.05 |
๋๊ธ