Dynamic Array

Sort by

recency

|

54 Discussions

|

  • + 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
    
  • + 0 comments

    My rust solution:

    fn dynamicArray(n: i64, queries: &[Vec<i64>]) -> Vec<i64> {
        let mut arr = vec![Vec::<i64>::new(); n as usize];
        let mut last_answer = 0;
        let mut answer = Vec::new();
        
        for query in queries {
            if query[0] == 1 {
                let idx = (query[1].bitxor(last_answer) % n) as usize;
                arr[idx].push(query[2]);
            } else {
                let idx = (query[1].bitxor(last_answer) % n) as usize;
                last_answer = arr[idx][(query[2] % arr[idx].len() as i64) as usize];
                answer.push(last_answer);
            }
        }
        answer
    }
    
  • + 0 comments

    Where is the problem solving here? no problem to solve, no thinking, the problem is giving us the instructions to write ?

  • + 0 comments
    def dynamicArray(n, queries):
        lastAnswer = 0
        arr = []
        answers = []
        for i in range(n):
            arr.append([])
        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)
    
  • + 0 comments

    def ndimensional_list(dimensions):

    return [] if not dimensions else [ndimensional_list(dimensions[1:]) for _ in range(dimensions[0])]
    

    def dynamicArray(n, queries):

    arr = ndimensional_list([n])
    lastAnswer = 0
    n_arr = []
    for i in queries:
        q = i[0]
        x = i[1]
        y = i[2]
        if q == 1:
            idx = (x ^ lastAnswer) % n
            arr[idx].append(y)
        if q == 2:
            idx = (x ^ lastAnswer) % n
            idy = y % len(arr[idx])
            lastAnswer = arr[idx][idy]
            n_arr.append(lastAnswer)
    return n_arr