Sort by

recency

|

3546 Discussions

|

  • + 0 comments

    Here's a solution in C

    public static List<int> rotateLeft(int d, List<int> arr)
        {
            for(int i =0; i < d; i++){
                arr.Add(arr[0]);
                arr.Remove(arr[0]);
            }
            
            return arr;
        }
    
  • + 0 comments

    My solution. What do you think?

    	let numref = [...arr]
        numref.length = d
      
        return arr.slice(d,arr.length).concat(numref)
    
  • + 0 comments

    i had a problem with the variable idx, the problem says about calculate two idx, the first in the query one (idx = (x ^ last_answer)) and the second in query two (idx = (x ^ last_answer) % n) i solve the problem with variable idx = (x ^ last_answer) % n in the up of my for cycle and delete other idx code:

    def dynamicArray(n, queries): array_2D = [] last_answer = 0 results = [] for i in range(len(queries)): array_2D.append([]) for types,x,y in queries: idx = (x ^ last_answer) % n if types == 1: #idx = (x ^ last_answer) array_2D[idx].append(y) else: #idx = (x ^ last_answer) % n last_answer = array_2D[idx][y % len(array_2D[idx])] results.append(last_answer) return results

    result = dynamicArray(2, [[1,1,1],[1,1,7],[1,0,3],[2,1,0],[2,1,1]]) 
    for i in result:
        print(i)
    
  • + 0 comments

    Concise O(n) Python solution using slicing

    def rotateLeft(d, arr):
        effective_d = d % len(arr)
        if len(arr) == 1 or effective_d == len(arr):
            return arr
            
        arr = arr[effective_d:] + arr[:effective_d]
            
        return arr
    
  • + 0 comments

    vector rotateLeft(int d, vector arr) { int n = arr.size(); if(d>n){ d = d%n; } reverse(arr.begin(),arr.begin()+d); reverse(arr.begin()+d,arr.end()); reverse(arr.begin(),arr.end()); return arr; }