Post

[Programmers] #120910 - 세균 증식 [Java][C++][Python]

[Programmers] #120910 - 세균 증식 [Java][C++][Python]

문제 링크


1. 아이디어

세균은 1시간에 두 배만큼 증식하므로 처음 세균의 마리수 n과 경과한 시간 t에 대해 t시간 후 세균의 수는 $n \times 2^t$마리다.


2. 복잡도

접근시간공간
풀이$O(1)$$O(1)$

3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public int solution(int n, int t) {
        return n << t;
    }
}

비트 시프트 연산자를 활용해서 간단하게 표현했다.

1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int solution(int n, int t) {
    return n << t;
}

비트 시프트 연산자를 활용해서 간단하게 표현했다.

1
2
def solution(n, t):
    return n * 2**t

This post is licensed under CC BY 4.0 by the author.