๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿบ ๋งค์ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ Kit ์Šคํƒ/ํ ๋ฌธ์ œํ’€์ด

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ kit ์Šคํƒ/ํ๋กœ ๋ถ„๋ฅ˜๋˜์–ด ์žˆ๋Š” ํƒ‘, ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ, ๊ธฐ๋Šฅ๊ฐœ๋ฐœ, ํ”„๋ฆฐํ„ฐ, ์‡ ๋ง‰๋Œ€๊ธฐ, ์ฃผ์‹๊ฐ€๊ฒฉ ๋ฌธ์ œํ’€์ด.

 

1.ํƒ‘

function solution(heights) {
    const answer = [0];
    for (let i = 1; i < heights.length; i++) {
        for (let j = i - 1; 0 <= j; j--) {
            if (heights[j] > heights[i]) {
                answer.push(j + 1)
                break
            }
        }
        if (answer.length === i) {
            answer.push(0)
        }
    }
    return answer;
}

 

 

2.๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

function solution(bridgeLength, weight, truckWeights) {
    truckWeights.reverse()
    const onBridge = new Array(bridgeLength).fill(0)
    let clock = 1
    let curr = truckWeights.pop()
    let onWeight = curr
    onBridge[0] = curr
    while (onWeight) {
        onWeight -= onBridge.pop();
        curr = truckWeights.pop()
        if (onWeight + curr <= weight) {
            onBridge.unshift(curr)
            onWeight += curr
        } else {
            onBridge.unshift(0)
            truckWeights.push(curr)
        }
        clock++
    }
    
    return clock
}

 

 

 

3.๊ธฐ๋Šฅ๊ฐœ๋ฐœ

function solution(progresses, speeds) {
    const days = progresses.map((x, i) => Math.ceil((100 - x) / speeds[i]))
    const answer = [1]
    let prev = days[0]
    for (let i = 1; i < days.length; i++) {
        if (days[i] <= prev) {
            answer[answer.length - 1]++
        } else {
            answer.push(1)
            prev = days[i]
        }
    }
    return answer
}

 

 

4.ํ”„๋ฆฐํ„ฐ

function solution(priorities, location) {
    const ordered = [...priorities].sort((a, b) => a < b ? 1 : -1)
    priorities = priorities.map((x, i) => [x, i])
    for (let i = 0; i < ordered.length; i++) {
        while (priorities[0][0] !== ordered[i]) {
            priorities.push(priorities.shift())
        }
        const [_, j] = priorities.shift();
        if (j === location) {
            return i + 1;
        }
    }
}

 

 

5.์‡ ๋ง‰๋Œ€๊ธฐ

function solution(arrangement) {
    let left = 0, answer = 0;
    for (let i = 0; i < arrangement.length; i++) {
        if (arrangement[i] === ')') {
            left--
            answer++
        } else {
            if (arrangement[i+1] === ')') {
                answer += left
                i++
            } else {
                left++
            }
        }
    }
    return answer
}

 

 

6.์ฃผ์‹๊ฐ€๊ฒฉ

def solution(prices):
    answer = []
    for i in range(len(prices) - 1):
        answer.append(0)
        for j in range(i + 1, len(prices)):
            answer[-1] += 1
            if prices[i] > prices[j]:
                break
    answer.append(0)
    return answer