본문 바로가기
Coding Test/Programmers

[Programmers] (월간 코드 챌린지 시즌2) Lv 2. 2개 이하로 다른 비트

by song.ift 2023. 7. 21.

https://school.programmers.co.kr/learn/courses/30/le\ssons/77885

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;

vector<long long> solution(vector<long long> numbers) {
    return ::accumulate(numbers.begin(), numbers.end(), vector<long long>(), [&](auto acc, const auto& number) {
        // 공식 (짝수도 동일)
        // 3 = 011 (마지막 1의 개수 2개)
        // 4 = 100
        // 5 = 101 (3 + (2 * *(1의 개수 - 1))) = (3 + (2 * *(2 - 1))) = 5

        long long n = 0, temp = number;
        while (temp % 2)
            ++n, temp /= 2;
        
        if (n == 0) n = 1; // n이 0이면 짝수인 경우인데, 무조건 +1은 해야하니까 n = 1

        acc.emplace_back(number + pow(2, n - 1));
        return acc;
    });
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/64d65b925d7e989b247a5889c8164a9a6bb16210

 

[level 2] Title: 2개 이하로 다른 비트, Time: 5563.33 ms, Memory: 28 MB -Baekj… · developeSHG/Algorithm-Baekjoon_Progr

…oonHub

github.com

 

댓글