Created
June 24, 2020 09:44
-
-
Save AppleCEO/b1a0e888de5da1ebb4512eac03eec2d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func finalPrices(_ prices: [Int]) -> [Int] { | |
| var answer = [Int]() | |
| for (index, price) in prices.enumerated() { | |
| if index+1 == prices.count { | |
| answer.append(price) | |
| } else { | |
| if price >= prices[index+1] { | |
| let discount = prices[index+1] | |
| answer.append(price - discount) | |
| } else { | |
| for afterIndex in index+2..<prices.count { | |
| if price >= prices[afterIndex] { | |
| let discount = prices[afterIndex] | |
| answer.append(price - discount) | |
| break | |
| } | |
| } | |
| if index == answer.count { | |
| answer.append(price) | |
| } | |
| } | |
| } | |
| } | |
| return answer | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment