Code&Data Insights

[Leetcode-977] Squares of a Sorted Array (Arrays) 본문

Algorithm/Leetcode

[Leetcode-977] Squares of a Sorted Array (Arrays)

paka_corn 2022. 6. 10. 09:52

 

 

[Leetcode-977] 

 

Squares of a Sorted Array (Arrays)

 

 

 

 

[ 내 코드 - Accepted ] 

 

class Solution {
    public int[] sortedSquares(int[] nums) {
        
        int [] square = new int[nums.length];
        
        for(int i=0; i < nums.length; i++){
            
            square[i] = nums[i] * nums[i];
        }
        
        Arrays.sort(square);
        
     return square;   
    }
}

 

=> 앞에 풀었던 두 개 문제보다 훨씬 쉬웠다. square 하는걸 Math.pow()함수 써서 했는데 오류나서 그냥 했다. 

아직 자료구조&알고리즘을 공부하기 시작한지 얼마 안되서 시간 복잡도나 이런건 잘 모르겠다.

 

Q. 그래서 O(n) 이란 무엇인가??? 

 

The comment was referring to the Big-O Notation.

Briefly:

  1. O(1) means in constant time - independent of the number of items.
  2. O(N) means in proportion to the number of items.
  3. O(log N) means a time proportional to log(N)

Basically, any 'O' notation means an operation will take the time up to a maximum of k*f(N)
where:

k is a constant multiplier

f() is a function that depends on N

 

 

 

===> Big O 에 대해 공부해야겠다! 

Comments