iT邦幫忙

2022 iThome 鐵人賽

DAY 19
0
自我挑戰組

30天HackerRank 1 Month Preparation Kit系列 第 19

Day 19 Dynamic Array用Python3

  • 分享至 

  • xImage
  •  

題目

https://ithelp.ithome.com.tw/upload/images/20220919/20151833HEsvj8KJBs.png
https://ithelp.ithome.com.tw/upload/images/20220919/201518331Jy7GkiU9x.png
https://ithelp.ithome.com.tw/upload/images/20220919/20151833T42jDLn7zh.png
https://ithelp.ithome.com.tw/upload/images/20220919/20151833QSoBm7GJAn.png

解題想法

def dynamicArray(n, queries):
    # Write your code here
    arr = [ [] for i in range(n) ]
    lastAnswer = 0
    answers = []
    
    for query in queries:
        idx = (query[1]^lastAnswer) % n
        if query[0] == 1:
            arr[idx].append(query[2])
        if query[0] == 2:
            lastAnswer = arr[idx][query[2] % len(arr[idx])]
            answers.append(lastAnswer)
    return answers

結果

https://ithelp.ithome.com.tw/upload/images/20220919/20151833hmr203o2hs.png
https://ithelp.ithome.com.tw/upload/images/20220919/20151833HKeurepVO8.png


上一篇
Day 18 Max Min用Golang
下一篇
Day 20 Grid Challenge用Javascript
系列文
30天HackerRank 1 Month Preparation Kit30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言