๐ฅ ์ฃผ์ด์ง๋ ๊ฒ
1) ์ผ์ ์ค์๋๊ฐ ํ์๋ ๋ฐฐ์ด
2) ์ถ๋ ฅ ์์๊ฐ ๊ถ๊ธํ ์์์ ์ธ๋ฑ์ค ๋๋ฒ(๋ก์ผ์ด์ )
๐ฅ ์๊ตฌํ๋ ๊ฒ
๋ฐฐ์ด์ ์์๋๋ก ์ถ๋ ฅํ๋, ์์์ ์ค์๋๊ฐ ๋ค์ ์๋ ๊ฒ ๋ณด๋ค ๋ฎ์ผ๋ฉด ๋งจ ๋ค๋ก ๋๊ธธ ๊ฒ(ํ๋ฒ์ ์ ๋ ฌX)
[2, 1, 3, 2] ๋ก ๋ฐฐ์ด์ด ์ฃผ์ด์ง๊ณ ๋ก์ผ์ด์ ์ด 2์ธ(์ค์๋ 3์ธ ์์) ๊ฒฝ์ฐ,
1, 3, 2, 2 (๋ต ++!)
3, 2, 2, 1 (๋ต ++1)
๋ก ๊ฒฐ๊ณผ๋ 2
์ด๋ฐ์์ผ๋ก ๋์ค๊ฒ ํด์ผํจ.
๐ฅ ์ด๋ ค์ ๋ ๊ฒ
1. ๋ฌธ์ ๊ฐ๋ ์ฑ์ด ์๋ฐ
2. ๋ฐ๋ณต๋ฌธ์ ์ด๋ป๊ฒ ์ ์ด ํด์ผํ๋๊ฐ,
----------
1. ์๊ธฐ ์๋ฆฌ์ ์ธ๋ฑ์ค ๋ฒํธ์ ์ค์๋๋ฅผ ๋ฌถ์ด์ฃผ๊ธฐ ์ํด map ์ผ๋ก ๋ฐฐ์ด์ ๋ณํ ์์ผ์ค
์ด๋ ๊ฒ ๋ฉ๋๋ค.
function solution1(priorities, location) {
var answer = 0;
let documents = priorities.map((priority, index) => ({location: index, priority: priority}))
console.log(priorities)
console.log(documents)
let locationPrinted = false
while (!locationPrinted) {
const shifted = documents.shift()
let printAvailable = true
if (documents.some((document) => shifted.priority < document.priority)) printAvailable = false
if (printAvailable) {
answer += 1
if (shifted.location === location) locationPrinted = true
} else {
documents.push(shifted)
}
}
return answer;
๋ด๊ฐ ์ํ๋ ์ธ๋ฑ์ค ๋ฒํธ๊ฐ ํ์ฌ ์ฌํํธ ๋ ์์์ ์ธ๋ฑ์ค ๋ฒํธ์ ์ผ์นํ๋์ง ํ์ธํ๊ณ ์ผ์นํ๋ฉด ํ๋ฆฐํธ ํจ,
๐ฅ ๊ทผ๋ฐ ์ด๋ ๊ฒ ํ๋ฉด ๋ฐํ์ ์๋ฌ๋จ