반응형
야근 지수
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
Arrays.sort(works);
long[] cnt = new long[works[works.length-1] + 1];
long sum = 0;
for(int a : works) {
sum += a;
cnt[a]++;
}
if(sum <= n) return answer;
for(int i = cnt.length-1; i > 1; i--) {
if(cnt[i] < n) {
cnt[i-1] += cnt[i];
n -= cnt[i];
cnt[i] = 0;
} else {
cnt[i] -= n;
cnt[i-1] += n;
n = 0;
break;
}
}
if(n!=0)
cnt[1] -= n;
for(int i = 1; i < cnt.length; i++) {
if(cnt[i] == 0) continue;
answer += i * i * cnt[i];
}
return answer;
}
}
최고의 집합
import java.util.*;
class Solution {
public int[] solution(int n, int s) {
int[] answer = new int[n];
if(n > s) {
return new int[] {-1};
}else if (n == s) {
Arrays.fill(answer, 1);
} else {
Arrays.fill(answer, s / n);
s = s % n;
for(int i = n-s; i < n; i++) {
answer[i]++;
}
}
return answer;
}
}
단어변환 - DFS/BFS
class Solution {
public int min = 10;
public void bfs(String begin, String target, String[] words, int cnt){
if(cnt > min) return ;
if(begin.equals(target)) {
if(cnt < min) {
min = cnt;
}
return ;
}
boolean[] visit = new boolean[words.length];
for(int i = 0; i < words.length; i++) {
if(visit[i]) continue;
String word = words[i];
int c = 0;
for(int j = 0; j < begin.length(); j++) {
if(begin.charAt(j) != word.charAt(j)){
c++;
}
if(c > 1)break;
}
if(c == 1) {
visit = new boolean[words.length];
visit[i] = true;
bfs(word, target, words, cnt + 1);
}
}
}
public int solution(String begin, String target, String[] words) {
int answer = 0;
String str = String.join("", words);
for(char c : target.toCharArray()) {
if(!str.contains(String.valueOf(c)))
return answer;
}
bfs(begin, target, words, 0);
return min;
}
}
반응형
'코딩 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Lv3 문제풀이6(feat. JAVA) (1) | 2023.10.11 |
---|---|
[프로그래머스] Lv3 문제풀이5(feat. JAVA) (0) | 2023.10.10 |
[프로그래머스] Lv3 문제풀이4(feat. JAVA) (0) | 2023.10.10 |
[프로그래머스] Lv3 문제풀이3(feat. JAVA) (0) | 2023.10.06 |
[프로그래머스] Lv3 문제풀이1(feat. JAVA) (0) | 2023.10.04 |