-
Notifications
You must be signed in to change notification settings - Fork 0
5-alstjr7437 #27
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
base: main
Are you sure you want to change the base?
5-alstjr7437 #27
Conversation
giljihun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ ๋ฌธ์ ๋ ์ฝ๋ฉํธ์์ ๋ง์ ์๊ธฐ๋ฅผ ํ๊ฑฐ๊ฐ๋ค์ !!
์ ๊ฐ ์๊ฐํ๋ ๋ฌธ์ ์ ํต์ฌ์ "๋ ๋ฒจ์ ๋ฌด์กฐ๊ฑด diff์ ๋ฒ์ ์์ ์์" ์ ์๊ฐํด๋ด๊ณ ,
์ด๋ถํ์์ ๋ค์ด๊ฐ๋๊ฑฐ์๋ค๊ณ ์๊ฐ์ด ๋๋ค์!
/*
ํ์ฌ ํผ์ฆ ๋์ด๋ - diff
ํ์ฌ ํผ์ฆ ์์์๊ฐ - time_cur
์ด์ ํผ์ฆ ์์์๊ฐ - time_prev
์๋ จ๋ - level
- diff <= level : ํผ์ฆ ํ๋ฆฌ์ง ์๊ณ , time_curr๋งํผ ์๊ฐ ์ฌ์ฉ
- diff > level : (diff - level)๋ฒ ํผ์ฆ์ ํ๋ฆผ,
์ด ๊ฒฝ์ฐ, ((diff - level) * time_curr)๋งํผ ์๊ฐ ์ฌ์ฉํ๊ณ
time_prev๋งํผ์ ์๊ฐ์ ์ฌ์ฉํด์ ์ด์ ํผ์ฆ์ ๋ค์ ํ๊ณ ์์ผํจ. (์ด ๊ฒฝ์ฐ ๋ฌด์กฐ๊ฑด ํ๋ฆฌ์ง ์์.)
- ์ ๊ณผ์ ์ดํ, ๋ฌด์กฐ๊ฑด time_cur๋งํผ ์๊ฐ์ ์ฌ์ฉํด์ ํด๊ฒฐํจ.
==> ((time_cur + time_prev) * (diff - level)) + time_cur
์ ์ฒด ์ ํ ์๊ฐ limit.
์ด๋ค level์ด ํผ์ฆ์ ๋ชจ๋ ํด๊ฒฐํ๋ ์ต์ ๋ ๋ฒจ์ผ๊น?
1. ๋ ๋ฒจ์ ๋ฌด์กฐ๊ฑด diff์ ๋ฒ์ ์์ ์์. (์๋ํ๋ฉด max๋์ด๋๋ณด๋ค ํฌ๋ฉด ๋ค ok์.)
2. ํด๋น ๋ฒ์ ์์์์ ์ต์๊ฐ์ ์ฐพ์์ผํจ.
*/
import Foundation
func solution(_ diffs:[Int], _ times:[Int], _ limit:Int64) -> Int {
// ==> ((time_cur + time_prev) * (diff - level)) + time_cur
func isPossible(level: Int) -> Bool {
var totalTime: Int64 = 0
for i in 0..<diffs.count {
let diff = diffs[i]
let timeCur = times[i]
let timePrev: Int
// ์ฒซ ํผ์ฆ์ ์ด์ ํผ์ฆ์ด ์์์ฌ -> 0
if i == 0 {
timePrev = 0
} else {
timePrev = times[i - 1]
}
if diff <= level {
totalTime += Int64(timeCur)
} else {
let failCount = diff - level
totalTime += Int64((timeCur + timePrev) * failCount + timeCur)
}
if totalTime > limit {
return false
}
}
return true
}
var left = 1
// ์ด ๊ฒฝ์ฐ ์ต์
๋ ๊ฐ์ ์ธ๋ํ์ ํ๋ฉด ์๋๋ ๊ฒฝ์ฐ๊ฐ ๋ญ์ง?
// var right = diffs.max()!
// ์ผ๋จ ๊ฐ๋๋ฌธ ์ฒ๋ฆฌ
guard let maxDiff = diffs.max() else { exit(0) }
var right = maxDiff
var answer = right
while left <= right {
let mid = (left + right) / 2
if isPossible(level: mid) {
answer = mid
right = mid - 1
} else {
left = mid + 1
}
}
return answer
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋ฃจ์
๋ถํฐ ์ฝ์ด์ ์ดํด๊ฐ ๋๊ธดํฉ๋๋ค!
๊ทธ๋๋ puzzle์ด๋ผ๋ ํจ์๋ช
์ ์กฐ๊ธ ๊ฐ๋
์ฑ์ ๋ฎ์ถ ๊ฒ ๊ฐ์ต๋๋ค!
์.. calTotalTime() ์ด๋ฐ๊ฑด ์ด๋จ๊น์~?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ์ค ํจ์๊ฐ ๋จ์ผ ์ฑ
์ ์์น์ ๋ง์ถ๊ธฐ ์ํด์ ์์ฑํ๋๋ฐ
์๊ฐํด๋ณด๋ puzzle๊ฒ์์ ํ๋ ์ ๋ ์ดํด๋ฅผ ํ์ง๋ง ๋ค๋ฅธ ์ฌ๋๋ค์ ์ดํด๋ฅผ ๋ชปํ๊ฒ ๋ค์!
๊ทธ๋ ๊ฒ ๋๋ฉด ์ฃผ์์ ์ถ๊ฐํ๋๊ฒ ๋ญ๊ฐ ๋ ๊ฐ๋ ์ฑ์ด ์ข์ ๊ฒ ๊ฐ์์!
calTotalTime์ ํด๋ ๋ญ๊ฐ ๋ชจ๋ฅด๋ ์ฌ๋์ด ๋ณด๋ฉด ๋ฌด์จ ์ด ์๊ฐ์ ๊ตฌํ๋๊ฑฐ์ง? ๋ผ๋ ์๊ฐ์ด ๋ค์ด์
puzzle๊ฒ์์ธ๋ฐ ์ค๋ช ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ด ๋จ์ผ ์ฑ ์ ์์น + ๊ฐ๋ ์ฑ์ ์งํค๋ ๊ฒ ๊ฐ๋ค์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ฐ์ ๋๊ธฐ๊ณ solution()์์ result <= limit ๊ฒ์ฌ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์
จ๊ตฐ์!
์ ๋ ๊ฐ๋ฅ์ฑ ์ฌ๋ถ๋ฅผ ํจ์์์ ํ๋จํ๊ณ ๋๊ธฐ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ด์!
// ==> ((time_cur + time_prev) * (diff - level)) + time_cur
func isPossible(level: Int) -> Bool {
var totalTime: Int64 = 0
for i in 0..<diffs.count {
let diff = diffs[i]
let timeCur = times[i]
let timePrev: Int
// ์ฒซ ํผ์ฆ์ ์ด์ ํผ์ฆ์ด ์์์ฌ -> 0
if i == 0 {
timePrev = 0
} else {
timePrev = times[i - 1]
}
if diff <= level {
totalTime += Int64(timeCur)
} else {
let failCount = diff - level
totalTime += Int64((timeCur + timePrev) * failCount + timeCur)
}
if totalTime > limit {
return false
}
}
return true
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ์ค ์ฝํ
๋ฌธ์ ํ๋๋ผ ์๊ด ์๊ธดํ๋ฐ
๊ฒ์ ํํ ๊ฒฐ๊ณผ + ํต๊ณผ ๊ฐ๋ฅํ๋ vs ๊ฒ์ ํํ ๊ฒฐ๊ณผ
์ด๋ ๊ฒ ํจ์๋ฅผ ์ด๋ป๊ฒ ์๋ฅด๋์ง์ ๋ฐ๋ผ ํจ์์ ์ฌ์ฉ์ฑ์ด ๋ฌ๋ผ์ ธ์ ๊ทธ๋ ๊ฒ ๋ ๊ฒ ๊ฐ๋ค์!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ limit๋ฅผ ๋์์๋ ํ๋จํ๋๊ฑฐ๋ก ์ฒ๋ฆฌ๋ฅผ ํด์คฌ๋๋ฐ, ๋งฅ๋ฝ์ ๋น์ทํ ๊ฒ ๊ฐ์์.
ํจ์๋ฅผ ์ด๋๊น์ง ์๋ผ์ผํ ์ง ๋ช
ํํ ๊ธฐ์ค์ด ์์ด ํญ์ ๋ค๋ฅธ๊ฒ๊ฐ์๋ฐ, ์ด๊ฑฐ๋ ์ทจํฅ์ฐจ์ด์ผ๊น์? ์ ๋ ๋๋ฌด ๋ด์์ฃผ๋ฉด ์คํ๋ ค ๋ณธ์ฝ๋๋ฅผ ๋ค ์ฎ๊ธด๋๋์ด๋ผ ์ต๋๊ธธ์ด๋ฅผ ์ค์ด๋ ๋ฐฉ์์ผ๋ก ๋ถํ ํ๋ค๋ ๋ง์ธ๋๋ก ์ ๊ทผํ๋๋ฐ, ๋ค๋ฅธ๋ถ๋ค์ ์ด๋ค ์๊ฐ์ผ๋ก ๋ถ๋ฆฌํ์๋ ์ง ๊ถ๊ธํ๋ค์
while left <= right {
let mid = (left + right) / 2
totalSpendTime = 0
for index in 0 ..< diffs.count {
var prevTime = 0
if index > 0 {
prevTime = times[index - 1]
}
let tmp = caculateTime(mid, diffs[index], times[index], prevTime)
totalSpendTime += tmp
}
if totalSpendTime > limit {
left = mid + 1
}
else {
level = mid
right = mid - 1
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์น ์ด๋ ๊ฒ ๋ ๋ณํฉ ์ฐ์ฐ์๋ก max()๋ฅผ ์ฒ๋ฆฌํ์ จ๊ตฐ์.
์ ๋ ์๋์ฒ๋ผ ์งฐ๋๋ฐ์
// ์ด ๊ฒฝ์ฐ ์ต์
๋ ๊ฐ์ ์ธ๋ํ์ ํ๋ฉด ์๋๋ ๊ฒฝ์ฐ๊ฐ ๋ญ์ง?
// var right = diffs.max()!
// ์ผ๋จ ๊ฐ๋๋ฌธ ์ฒ๋ฆฌ
guard let maxDiff = diffs.max() else { exit(0) }์ right๋ก ํ๋๋ฐ(ํจ๋ ์ฝ๋์์ end๋ค์ฉ)
์๊ฐํด๋ณด๋ ๊ฐ์ ์ธ๋ํ์ ํ๋ฉด ์๋๋ ๊ฒฝ์ฐ๊ฐ ๋ญ๊ฐ ์๋~ ์๊ฐ์ ํ๊ฒ๋์์ต๋๋ค.
์ผ๋จ ๊ฐ๋๋ฌธ ์ฒ๋ฆฌํ๊ณ ๋์ค์๋ณด๋ ๋น ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ๋ง ์๋๋๊ฑฐ๊ฐ๋๋ผ๊ตฌ์.
์ฌ์ค ๋ฌธ์ ์์ ๋น ๋ฐฐ์ด์ด ์ฃผ์ด์ง์ง ์๋๊ฑฐ๊ฐ์์ ์ค์ํ์ง ์์๊ฑฐ๊ฐ์ต๋๋ค.ใ ใ ใ ใ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ ๋๋ผ๋๋ฐ ์ ๊ฐ ๋ฐฉ์ด์ ์ผ๋ก ์ฝ๋์ง๋ ์ต๊ด์ด ์กฐ๊ธ ์๊ธฐ์ง ์์๋ ์ถ์ด์!
๊ทผ๋ฐ ๋นํจ์จ์ ๊ฐ์ ธ์ค๊ธฐ๋ ํ๋๊ฑฐ๊ฐ์์ ๋ ์กฐ์ฌํ ์ฌ์ฉํด์ผ๊ฒ ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์น ์ด๋ ๊ฒ guard๋ฌธ์ ํ๋ ์์ธ๋ฅผ ๋ ํ์คํ ์ก๋ ๋๋์ด๋ผ ์ข๋ค์!!
์ด ๋ฌธ์ ์์ ๊ทธ๋ฐ ์์ ๋ ์์ง๋ง
๊ฐ์๊ธฐ ์ด๊ณผ๋๋ ๊ฒฝ์ฐ 0์ ์ถ๋ ฅํด๋ผ
์ด๋ ๊ฒ ๋๋ฉด Gil ์ฒ๋ผ guard๋ฌธ์ผ๋ก ํ์ถํด์ print(0)์ ์ฐ์ผ๋ฉด ๋์ด๋ผ ๋์ฑ ์ข์ ๊ฒ ๊ฐ์์!
์ ์ฌ์ค max()๊ฐ ์ต์
๋๋ก ์ค๋์ง ๋ชฐ๋ผ์ ๊ธํ๊ฒ 10000์ ๋ฃ์์ด์..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ธฐ๊ฐ๋งํ๋ค์.. ์ ๋ ์ธ์ ์ฏค guard๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ๋ฃ์ ์ ์์๊น์..
guard์ฐ๋ฉด ๋ช
์์ ์ผ๋ก ์์ธ๊ฐ ๋๊ปด์ ธ์ ๊ฐ๋
์ฑ์ด ์ข์ ๊ฒ ๊ฐ์์ !
@giljihun +if๋ฌธ ๊ถ๊ธ์ฆ let timePrev: Int
// ์ฒซ ํผ์ฆ์ ์ด์ ํผ์ฆ์ด ์์์ฌ -> 0
if i == 0 {
timePrev = 0
} else {
timePrev = times[i - 1]
} let timePrev = (i == 0) ? 0 : times[i - 1]์ด๋ ๊ฒ if i == 0 ๊ณผ ๊ฐ์ ๋ถ๋ถ์ ์์ ๊ฐ์ด ๋ง๋ค ์๋ ์์ ๊ฒ ๊ฐ์์! |
|
@alstjr7437 |
bishoe01
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์ ๊ฐ ๋์ ์ ์๋ค์ด์์ด์! ์ค๋ช ์ด ์ฅํฉํ๋๊น ํท๊ฐ๋ฆฌ๋ค์.
Level์ด ์๋ จ๋๋ผ๊ณ ํํ๋์ด์์ด์ ํผ๋..
๊ฒฐ๊ตญ์ ๊ทธ๋ฅ ์ ์ ํ Level๊ฐ ์ฐพ๋๊ฑฐ์๋ค์!
๊ทธ๋ผ์ด์ ๊ทธ๋ฌ๋ฉด ์์์ ์ ์ด๋ป๊ฒ ์ก์ ๊ฒ์ด๋...! ์๋๋ฐ,
left๋ฅผ ์ต์๋ ๋ฒจ๋ก , right๋ฅผ ์ต๋๋ ๋ฒจ๋ก ์ก๊ณ ์ด๋ถํ์์ ํด์คฌ์ต๋๋ค !
caculateTimeํจ์๋ก ํ๋์ ๋ฌธ์ ๋ฅผ ํ๋ ๊ณ์ฐํด์ฃผ๋ ๊ฒ์ ๋นผ์คฌ์ต๋๋ค.- ์ด๋ถํ์์ผ๋ก
limit๋๋ ์๋๋๋ก ๋ฒ์ ์กฐ์ ํด์ฃผ๋ฉด์ ํ์ดํ์ต๋๋ค.
import Foundation
func solution(_ diffs: [Int], _ times: [Int], _ limit: Int64) -> Int {
var level = -1
var left = diffs.min()!
var right = diffs.max()!
var totalSpendTime = 0
func caculateTime(_ level: Int, _ diff: Int, _ time: Int, _ prevTime: Int) -> Int {
let gap = diff - level
// level์ด ๋ ๋์ ๋ - ๋ฐ๋กํ๋ฆด๋
if gap <= 0 {
return time
}
else {
// print(gap)
// print(time)
// print(prevTime)
// print((gap * (time + prevTime)) + time)
return (gap * (time + prevTime)) + time
}
}
while left <= right {
let mid = (left + right) / 2
totalSpendTime = 0
for index in 0 ..< diffs.count {
var prevTime = 0
if index > 0 {
prevTime = times[index - 1]
}
let tmp = caculateTime(mid, diffs[index], times[index], prevTime)
totalSpendTime += tmp
// print(totalSpendTime)
}
if totalSpendTime > limit {
// print("TOTLAL", totalSpendTime)
left = mid + 1
}
else {
level = mid
right = mid - 1
}
// print(totalSpendTime)
}
return level
}
๐ ๋ฌธ์ ๋งํฌ
ํผ์ฆ๊ฒ์ ์ฑ๋ฆฐ์ง

โ๏ธ ์์๋ ์๊ฐ
30๋ถ
์ฃผ์ํ ๋ถ๋ถ!
testCase๋ฅผ ์น์ ํ ์ฃผ์ง ์์ผ๋ฉด ์๊ฐ์ํ๊ณ ํ๋ค๊ฐ ๊ดํํ ๋ป ํ์ต๋๋ค.
โจ ์๋ ์ฝ๋
๊ทธ๋ ๊ฒ ํด์ ์ด๋ฒ ๋ฌธ์ ์์ ํฌ๊ฒ ์๊ฐํ ๋ถ๋ถ์ ๋๊ฐ์ง์ ๋๋ค!
ํผ์ฆ ๊ฒ์์ ์ฌ์ค ๋ก์ง์ ํ๋ฒ ๋ณด๊ณ ์ดํด๊ฐ ์๊ฐ๋๋ฐ ์์ ๋ฅผ ๋ณด๋๊น ์ดํด๋ฅผ ํ์ต๋๋ค.
์ผ๋ก puzzle์ด ๋์๊ฐ์ต๋๋ค!
(์ดํด๊ฐ ์๊ฐ๋ฉด ์ฝ๋๋ฅผ ๋ณด๋ฉด ํธํ์ค๊ฑฐ์์!)
๊ทธ๋ฌ๊ณ ๋๋จธ์ง ์ด๋ถ ํ์์ ๋งค๋ฒ ์งํํ๋ ์ด๋ถ ํ์์ ๋๋ค!
๊ฒฐ๊ณผ ๊ฐ์ด limit๋ณด๋ค ํฌ๋ฉด? -> start๋ฅผ ๋์
๊ฒฐ๊ณผ ๊ฐ์ด limit๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด? -> end๋ฅผ ๋ฎ์ถค
(ํ์ง๋ง ๊ฒฐ๊ณผ ๊ฐ ์ ์ฅ์ ์ํด answer์ ์ ์ฅํด๋ -> ์ดํ ๋ถ๋ถ์ด limit๋ณด๋ค ํฌ๋ฉด ์ด answer์ ์ฌ์ฉํ๊ธฐ ์ํด)
์ ๋ต ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ๋ก๊ทธ๋๋จธ์ค์์๋ ๋จผ๊ฐ ์ธ์ด์ ๋ํ ์ดํด๊ฐ ๋ง์ง ์์ผ๋ฉด ์ฐธ ํ๋ ๊ฒ ๊ฐ์์
์ค๋๋ง์ max๋ฅผ ์ป๋ค๊ณ ๋ฐ๋ก
max(diffs)๋ฅผ ์ฐ๊ณ ์ ์๋์ง ํ๊ณ ์์์ต๋๋ค ใ ..