-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import Foundation | ||
|
|
||
| func puzzle(diffs: [Int], times: [Int], level: Int) -> Int{ | ||
| var result = 0 | ||
| for i in 0..<diffs.count { | ||
| if diffs[i] <= level { | ||
| result += times[i] | ||
| } else { | ||
| let discorrectCount = diffs[i] - level | ||
| let discorrectUseTime = times[i - 1] + times[i] | ||
| let totalUseTime = discorrectUseTime * discorrectCount + times[i] | ||
| result += totalUseTime | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
Comment on lines
+3
to
+17
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฌ์ค ์ฝํ
๋ฌธ์ ํ๋๋ผ ์๊ด ์๊ธดํ๋ฐ
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
}
} |
||
|
|
||
|
|
||
| func solution(_ diffs:[Int], _ times:[Int], _ limit:Int64) -> Int { | ||
| var start = 1 | ||
| var end = diffs.max() ?? 100000 | ||
|
Comment on lines
+21
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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๋ค์ฉ) ์ฌ์ค ๋ฌธ์ ์์ ๋น ๋ฐฐ์ด์ด ์ฃผ์ด์ง์ง ์๋๊ฑฐ๊ฐ์์ ์ค์ํ์ง ์์๊ฑฐ๊ฐ์ต๋๋ค.ใ ใ ใ ใ
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ ๋๋ผ๋๋ฐ ์ ๊ฐ ๋ฐฉ์ด์ ์ผ๋ก ์ฝ๋์ง๋ ์ต๊ด์ด ์กฐ๊ธ ์๊ธฐ์ง ์์๋ ์ถ์ด์!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์น ์ด๋ ๊ฒ guard๋ฌธ์ ํ๋ ์์ธ๋ฅผ ๋ ํ์คํ ์ก๋ ๋๋์ด๋ผ ์ข๋ค์!! ์ด ๋ฌธ์ ์์ ๊ทธ๋ฐ ์์ ๋ ์์ง๋ง ์ด๋ ๊ฒ ๋๋ฉด Gil ์ฒ๋ผ guard๋ฌธ์ผ๋ก ํ์ถํด์ print(0)์ ์ฐ์ผ๋ฉด ๋์ด๋ผ ๋์ฑ ์ข์ ๊ฒ ๊ฐ์์!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ธฐ๊ฐ๋งํ๋ค์.. ์ ๋ ์ธ์ ์ฏค |
||
| var mid = 0 | ||
| var answer = 0 | ||
|
|
||
| while start <= end { | ||
| mid = (start + end) / 2 | ||
| let result = puzzle(diffs: diffs, times: times, level: mid) | ||
|
|
||
| if result <= limit { | ||
| answer = mid | ||
| end = mid - 1 | ||
| } else { | ||
| start = 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๊ฒ์์ธ๋ฐ ์ค๋ช ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ด ๋จ์ผ ์ฑ ์ ์์น + ๊ฐ๋ ์ฑ์ ์งํค๋ ๊ฒ ๊ฐ๋ค์!