Re: [閒聊] leetcode 大師請進已回收
※ 引述《pandix (麵包屌)》之銘言:
改成這樣
py code
----------------------------------------------------
def find_LIS_indices(nums):
n = len(nums)
dp = [1] * n
for i in range(1, n):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
max_length = max(dp)
indices = [False] * n
for i in range(n - 1, -1, -1):
if dp[i] == max_length:
indices[i] = True
max_length -= 1
return indices
def min_actions(arr):
indices = find_LIS_indices(arr)
res = []
s = []
ls = []
for i in range(len(arr)):
if arr[i] in indices:
s.append(i)
else:
ls.append(arr[i])
ls.sort()
offset = 0
while ls:
while s and arr[s[-1]] > ls[-1]:
s.pop()
x = ls.pop()
if not s:
res.append((0, x))
else:
res.append((s[-1] + 1 + offset, x))
offset -= 1
return res
# 測試
print(min_actions([1, 3, 7, 9, 5, 2])) # [(2, 5), (0, 2)]
print(min_actions([9, 7, 5, 3, 1])) # [(5, 9), (4, 7), (3, 5), (2, 3)]
print(min_actions([3, 2, 1])) # [(3, 3), (2, 2)]
print(min_actions([1, 7, 9, 2, 3, 4])) # [(6, 9), (5, 7)]
----------------------------------------------------
幹我兔了 擊敗題目==
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1715008887.A.824.html
推
05/06 23:22,
6月前
, 1F
05/06 23:22, 1F
→
05/06 23:22,
6月前
, 2F
05/06 23:22, 2F
→
05/06 23:25,
6月前
, 3F
05/06 23:25, 3F
→
05/06 23:25,
6月前
, 4F
05/06 23:25, 4F
→
05/06 23:25,
6月前
, 5F
05/06 23:25, 5F
推
05/06 23:26,
6月前
, 6F
05/06 23:26, 6F
→
05/06 23:27,
6月前
, 7F
05/06 23:27, 7F
→
05/06 23:27,
6月前
, 8F
05/06 23:27, 8F
→
05/06 23:28,
6月前
, 9F
05/06 23:28, 9F
幹 超麻煩 瘋了 先換回O(n^2)的 靠你了麵包屌
※ 編輯: Rushia (122.100.73.13 臺灣), 05/06/2024 23:31:15
討論串 (同標題文章)
完整討論串 (本文為第 4 之 8 篇):