Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-oesnuj #47

Merged
merged 3 commits into from
Sep 5, 2024
Merged

15-oesnuj #47

merged 3 commits into from
Sep 5, 2024

Conversation

oesnuj
Copy link
Member

@oesnuj oesnuj commented Aug 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | ์ฃผ์‹

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ 30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ณธ ๊ฒƒ ๊ฐ™์ง€ ์•Š์•„ ํ•œ๋ฒˆ ํŒŒ๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ์ง‘์—์„œ ์ œ๋ชฉ์ด ์ œ์ผ ๋Œ๋ฆฌ๋Š” ๋ฌธ์ œ๋กœ ๊ณจ๋ผ๋ดค์Šต๋‹ˆ๋‹ค.

์‹œ๊ฐ„์ดˆ๊ณผ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ์— ๋งž์ถ”์ง€๋Š” ๋ชปํ•œ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
๊ทธ๋ƒฅ ๋‹ต์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์—ˆ์ง€๋งŒ ๊ทธ๋ฆฌ๋”” ๋‹ต๊ฒŒ ๋” ํšจ์œจ์ ์œผ๋กœ ์ฐพ๋Š” ๋ฒ•์„ ๋” ์ƒ๊ฐํ•ด๋‚ด์•ผํ–ˆ๋„ค์š”


๐Ÿ˜ข๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ์ƒ๊ฐ์ด ๋“  ํ’€์ด(์‹œ๊ฐ„์ดˆ๊ณผ)

  1. ๋‚  ๋ณ„ ์ฃผ๊ฐ€ ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
  2. ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ์•ž์— ์œ„์น˜ํ•˜๋ฉด์„œ ์ž‘์€ ๋ชจ๋“  ๊ฐ’์„ ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐ ์‹œํ‚จ๋‹ค.
  3. ๋‹ค์‹œ 1๋ฒˆ์œผ๋กœ ๋Œ์•„๊ฐ€ ๋‹ค์Œ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

์œ„ ๋กœ์ง์„ ๋ฐฐ์—ด์ด ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

function calc(i){
    let stockFlow = input[i].trim().split(' ').map(e => parseInt(e));
    let profit = 0;

    while(stockFlow.length > 0){
        const max = Math.max(...stockFlow);
        const maxIndex = stockFlow.indexOf(max);

        for(let j = maxIndex; j >= 0; j--) {
            if (max >= stockFlow[j]) {
                profit += max - stockFlow[j];
                stockFlow.splice(j, 1);
            }
        }
    }
    return profit;
}

์‹œ๊ฐ„ ์ดˆ๊ณผ ์ด์œ ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ƒˆ๋กœ์šด ์ตœ๋Œ“๊ฐ’์„ ๊ณ„์† ์ฐพ์•„๋‚ด๋Š” ๊ณผ์ •๊ณผ splice()๋กœ ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐ์‹œํ‚ค๋Š” ๋กœ์ง์ด ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


๐Ÿ˜ƒ ํ†ต๊ณผํ•œ ํ’€์ด

๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ๋ญ˜๊นŒ ์ด๊ฒƒ์ €๊ฒƒ ๊ณ„์† ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ์š”....
๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐ์‹œํ‚ฌ๋•Œ ๋ฐฐ์—ด์„ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ˆœํšŒํ•ด์•ผ ์ œ๊ฑฐ๋œ ์ธ๋ฑ์Šค๊ฐ€ ๋‹ค์Œ ์ˆœํšŒ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ๋‚˜์˜จ ์ƒ๊ฐ์ด์—ˆ๋Š”๋ฐ ๊ทธ๋ƒฅ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
์ˆœํšŒ ์ค‘์— ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฐ€์žฅ ๋น„์‹ผ ์ฃผ๊ฐ€๊ฐ€ ๋‚จ์•„์žˆ๋Š”๋ฐ์š”.
์ด ๊ฐ€๊ฒฉ๋ณด๋‹ค ๋น„์‹ธ๊ฒŒ ํŒŒ๋Š” ๋‚ ๋ฅผ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ชจ๋‘ ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ฐ€๊ฒฉ์œผ๋กœ ํŒ”์•„์ฃผ๋ฉด ์ตœ๊ณ  ์ต์ ˆ์ด ๋ฉ๋‹ˆ๋‹ค.

์ตœ์ ํ™” ๋กœ์ง:

  • ์ตœ๋Œ€ ์ฃผ๊ฐ€ ์ฐพ๊ธฐ: ๋’ค์—์„œ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€์˜ ์ฃผ๊ฐ€ ์ค‘ ๊ฐ€์žฅ ๋น„์‹ผ ๋‚ ์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
  • ์ด์ต ๊ณ„์‚ฐ: ํ˜„์žฌ ์ฃผ๊ฐ€์™€ ๊ทธ ์ดํ›„์˜ ์ตœ๊ณ  ์ฃผ๊ฐ€ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ์ด์ต์œผ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
const filepath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = require('fs').readFileSync(filepath).toString().trim().split('\n'); //๋ชจ๋“  ์ž…๋ ฅ \n์œผ๋กœ ํŒŒ์‹ฑ

let answer ="";
for(let i = 2; i < input.length; i += 2) { //๋‚  ๋ณ„ ์ฃผ๊ฐ€๊ฐ€ ์ ํ˜€์žˆ๋Š” ์ธ๋ฑ์Šค๋งŒ ์ถ”์ถœ
    answer += calc(i) + "\n";
}
console.log(answer);

function calc(i){
    let stockFlow = input[i].split(' ').map(Number); //๋‚  ๋ณ„ ์ฃผ๊ฐ€ ํ๋ฆ„์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ฆ
    let profit = 0;
    let maxPrice = 0;
    for(let j = stockFlow.length - 1; j >= 0; j--) {
        if (stockFlow[j] > maxPrice) {
            maxPrice = stockFlow[j];
        }
        profit += maxPrice - stockFlow[j];
    }
    return profit;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ด๋Ÿฐ ์œ ํ˜•์—์„œ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ์‹์„ ์ด์šฉํ•˜๋‹ˆ๊นŒ ์—„์ฒญ ๊ฐ„๋‹จํ•˜๊ฒŒ ์งœ์ง€๋„ค์š”.
๊ทธ๋™์•ˆ ํ•œ๋ฒˆ๋„ ์ƒ๊ฐ ๋ชปํ•ด๋ณธ ์•„์ด๋””์–ด์˜€๋Š”๋ฐ ํ•˜๋‚˜ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค.

ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋Œ“๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Š๋‚Œ์—์„œ ๋ชจ๋…ธํ†ค ์Šคํƒ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์•„์š”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants