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
- CS
- 후니의 쉽게 쓴 시스코 네트워킹
- 문자열
- 시뮬레이션
- dfs
- 구현
- 프로그래머스
- Kotlin
- 동적계획법
- 백준
- 이분탐색
- 수학
- java
- mst
- 완전탐색
- 백트래킹
- Network
- JUnit 5
- 플로이드-와샬
- 세그먼트 트리
- BFS
- 투 포인터
- swea
- 그리디
- 위상정렬
- Effective Java
- 알고리즘
- 스택
- 에라토스테네스의 체
- 유니온 파인드
Archives
반갑습니다!
[프로그래머스] N개의 최소공배수 본문
풀이
Queue
를 이용하여 2개씩 짝지어 최소공배수를 구하고, 다시 큐에 넣는 것을 반복하였다.
코드
#include <string>
#include <vector>
#include <queue>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int solution(vector<int> arr) {
int answer = 0;
queue<int> q;
for (int i : arr) q.push(i);
while (q.size() > 1) {
int a = q.front(); q.pop();
int b = q.front(); q.pop();
int g = gcd(a, b);
q.push(a * b / g);
}
answer = q.front();
return answer;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 방문 길이 (0) | 2020.04.17 |
---|---|
[프로그래머스] 소수 만들기 (0) | 2020.04.17 |
[백준] 1806 부분합 (0) | 2020.04.17 |
[백준] 2003 수들의 합 2 (0) | 2020.04.17 |
[프로그래머스] 숫자 게임 (0) | 2020.04.16 |