Algorithm/프로그래머스
프로그래머스 N개의 최소공배수
CtrlZ
2022. 10. 14. 14:32
반응형
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 문제
무든 arr에 대한 최소공배수를 출력하는 문제다.
- 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> arr) {
int answer = *max_element(arr.begin(),arr.end());
bool b = false;
while(!b){
b = true;
for(auto a : arr){
if(answer % a != 0){
b = false;
break;
}
}
answer++;
}
answer--;
return answer;
}
거의 O(n^2)의 느낌으로 풀었다.
- 기억할 것!
X
반응형