01-08 08:57
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Programmers] Java ๋ถ€์กฑํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm/Programmers

[Programmers] Java ๋ถ€์กฑํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ

miinsun 2021. 12. 6. 16:03

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

์ƒˆ๋กœ ์ƒ๊ธด ๋†€์ด๊ธฐ๊ตฌ๋Š” ์ธ๊ธฐ๊ฐ€ ๋งค์šฐ ๋งŽ์•„ ์ค„์ด ๋Š์ด์งˆ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋†€์ด๊ธฐ๊ตฌ์˜ ์›๋ž˜ ์ด์šฉ๋ฃŒ๋Š” price์› ์ธ๋ฐ, ๋†€์ด๊ธฐ๊ตฌ๋ฅผ N ๋ฒˆ ์งธ ์ด์šฉํ•œ๋‹ค๋ฉด ์›๋ž˜ ์ด์šฉ๋ฃŒ์˜ N๋ฐฐ๋ฅผ ๋ฐ›๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ฆ‰, ์ฒ˜์Œ ์ด์šฉ๋ฃŒ๊ฐ€ 100์ด์—ˆ๋‹ค๋ฉด 2๋ฒˆ์งธ์—๋Š” 200, 3๋ฒˆ์งธ์—๋Š” 300์œผ๋กœ ์š”๊ธˆ์ด ์ธ์ƒ๋ฉ๋‹ˆ๋‹ค.

๋†€์ด๊ธฐ๊ตฌ๋ฅผ count๋ฒˆ ํƒ€๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์—์„œ ์–ผ๋งˆ๊ฐ€ ๋ชจ์ž๋ผ๋Š”์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ๋‹จ, ๊ธˆ์•ก์ด ๋ถ€์กฑํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ return ํ•˜์„ธ์š”.

 

โ€‹

๐Ÿšซ ์ œํ•œ ์‚ฌํ•ญ

  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ๋ฃŒ price : 1 ≤ price ≤ 2,500, price๋Š” ์ž์—ฐ์ˆ˜
  • ์ฒ˜์Œ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ธˆ์•ก money : 1 ≤ money ≤ 1,000,000,000, money๋Š” ์ž์—ฐ์ˆ˜
  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ ํšŸ์ˆ˜ count : 1 ≤ count ≤ 2,500, count๋Š” ์ž์—ฐ์ˆ˜

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

pric
money
count
result
3
20
4
10
  • ์ด์šฉ๊ธˆ์•ก์ด 3์ธ ๋†€์ด๊ธฐ๊ตฌ๋ฅผ 4๋ฒˆ ํƒ€๊ณ  ์‹ถ์€ ๊ณ ๊ฐ์ด ํ˜„์žฌ ๊ฐ€์ง„ ๊ธˆ์•ก์ด 20์ด๋ผ๋ฉด
  • ์ด ํ•„์š”ํ•œ ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ ๊ธˆ์•ก์€ 30 (= 3+6+9+12) ์ด ๋˜์–ด 10๋งŒํผ ๋ถ€์กฑํ•˜๋ฏ€๋กœ 10์„ return ํ•ฉ๋‹ˆ๋‹ค.

โ€‹

โ€‹

๐Ÿ’ป Solution.java

class Solution {
    public long solution(int price, int money, int count) {
        long answer = 0;
        long total = 0;
        
        for(int i = 1; i <= count; i++){
            total += price * i;
        }
    
        if(total > money)
            return answer = total - money;
        else
            return answer;
    }
    
}
Comments