Skip to content

Latest commit

 

History

History
123 lines (112 loc) · 3.86 KB

004. 기능개발.md

File metadata and controls

123 lines (112 loc) · 3.86 KB

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

자바로 알고리즘 푸는게 아직은 어려운 것 같다.

배열-리스트 형변환 : https://wakestand.tistory.com/183
큐사용법 : https://coding-factory.tistory.com/602 , https://wakestand.tistory.com/198
향상된 for 문 : https://library1008.tistory.com/66

import java.util.*;

class Solution {
    public Integer[] solution(int[] progresses, int[] speeds) {
        Integer[] answer = {};
        List<Integer> ansList = new ArrayList<>();
        Queue<Integer> prog = new LinkedList<>();
        Queue<Integer> speed = new LinkedList();
        int tempProg = 0;
        int tempSpeed = 0;
        int cnt=0;
        
        for(int i=0; i < progresses.length; i++){//배열의 길이는 length 괄호뺴고
            prog.offer(progresses[i]);
            speed.offer(speeds[i]);
        }
        while(!prog.isEmpty()){
            int sucCnt = 0;
            int progSize = prog.size();
            for(int i=0;i<progSize;i++){
                tempProg = prog.poll();
                tempSpeed = speed.poll();
                tempProg += tempSpeed;
                if(tempProg>=100){
                    if(i==0){
                        sucCnt++;
                    }else if(sucCnt==i){
                        sucCnt++;
                    }else{
                        prog.offer(tempProg);
                        speed.offer(tempSpeed);
                    }
                }else{
                    prog.offer(tempProg);
                    speed.offer(tempSpeed);
                }
            }
            if(sucCnt>0) {//?????????배열에 어떻게 넣지
                ansList.add(sucCnt);
                cnt++;
            }
        }
        answer = ansList.toArray(new Integer[ansList.size()]);
        return answer;
    }
}
//return 다른 방법 
       distributes = new int[index];
        for(int i = 0; i < distributes.length; i++){
            distributes[i] = tempDistributes[i];
        }

        int[] answer = distributes;
        return answer;

2022.02.06
C의 구조체처럼 pair클래스를 생성하여 으로 큐에 적재

import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
class pair{
    int x,y;
    pair(int x,int y){
        this.x = x;
        this.y = y;
    }
}
class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] answer = {};
        ArrayList<Integer> temp = new ArrayList<>();
        Queue<pair> q = new LinkedList<>();//배열바로 못 넣나? 못 넣는다
        for(int i=0;i<progresses.length;i++) {//큐에 input데이터 적재
            q.add(new pair(progresses[i],speeds[i]));
        }
        int cnt = 0; //첫째 작업 끝났을 때 뒷작업의 cnt
        while(!q.isEmpty()){
             boolean suc = false;//첫작업 종결여부 및 뒷작업들의 바로 앞 작업 미종결시 false
             int qSize = q.size();//for문에 바로 넣으면 for문 안에서 size 변경이 일어날 수 있음
             for(int i=0;i<qSize;i++){
                 int x = q.peek().x;
                 int y = q.peek().y;
                 q.remove();
                 x+=y;
                 if(x>=100 && i==0){
                     suc = true;
                     cnt++;
                 }else{
                     if(x>=100 && suc) cnt++;
                     else {
                         suc = false;
                         q.add(new pair(x,y));
                     }
                 }
             }
             if(cnt!=0){//첫작업이 종결되어 cnt++됐다면
                 temp.add(cnt);
                 cnt = 0;
             }
        }
        answer = new int[temp.size()];//ArrayList(tmep) -> answer 배열로 이동
        for(int i=0;i<temp.size();i++) answer[i] = temp.get(i);
        
        return answer;
    }
}