본문 바로가기
[프로그래머스 C++]/LEVEL 0

[프로그래머스 C++] 겹치는 선분의 길이

by AKI(JUNI) 2024. 2. 29.

◈ 문제 설명


◈ 문제 설명 링크
코딩테스트 연습 - 겹치는 선분의 길이 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr


◈ 작성 코드

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> lines) 
{
    int answer = 0;
    int arr[200] = {0, };
    
    for(int i = 0; i < lines.size(); i++)
    {
        int a = lines[i][0];
        int b = lines[i][1];
        
        for(int j = a; j < b; j++)
        {
            arr[j+100]++;
        }
    }
    
    for(int i = 0; i < 200; i++)
    {
        if(arr[i] >= 2)
        {
            answer++;
        }
    }
    
    return answer;
}