Dynamic Array

  • + 0 comments

    Python 3 solution

    def dynamicArray(n: int, queries: list[list[int]]) -> list[int]:
        arr: list[list[int]] = [[] for __ in range(n)]
        lastAnswer = 0
        answers: list[int] = []
        save_answer = answers.append
        for query in queries:
            subarray = arr[(query[1] ^ lastAnswer) % n]
            if query[0] == 1:
                subarray.append(query[2])
                continue
            save_answer(lastAnswer := subarray[query[2] % len(subarray)])
        return answers