Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Programmers/연습문제/LEVEL1/대충 만든 자판/Sanghoo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package programmers.대충만든자판;

import java.util.HashMap;
import java.util.Map;

public class Sanghoo {
public static void main(String[] args) {
String[] keyMap = {"BC"};
String[] targets = {"AC", "BC"};

solution(keyMap, targets);
}

public static int[] solution(String[] keymap, String[] targets) {
int[] answer = new int[targets.length];
Map<Character, Integer> minKeyMap = new HashMap<>();

// keymap을 역순으로 돌아, Map에 각 키(Key)를 최소한으로 나타낼 수 있는 개수(Value)를 저장해두고 사용한다.
for (String key : keymap) {
char[] chars = key.toCharArray();

for (int j = chars.length - 1; j >= 0; j--) {
int value = minKeyMap.get(chars[j]) == null ? Integer.MAX_VALUE : minKeyMap.get(chars[j]);
minKeyMap.put(chars[j], Math.min(value, j + 1));
}
}

for (int i = 0; i < targets.length; i++) {
int count = 0;
for (char ch : targets[i].toCharArray()) {
Integer getVal = minKeyMap.get(ch);

// target을 만들 수 없을 경우 -1을 저장하고 break
if (getVal == null) {
count = -1;
break;
}
count += getVal;
}
answer[i] = count;
}

return answer;
}

}
29 changes: 29 additions & 0 deletions Programmers/연습문제/LEVEL2/숫자의 표현/Sanghoo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Programmers.숫자의표현;

public class Sanghoo {

public static void main(String[] args) {
solution(15);
}

public static int solution(int n) {
int answer = 1;

for (int i = 1; i < n; i++) {
int sum = i;

for (int j = i + 1; j < n; j++) {
sum += j;

if (sum > n) break;
if (sum == n) {
answer++;
break;
}
}
}

return answer;
}

}