https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=java 

 

프로그래머스

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

programmers.co.kr


HashSet을 이용하여 효과적으로 카운트할 수 있다. 하지만 중복이 나오는 경우도 있기에 처리해주어야 한다.

집합을 보고 단어가 있는지, 없는지 확인하고

있다면 집합에서 빼주고, 없다면 추가하였다.

다행히 딱 한 명만 완주하지 못하였기 때문에 간단하게 구할 수 있었다.

 

import java.util.HashSet;
class Solution {
    public String solution(String[] participant, String[] completion) {
        HashSet<String> aa = new HashSet<>();
        for(int i=0;i<participant.length;i++){
            if(aa.contains(participant[i]))
                aa.remove(participant[i]);
            else aa.add(participant[i]);
        }
        
        for(int i=0;i<completion.length;i++){
            if(aa.contains(completion[i]))
                aa.remove(completion[i]);
            else aa.add(completion[i]);
        }
        String s="";
        for(String k : aa)
            s=k;
        return s;
    }
}

https://school.programmers.co.kr/learn/courses/30/lessons/59040

 

프로그래머스

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

programmers.co.kr


묶어서 count를 하는 방법은 group by를 이용한다,

고양이와 개를 조회해야한다. 원래라면 다른 동물이 있을 수도 있기 때문에, 확인해야하지만 둘만 있을 것이라 생각하고 진행했다.

개인적으로 좋지 않은 답이라 생각한다.

 

SELECT animal_type, count(animal_type) from animal_ins 
group by animal_type order by animal_type;

https://school.programmers.co.kr/learn/courses/30/lessons/42862

 

프로그래머스

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

programmers.co.kr


앞 뒤 값을 쳐다보며 체육복을 가져올 수 있으면 가져오는 방법을 택했다.

- Java

import java.util.Arrays;
class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        int[] cnt = new int[n];
        Arrays.fill(cnt,1);
        for(int i=0;i<lost.length;i++)
            cnt[lost[i]-1]--;
        for(int i=0;i<reserve.length;i++)
            cnt[reserve[i]-1]++;
        
        
        if(cnt[0]==0)
            if(cnt[1]==2){
                cnt[0]++;cnt[1]--;
            }
        for(int i=1;i<n-1;i++){
            if(cnt[i]==0){
                if(cnt[i-1]==2){
                    cnt[i]++;cnt[i-1]--;
                }
                else if(cnt[i+1]==2){
                    cnt[i]++;cnt[i+1]--;
                }
            }
        }
        
        if(cnt[n-1]==0)
            if(cnt[n-2]==2){
                cnt[n-1]++;cnt[n-2]--;
            }
        
        int answer = n;
        for(int i=0;i<n;i++)
            if(cnt[i]==0)
                answer--;
        return answer;
    }
}

 

- Ruby

def solution(n, lost, reserve)
    cnt=Array.new(n,1)
    lost.each{|l|
        cnt[l-1]=cnt[l-1]-1
    }
    reserve.each{|r|
        cnt[r-1]=cnt[r-1]+1
    }
    if cnt[0]==0
        if cnt[1]==2
            cnt[0]=1
            cnt[1]=1
        end
    end
    
    for i in 1..n-1
        if cnt[i]==0
            if cnt[i-1]==2
                cnt[i]=1
                cnt[i-1]=1
            elsif cnt[i+1]==2
                cnt[i]=1
                cnt[i+1]=1
            end
        end
    end
    
    if cnt[n-1]==0
        if cnt[n-2]==2
            cnt[n-1]=1
            cnt[n-2]=1
        end
    end
    
    return n-cnt.count(0)
end

+ Recent posts