Home 02. Dynamic Arrays
Post
Cancel

02. Dynamic Arrays

Problem: Dynamic Arrays

Solution 1: Using for loop and if-else statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def dynamicArray(n, queries):
    seqList = [[] for _ in range(n)]
    lastAnswer = 0
    result = []

    for query in queries:
        index = (query[1] ^ lastAnswer) % n

        if query[0] == 1:
            seqList[index].append(query[2])
        else:
            lastAnswer = seqList[index][query[2] % len(seqList[index])]
            result.append(lastAnswer)

    return result
This post is licensed under CC BY 4.0 by the author.

Learn Data Science step by step

01. Print "Hello World!"