跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 不知道有没有更好的解法~
未分類
18 4 月 2021

不知道有没有更好的解法~

不知道有没有更好的解法~

資深大佬 : zzzrf 0

原题直接看这里吧~

我就写了一种解法。也不知道是不是最优解。大家帮忙看看~

解释:

每一个物品都保持原价,他们的右边都没有等于或者更低价格的物品

单调非递减栈来存储之前的值,当出现比栈顶所存值更小的值则可以更新之前的价格。

代码

class Solution:     """     @param prices: a list of integer     @return: return the actual prices     """     def FinalDiscountedPrice(self, prices):         # write your code here         s, res = [], [prices[i] for i in range(len(prices))]          for i in range(len(prices)):                 while len(s) != 0 and prices[s[-1]] >= prices[i]:                         index = s[-1]                         s.pop()                         res[index] = prices[index] - prices[i]                 s.append(i);         return res 

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具