Skip to content

Instantly share code, notes, and snippets.

@AppleCEO
Created June 24, 2020 09:44
Show Gist options
  • Select an option

  • Save AppleCEO/b1a0e888de5da1ebb4512eac03eec2d6 to your computer and use it in GitHub Desktop.

Select an option

Save AppleCEO/b1a0e888de5da1ebb4512eac03eec2d6 to your computer and use it in GitHub Desktop.
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