Leetcode Note: Go - Final Prices With a Special Discount in a Shop
Final Prices With a Special Discount in a Shop - LeetCode
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/
- Go 言語で取り組んだメモ
回答
Golang Two Solutions - Final Prices With a Special Discount in a Shop - LeetCode
https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/solutions/1311524/golang-two-solutions/
func finalPrices(prices []int) []int {
arr := make([]int , len(prices))
for i := 0 ; i < len(prices)-1 ; i++ {
for j := i+1 ; j< len(prices) ; j++{
//if the price has no discount then just add the price to the result
if prices[j] > prices[i] && j == len(prices)-1{
arr[i] = prices[i]
break
}
//if price is greater or equal to a price
//after it return the start price minus the discount
if prices[j] <= prices[i]{
arr[i] = prices[i]-prices[j]
break
}
}
}
//Appending the last price to the result with no modifications to itself
// becasue it is the last price and there is no discount on it
arr[len(arr)-1] = prices[len(prices)-1]
return arr
}