2460. Apply Operations to an Array

problem

solution

  • One pass
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class Solution {
    public:
    vector<int> applyOperations(vector<int>& nums) {
    int n = nums.size();
    vector<int> ret(n,0);
    int k = 0;
    for(int i=0;i<n;++i)
    {
    if(nums[i]==0) continue;
    if(i<n-1 && nums[i] == nums[i+1]){
    i++;
    ret[k++] = nums[i]*2;
    }
    else ret[k++] = nums[i];

    }
    return ret;
    }
    };
  • Move Zeroes
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Solution {
    public:
    vector<int> applyOperations(vector<int>& nums) {
    int n = nums.size();
    for(int i=0;i<n-1;++i)
    {
    if(nums[i] == nums[i+1]){
    nums[i]*=2;
    nums[i+1] = 0;
    i++;
    }
    }
    int i=0, j=0;
    for(int i=0;i<n;++i)
    {
    if(nums[i])
    {
    swap(nums[j++], nums[i]);
    }
    }
    return nums;

    }
    };

analysis

  • time complexity O(1)
  • space complexity O(n)