에디의 우당탕탕 코딩공장

💖lv0_코딩테스트연습_algorithm헤더를 사용하는 문제 모음.(업데이트중)

by 인턴 에디

<algorithm> | Microsoft Learn

 

<algorithm>

자세한 정보:

learn.microsoft.com

 

 

 

1. 중복된 숫자 개수

코딩테스트 연습 - 중복된 숫자 개수 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

✔️ 사용 함수 : count()

 

⬇️코드보기

더보기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> array, int n) {
    int answer = 0;

    answer = count(array.begin(), array.end(),n);
    return answer;
}

 

 

 

2. 머쓱이보다 키가 큰사람.

코딩테스트 연습 - 머쓱이보다 키 큰 사람 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

✔️ 사용 함수 : count_if()

 

⬇️코드보기

더보기
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> array, int height) {
    int answer = 0;
     
    answer = count_if(array.begin(), array.end(), [=](int num){return height<num;});
    
    return answer;
}

 

 

3. 최빈값 구하기

코딩테스트 연습 - 최빈값 구하기 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

✔️ 사용 함수: max_element()

 

max_element,min_element 

      주소값을 반환하기 떄문에, 값을 구하려면 앞에 *(포인터)를 붙여야한다.

💡 해당 값의 인덱스를 알고 싶다면, max_element의 주소값에서 시작 주소값을 뺴주면 된다.

 int maxC = *max_element(countL.begin(),countL.end());
    
int max_index = max_element(countL.begin(), countL.end()) - countL.begin();

 

⬇️코드보기

더보기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> array) {
    int answer = 0;
    
    vector<int> uniqlist = array;
    
    vector<int> countL;
    
    //정렬
    sort(uniqlist.begin(), uniqlist.end());
    //중복제거
    uniqlist.resize( unique(uniqlist.begin(), uniqlist.end()) - uniqlist.begin() );
    //값의 개수
    for(auto num : uniqlist)
    {
        countL.push_back(count(array.begin(),array.end(),num));
    };

    //최빈값
    int maxC = *max_element(countL.begin(),countL.end());
    
    int max_index = max_element(countL.begin(), countL.end()) - countL.begin();
        
    answer = count(countL.begin(),countL.end(),maxC) > 1 ? -1 : uniqlist[max_index];
    
    return answer;
}

 

 

 

4. 모음제거

코딩테스트 연습 - 모음 제거 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

✔️ 사용 함수 : remove()

✔️ 풀이 방법 : remove로 특정 값을 삭제 한 뒤, string의 erase함수를 사용하여 복사된 쓰레기 값을 지워준다.

⬇️코드보기

더보기
#include <vector>
#include <algorithm>
using namespace std;

string solution(string my_string) {
    string answer = "";
    
    my_string.erase(remove_if(my_string.begin(),my_string.end(),[=](char n){ return n == 'a';}), my_string.end());
    my_string.erase(remove_if(my_string.begin(),my_string.end(),[=](char n){ return n == 'e';}), my_string.end());
    my_string.erase(remove_if(my_string.begin(),my_string.end(),[=](char n){ return n == 'i';}), my_string.end());
    my_string.erase(remove_if(my_string.begin(),my_string.end(),[=](char n){ return n == 'o';}), my_string.end());
    my_string.erase(remove_if(my_string.begin(),my_string.end(),[=](char n){ return n == 'u';}), my_string.end());
    
    answer = my_string;
    return answer;
}

블로그의 정보

에디의 우당탕탕 코딩 공장

인턴 에디

활동하기