티스토리 뷰
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/132267




테스트 케이스는 통과하는데 제출시 우르르 실패로 떠서 뭐지(?) 싶었다.
그런데 역시 내가 문제였고.. 문제를 잘 읽어봤어야 했다..
처음 제출했던 답은 아래와 같다
func solution(_ a:Int, _ b:Int, _ n:Int) -> Int {
var currentCola: Int = n
var result: Int = 0
while currentCola >= a {
currentCola -= a
result += 1 // 빈병을 반납하면 콜라 한병을 받는거로 구했음
currentCola += b
}
return result
}
a갯수만큼 빈병을 가져다주면 b갯수만큼 돌려 받는다는걸
예시에만 꽂혀서 b갯수가 아닌 한병씩 돌려받는걸로 구현을 했다가 제출하니 우르르 실패가 떴었던것.
(컴퓨터는 거짓말을 안한다.🫠)
내 풀이
func solution(_ a:Int, _ b:Int, _ n:Int) -> Int {
var currentCola: Int = n
var result: Int = 0
while currentCola >= a {
currentCola -= a
result += b // <== 수정
currentCola += b
}
return result
}
b 갯수만큼 돌려받는걸로 수정하니 해결~!
728x90
'알고리즘' 카테고리의 다른 글
| [Swift 알고리즘] - 올바른 괄호 (Programmers) (0) | 2024.08.09 |
|---|---|
| [Swift 알고리즘] - 숫자 문자열과 영단어 (Programmers) (0) | 2024.07.31 |
| [Swift 알고리즘] - 행렬의 덧셈 (Programmers) (0) | 2024.07.11 |
| [Swift 알고리즘] - Valid Parentheses (LeetCode) (2) | 2023.08.24 |
| [Swift 알고리즘] - Best Time to Buy and Sell Stock (LeetCode) (0) | 2023.08.22 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- swift reduce
- Combine: Asynchronous Programming with Swift
- Swift Leetcode
- Swift init
- Swift 알고리즘
- RIBs tutorial
- ios
- RTCCameraVideoCapturer
- Swift inout
- Swift joined()
- Swift final
- swift 고차함수
- Swift joined
- swift protocol
- CS 네트워크
- Swift 프로그래머스
- 원티드 프리온보딩
- Swift 프로퍼티
- swift property
- Swift
- iOS error
- Class
- Swift ModernRIBs
- Swift RIBs
- swift (programmers)
- Swift Error Handling
- 2023년 회고
- removeLast()
- swift programmers
- Swift 내림차순
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함