Algorithms and Data Structures/Coding Practices

AlgoExpert Sorted Squared Array

brightlightkim 2022. 7. 10. 15:25

import java.util.*;

class Program {

  public int[] sortedSquaredArray(int[] array) {
    int counter = array.length - 1;
    int lastIdx = array.length - 1;
    int firstIdx = 0;
    int[] out = new int[array.length];
    for (int i = 0; i < array.length; i++){
      if (Math.abs(array[firstIdx]) > Math.abs(array[lastIdx])){
        out[counter - i] = array[firstIdx]*array[firstIdx];
        firstIdx++;
      } else {
        out[counter - i] = array[lastIdx]*array[lastIdx];
        lastIdx--;
      }
    }
    return out;
  }
}

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글

AlgoExpert Depth First Search (DFS)  (0) 2022.07.12
AlgoExpert Node Depths  (0) 2022.07.12
AlgoExpert Non-Constructible Change  (0) 2022.07.10
AlgoExpert Tournament Winner  (0) 2022.07.10
LeetCode 45. Jump Game II  (0) 2022.07.08