DevLog:-)

[프로그래머스][Javascript]기능개발 본문

알고리즘/프로그래머스

[프로그래머스][Javascript]기능개발

hyeon200 2023. 7. 29. 19:42
반응형

문제

기능개발

 

프로그래머스

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

programmers.co.kr

 

 

 

코드

function solution(progresses, speeds) {
    var answer = [];
    let list =[];
    let k;
    
    for(let i in progresses){
        
        k = ((100-progresses[i])/speeds[i]);
        if(parseInt(k)!=k){k = parseInt(k)+1;}
        list.push(k);
    }
let min = list[0];
    let sum =0;
    for(let i of list){
        if(min<i){answer.push(sum);sum =0;min= i;}
        sum++;
    }
    answer.push(sum);
    return answer;
}

 

📖다른 풀이

function solution(progresses, speeds) {
    let answer = [0];
    let days = progresses.map((progress, index) => Math.ceil((100 - progress) / speeds[index]));
    let maxDay = days[0];

    for(let i = 0, j = 0; i< days.length; i++){
        if(days[i] <= maxDay) {
            answer[j] += 1;
        } else {
            maxDay = days[i];
            answer[++j] = 1;
        }
    }

    return answer;
}

배열의 map함수와 Math.ceil함수를 이용해 더 간결하게 코드를 작성함

 

✅checkpotin!

arr.map((element,index,arr)=>element+1)  콜백 함수를 이용해 각각의 요소에 호출해서 그 값을 변환
Math.ceil() 올림
ex)
Math.ceil(0.95) //1
Math.ceil(7.04) //8
Math.ceil(-0.05) //0
Math.floor() 내림
ex)
Math.floor(1.22)//1
Math.floor(-1.5)//-2
Math.round() 반올림 (0.5기준)
ex) 
Math.round(1.22)//1
Math.round(1.6)//2
Math.round(-1.7)//-2
Math.round(-1.5)//-1
Math.round(-1.1)//-1
[배열에서 특정 값 삭제]
arr.filter(조건 함수) ->조건함수에서 true로 나오는 element만 반환
arr= ['a','b','b','c']
new_arr = arr.filter((element)=> element !== 'b');
//new_arr = ['a','c'];

 

반응형