Programmers / Operation Based on Length

Programmers / Operation Based on Length

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        
        if (num_list.length >= 11) {
            for (int i = 0; i < num_list.length; i++) {
                answer += num_list[i];
            }
        } else {
            answer = 1;
            for (int i = 0; i < num_list.length; i++) {
                answer *= num_list[i];
            }
        }
        
        return answer;
    }
}
Solution 1
  • Description
    • Check the array length and then perform the operation
  • Time Complexity
    • O(1)
    • Always performs the same operation
  • Space Complexity
    • O(1)
    • Function parameters and local variables