[問題]算法 k distinct letters

看板Python作者 (suhang)時間7年前 (2018/03/13 07:10), 編輯推噓1(102)
留言3則, 2人參與, 7年前最新討論串1/2 (看更多)
https://imgur.com/a/2GAU3 my solution https://repl.it/@shih_hsuanhsu/KDistinctCharacter def KDistinctCharacter3 def KDistinctCharacter2 兩個方法應該都正確,但是複雜度為O(nk) 網路上高手說可以做到O(n) 我試著又寫了 KDistinctCharacter 但是我想不透該怎麼做 求助! 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.89.32.145 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1520896240.A.EE7.html

03/13 10:00, 7年前 , 1F
想想看iterate的時候哪些東西可以不用重算
03/13 10:00, 1F

03/19 13:39, 7年前 , 2F
大概就是不用每個run都檢查window中的character
03/19 13:39, 2F

03/19 13:40, 7年前 , 3F
用記錄該字元上次出現的位置來達到O(n)
03/19 13:40, 3F
文章代碼(AID): #1QfmZmxd (Python)
討論串 (同標題文章)
文章代碼(AID): #1QfmZmxd (Python)