SHARE
TWEET

Untitled

a guest Oct 20th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Runtime: 1 ms, faster than 100.00% of Java online submissions for Squares of a Sorted Array.
  2. // Memory Usage: 40.7 MB, less than 96.34% of Java online submissions for Squares of a Sorted Array.
  3. class Solution {
  4.     public int[] sortedSquares(int[] A) {
  5.         int len = A.length;
  6.         int[] res = new int[len];
  7.        
  8.         int p = 0;
  9.         int n = len;
  10.        
  11.         while (p < len && A[p] < 0)
  12.             res[--n] = -A[p++];
  13.        
  14.         int r = 0;
  15.         while (p < len || n < len) {
  16.             if (n >= len || p < len && A[p] < res[n]) {
  17.                 res[r++] = A[p] * A[p];
  18.                 p++;
  19.             } else {
  20.                 res[r++] = res[n] * res[n];
  21.                 n++;
  22.             }
  23.         }
  24.        
  25.         return res;
  26.     }
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top