내일배움캠프 47일차 TIL _ 10주 4일차

2024. 6. 20. 22:50TIL Java

 

  • 오늘 있었던 일
    • 팀과제
    • 알고리즘

SPRIONG

  • 스프링 정리

알고리즘 문제 풀기

 

     ● 프로세스

더보기

● 프로세스//링크

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        boolean a = false;

         List<Integer> in = new ArrayList<>(List.of(Arrays.stream(priorities).boxed().toArray(Integer[]::new)));
         in.sort(Collections.reverseOrder());

         for (int i = 0; i < priorities.length; i++) {
             for (int j = 0; j < priorities.length; j++) {
                 if( in.get(answer) == priorities[j]) {
                     answer++;
                     if(j == location){
                         a =true;
                         break;
                     }
                 }
             }
             if(a){
                 break;
             }

         }
        return answer;
    }
}
  • 태스트문은 통과했었지만 이후 시간초과가 일어났었다.
  • 문제 지문에
    • 프로세스 4개 [A, B, C, D]가 순서대로 실행 대기 큐에 들어있고, 우선순위가 [2, 1, 3, 2]라면 [C, D, A, B] 순으로 실행하게 됩니다.

    • Queue를 써서 문제에 나온대로 했더니 시간초과가 났다..
    • list를 이용해 정렬하고 그 값대로 체크하게 만들어서 통과했다
  • 밑은 다른 사람의 풀이
  • class Solution {
        public int solution(int[] priorities, int location) {
            int answer = 0;
            int l = location;
    
            Queue<Integer> que = new LinkedList<Integer>();
            for(int i : priorities){
                que.add(i);
            }
    
            Arrays.sort(priorities);
            int size = priorities.length-1;
    
    
    
            while(!que.isEmpty()){
                Integer i = que.poll();
                if(i == priorities[size - answer]){
                    answer++;
                    l--;
                    if(l <0)
                        break;
                }else{
                    que.add(i);
                    l--;
                    if(l<0)
                        l=que.size()-1;
                }
            }
    
            return answer;
        }
    }
  • 내가 했을땐 안됐는데 어째서...

 


당일 회고

  • 소셜 로그인 기능 구현이 어렵다