#82612
๐ Question ?
https://school.programmers.co.kr/learn/courses/30/lessons/82612
//82612
class Solution {
fun solution(price: Int, money: Int, count: Int): Long {
var answer: Long = -1
return answer
}
}
๐งฉ Thought Process
- use the for loop to find the summation of all the n times of rides. repeat count times!
for ๋ฌธ์ ์ด์ฉํด์ ์ด ๊ฐ๊ฒฉ ์ฐพ๊ธฐ - Then subtract the sum by the money and return that as an answer
์ด ํฉ๊ณ์์ money๋ฅผ ๋นผ์ฃผ๊ณ ๊ทธ ๊ฐ์ answer๋ก ๋ฆฌํดํ๊ธฐ - Right before returning the answer, check the if the answer is negative using the if clause. If so, return -1.
answer ๋ฆฌํด ํ๊ธฐ ์ง์ ์ if ๋ฌธ์ ์ด์ฉํ์ฌ answer์ด ์์์ธ์ง ํ์ธํ๊ณ ์์์ด๋ฉด -1 ๋ฆฌํดํ๊ธฐ
class Solution {
fun solution(price: Int, money: Int, count: Int): Long {
var answer: Long = -1
var sum: Int = 0
for (i in 0 until count) {
sum += price*(i+1)
}
if (sum-money >= 0) {
answer = (sum-money).toLong()
} else {
answer = 0
}
return answer
}
}
์ด๋ ๊ฒ ์ ์๋๋ฐ ํ๋ ธ๋ค๊ณ ํ๋ค...
์ ํ์ฑ 82 ํผ์ผํธ.. ์ด๋ด๋๊ฐ ๊ฐ์ฅ ๋ง๋งํ๋ค.. ์ด์ฉ๋ผ๋๊ฑฐ์ง..:( ๋ชจ๊ฐ ๋ฌธ์ ์ธ๊ฑธ๊น... ์ด๋ค ํ ์คํธ๋ฅผ ํ๋์ง๋ฅผ ์๋ฉด ๊ทธ๊ฑธ ๊ณ ์น๋ฉด ๋๋๋ฐ.. ๊ทธ๊ฒ๋ ์๋๋ผ;;
ํน์๋ ํด์ sum์ ์๋ฃํ์ Long์ผ๋ก ๋ฐ๊พธ์ด์ฃผ์๋ค. sum์ด ๋ชจ๋ ๊ฐ์ ๋ํ๊ฑฐ๋๊น ํจ ์ฌ ํฐ ๊ฐ์ ๊ฐ์ง๊ฑฐ๋๊น!!
ํ๋ณต ์ง๋ฆฟ! ๋ฌด๋ ค 11์ ์ง๋ฆฌ ๋ฌธ์ ์๋ค :))
๐ Answer
class Solution {
fun solution(price: Int, money: Int, count: Int): Long {
var answer: Long = -1
var sum: Long = 0
for (i in 0 until count) {
sum += price*(i+1)
}
if (sum-money >= 0) {
answer = (sum-money).toLong()
} else {
answer = 0
}
return answer
}
}
#2
class Solution {
fun solution(price: Int, money: Int, count: Int): Long {
return Math.max(count * (price.toLong() + price * count) / 2L - money, 0)
}
}
์ถ์ฒ: https://yline.tistory.com/87 [Y_LINE's_Repository:ํฐ์คํ ๋ฆฌ]
๋ค๋ฅธ ํ์ด ๋ถ์: 2L๋ก ๋๋์ด์ค์ผ๋ก์ Longํ์ ์ ์งํด์ฃผ์๋ค. maxํจ์๋ฅผ ๊ตณ์ด ์ฌ์ฉํ์ด์ผํ๋ ์ถ๊ธด ํ๋ค...
๐ Result
๐ Comment
Long ์๋ฃํ์ ์๊ตฌํ๋ ๋ฌธ์ ๋ ์ฒ์ ํ์ด๋ดค๋ค. ๋ง์ด ๋ฐฐ์:)