음양더하기, 내적, 예산

이준경·2021년 5월 5일
0

<음양더하기>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public int solution(int[] absolutes, boolean[] signs) {
        int answer = 0;
 
        for(int i = 0; i < signs.length; i++){
            if(signs[i]){
                answer += absolutes[i];
            } else {
                answer -= absolutes[i];
            }
        }
 
        return answer;
    }
}
cs

<내적>

1
2
3
4
5
6
7
8
9
class Solution {
    public int solution(int[] a, int[] b) {
         int sum = 0;
        for(int i=0;i<a.length;i++){
            sum+= a[i]*b[i];
        }
        return sum;
    }
}
cs

<예산>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Arrays;
 
class Solution {
    public int solution(int[] d, int budget) {
        int answer = 0;
        Arrays.sort(d);
        
        for(int n : d){
            if(budget-<0)
                break;
            budget -=n;
            answer++;
        }
        
        return answer;
    }
}
cs

0개의 댓글

관련 채용 정보