Notice
Recent Posts
Recent Comments
Today
Total
01-26 01:17
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ2] ์Œ์–‘ ๋”ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ2] ์Œ์–‘ ๋”ํ•˜๊ธฐ

WonderJay 2022. 7. 1. 22:12
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://programmers.co.kr/learn/courses/30/lessons/76501

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์Œ์–‘ ๋”ํ•˜๊ธฐ

์–ด๋–ค ์ •์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •์ˆ˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด absolutes์™€ ์ด ์ •์ˆ˜๋“ค์˜ ๋ถ€ํ˜ธ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ๋ถˆ๋ฆฌ์–ธ ๋ฐฐ์—ด signs๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์‹ค์ œ ์ •์ˆ˜๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ re

programmers.co.kr

absolute ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ sign ๋ฐฐ์—ด์„ ํ™•์ธํ•˜๊ณ  true ๋ฉด ๋”ํ•˜๊ณ  false ๋ฉด ๋นผ์„œ answer ์— ์ €์žฅํ•œ๋‹ค.

 

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> absolutes, vector<bool> signs) {
    int answer = 123456789;
    int sum = 0;

    for (int i = 0; i < absolutes.size(); i++)
    {
        if (signs[i] == true) // positive
        {
            sum += absolutes[i];
        }
        else
        {
            sum -= absolutes[i];
        }
    }
    answer = sum;

    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments