Dynamic Array

  • + 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
    }