본문 바로가기

반응형

1. 기능 개발 
https://school.programmers.co.kr/learn/courses/30/lessons/42586

 

프로그래머스

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

programmers.co.kr

import java.util.*;

class Solution {
    public List<Integer> solution(int[] progresses, int[] speeds) {
        int[] answer = new int[progresses.length];
        
        for(int i = 0; i < progresses.length; i++){
            if((100-progresses[i])%speeds[i] > 0){
                answer[i] = (100-progresses[i])/speeds[i] + 1;
            } else {
                answer[i] = (100-progresses[i])/speeds[i];
            }
        }
        
        List<Integer> countMap = new ArrayList<>();
        
        int count = 0;
        int max = 0;
        for(int i = 0; i < answer.length; i++){
            if(i == 0){
                count++;
                max = answer[i];
            } else if(max < answer[i]){
                countMap.add(count);
                count = 1;
                max = answer[i];
            } else {
                count++;
            }
        }
        countMap.add(count);
        
        return countMap;
    }
}

각 progress당 작업 기간이 어떻게 되는지 계산하여 int 배열 answer에 대입시키고, max 값을 이용해서 ArrayList 변수에 count를 추가시켜주고 반환해주는 코드를 짰습니다.
원래는 인덱스와 max 값을 HashMap 변수에 추가시켜주려고 했는데 그런 복잡한 과정은 비효율적이라고 생각이 들어서 이와 같이 간소화 시키게 됐습니다.

2. [PCCE 기출문제] 6번 / 가채점
https://school.programmers.co.kr/learn/courses/30/lessons/250128

 

프로그래머스

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

programmers.co.kr

class Solution {
    public String[] solution(int[] numbers, int[] our_score, int[] score_list) {
        int num_student = numbers.length;
        String[] answer = new String[num_student];

        for (int i = 0; i < num_student; i++) {
            if (our_score[i] == score_list[numbers[i]-1]) {
// 원래 코드 if (our_score[i] == score_list[i])
                answer[i] = "Same";
            }
            else {
                answer[i] = "Different";
            }
        }

        return answer;
    }
}

i는 our_score와 numbers 기준이기 때문에 score_list의 인덱스에 가공하지 않고 들어가면 안 됩니다. 그래서 numbers를 이용해서 score_list의 인덱스에 값을 넣어줬습니다.

3. 프로세스
https://school.programmers.co.kr/learn/courses/30/lessons/42587

 

프로그래머스

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

programmers.co.kr

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        int len = priorities.length;
        Queue<Integer> queue = new LinkedList<>();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());

        for (int i = 0; i < len; i++) {
            queue.offer(i);
            priorityQueue.offer(priorities[i]);
        }

        while (!queue.isEmpty()) {
            int currentProcess = queue.poll();
            int currentPriority = priorities[currentProcess];

            if (currentPriority == priorityQueue.peek()) {
                priorityQueue.poll();
                answer++;

                if (currentProcess == location) {
                    break;
                }
            } else {
                queue.offer(currentProcess);
            }
        }

        return answer;
    }
}

이번에는 Queue에 대해서 알기 전까지 고생한 문제였습니다.
스택과 동일하게 자료를 쌓지만 선입선출 하는 성질을 가진 Queue 변수에 인덱스를, 우선순위를 기준으로 정렬하는 PriorityQueue변수에는 우선순위를 넣어서 중간 우선순위가없는데 9~1까지 모두 찾아보는 일이 없도록 했습니다.
우선순위를 비교해서 아니라면 다시 마지막 차례로 보내버리는 방법으로 문제를 풀 수 있게 됐습니다. 

- Queue
Stack처럼 들어오는 순서대로 저장되지만 Stack과는 다르게 빠져나갈 땐 들어왔던 순서대로 빠져나가는 변수로써 주로 사용되는 두 가지 구현 방법에는 LinkedList와 PriorityQueue가 있습니다.

- LinkedList
노드들이 서로 연결된 구조를 가지며, 데이터의 추가 및 삭제가 양 끝에서 O(1)의 시간 복잡도로 이루어집니다.
(ex)
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
int frontElement = queue.poll();

- PriorityQueue
우선순위를 고려하여 데이터를 저장하는 큐입니다. 이 구현은 이진 힙(binary heap)을 사용하며, 우선순위가 높은 원소가 먼저 나가게 됩니다.
(ex)
Queue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.offer(1);
int highestPriorityElement = priorityQueue.poll();

점점 더 많은 변수들을 알게 되어 가고 있습니다. 좀 더 제 코딩 세계가 다채로워지겠죠? 현재 저는 백엔드 개발을 할 때 Map(HashMap)과 List(ArrayList)만 쓰고도 충분했지만 혹시 모를 나중을 위해 이렇게 알아두는 것도 나쁘지 않다고 생각됩니다. 아마 복잡한 계산을 해야 할 때 쓰겠죠?

반응형
댓글