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

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

 

해시 알고리즘 사용

 

import java.util.HashMap;
import java.util.Map;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;

		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 0; i < clothes.length; i++) {
			map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
		}
		for(String key : map.keySet()) {
			answer *= map.get(key) + 1;
		}
		
		return answer - 1;
    }
}

 

출처 : https://www.acmicpc.net/problem/1316

 

1316번: 그룹 단어 체커

그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때

www.acmicpc.net

 

map을 이용해 이력이 있는 문자를 저장했다.

#include <iostream>
#include <string>
#include <map>
using namespace std;

int groupCheck(string vString) {
	char chTemp;
	map<char, bool> mGroup;
	for (int i = 0; i < vString.size(); i++) {
		auto it = mGroup.find(vString[i]);
		if (it == mGroup.end()) {
			mGroup[vString[i]] = true;
			chTemp = vString[i];
		}
		else {
			if (chTemp != vString[i]) {
				return 0;
			}
		}
	}
	return 1;
}

int main(void) {
	string sTemp;
	int nCount;
	int nResult = 0;

	cin >> nCount;
	for (int i = 0; i < nCount; i++) {
		cin >> sTemp;
		nResult += groupCheck(sTemp);
	}
	cout << nResult;
    return 0;
}

 

출처 : https://programmers.co.kr/learn/courses/30/lessons/42576?language=cpp 

 

코딩테스트 연습 - 완주하지 못한 선수

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수

programmers.co.kr

 

STL map을 이용해 중복 체크를 사용했다.

#include <string>
#include <vector>
#include <map>
using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer;
    int nCount = 0;
    map<string, int> mParticipant;
    for(int i = 0; i < participant.size(); i++) {
        mParticipant[participant[i]] += 1;
    }
    for(int i = 0; i < completion.size(); i++) {
        mParticipant[completion[i]] -= 1;
    }
    for (auto it = mParticipant.begin(); it != mParticipant.end(); it++) {
        if (it->second > 0) {
            answer = it->first;
        }
    }
    return answer;
}

 

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

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

해시 자료구조 사용

 

 

import java.util.HashMap;
import java.util.Map;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;

		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 0; i < clothes.length; i++) {
			map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
		}
		for(String key : map.keySet()) {
			answer *= map.get(key) + 1;
		}
		
		return answer - 1;
    }
}

 

 

 

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

 

코딩테스트 연습 - 주식가격

초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00

programmers.co.kr

 

스택/큐 문제이다. (LEVEL2)

 

풀이 설명

: prices의 첫 번째 원소부터 배열 끝까지 가면서 가격이 떨어질 때까지 count를 +1 해준다.

 

class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];
        for (int i = 0; i < prices.length - 1; i++) {
			for (int j = i + 1; j < prices.length; j++) {
				answer[i]++;
				if (prices[j] < prices[i]) {
					break;
				}
			}
		}
		return answer;
    }
}

+ Recent posts