티스토리 뷰
문제주소
- 카카오 인형뽑기 : https://programmers.co.kr/learn/courses/30/lessons/64061
- 모의고사 : https://programmers.co.kr/learn/courses/30/lessons/42840
- 이상한 문자 만들기 : https://programmers.co.kr/learn/courses/30/lessons/12930
- K번째 수 : https://programmers.co.kr/learn/courses/30/lessons/42748
내 풀이 && 개선방안
// 카카오 인형뽑기
public int solution(int[][] board, int[] moves) {
int answer = 0;
int boardSize = board.length;
List<Integer> stack = new ArrayList<>();
for (int move : moves) {
for (int i = 0; i < boardSize; i++) {
int item = board[i][move - 1];
if (item != 0) {
// 초기에 stack에 0을 추가하고 사용하면 stack.size() != 0 을 체크하지 않아도 됨
if (stack.size() != 0 && stack.get(stack.size() - 1) == item) {
stack.remove(stack.size() - 1);
answer += 2;
} else {
stack.add(item);
}
board[i][move - 1] = 0;
// break 안 해주면 끝까지 탐색함
break;
}
}
}
return answer;
}
// 모의고사
public List<Integer> solution42840(int[] answers) {
List<Integer> answer = new ArrayList<>();
int[] supo1 = new int[]{1, 2, 3, 4, 5};
// 문제 제대로 읽자!!
int[] supo2 = new int[]{2, 1, 2, 3, 2, 4, 2, 5};
int[] supo3 = new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
// 배열로 하면 시간이 더 늘어난다고 한다! (int 방식으로 지정하면 시간 줄어듦)
int[] correct = new int[]{0, 0, 0};
for (int i = 0; i < answers.length; i++) {
// index bound 확인하기!
if (supo1[i % supo1.length] == answers[i]) {
correct[0] += 1;
}
if (supo2[i % supo2.length] == answers[i]) {
correct[1] += 1;
}
if (supo3[i % supo3.length] == answers[i]) {
correct[2] += 1;
}
}
int max = Math.max(correct[0], Math.max(correct[1], correct[2]));
for (int i = 0; i < 3; i++) {
if (correct[i] == max) {
answer.add(i + 1);
}
}
return answer;
}
// 이상한 문자 만들기
public String solution12930(String s) {
StringBuilder answer = new StringBuilder();
int index = 0;
// 문제가 하라는 대로 하자!! 공백이 여러개일수도 있다.
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
if (index % 2 == 0) {
answer.append(String.valueOf(s.charAt(i)).toUpperCase());
} else {
answer.append(String.valueOf(s.charAt(i)).toLowerCase());
}
index += 1;
} else {
index = 0;
answer.append(" ");
}
}
return answer.toString();
}
// K번째 수
public int[] solution42748(int[] array, int[][] commands) {
List<Integer> answer = new ArrayList<>();
for (int[] command : commands) {
int i = command[0];
int j = command[1];
int k = command[2];
// copyOfRange 메소드 fromInclusive, endExclusive
int[] arrayCopy = Arrays.copyOfRange(array, i - 1, j);
// sort
Arrays.sort(arrayCopy);
answer.add(arrayCopy[k-1]);
}
// 리스트를 int[]로 변경하는 방법
return answer.stream().mapToInt(i -> i).toArray();
}
새로 알게된 메소드
- Arrays.copyOfRange(원본배열, int fromInclusive, int toExclusive)
- char[] chars = s.toCharArray(); ---> String s를 char의 배열로 만들어준다. 문자열 인덱스 접근이 헷갈릴때 많은데 char의 배열로 만들고 시작할 수 있다.
- Character.toUpperCase(chars[i]) ---> char를 String으로 바꾸고 toUpperCase를 사용했는데 이렇게하면 char에서 바로 대소문자 변경이 가능하다
- answer.stream().mapToInt(i -> i).toArray(); ---> 리스트를 int[]로 변경하는 방법
스트림을 활용하자! 아직 스트림 활용이 쉽지 않다😂
정리
어렵지 않은 문제, 전에 풀었던 문제가 많았음에도 한 번에 테스트 통과가 안되는 것을 보며 문제를 주어진 조건대로 정확하게 풀어내는 연습을 많이 해야된다는 것을 느꼈다. 항상 indexRange 초과 예외가 발생하지 않게 inclusive, exclusive를 확인해주어야 한다! index를 사용할 때는 boundary를 항상 한 번 더 체크하자! (특히 맨 앞, 뒤에서 발생하는 상황) 그리고 문제를 재해석하지 말고, 문제가 원하는 대로!!! 구현해야한다. (이상한 문자 만들기 문제에서 이걸 다시 느꼈다.)
'Algorithm > 문제풀이 Java' 카테고리의 다른 글
leetcode - Palindrome, Longest common prefix, Linked list cycle (0) | 2021.01.13 |
---|---|
leetcode - Two Sum, Reverse Integer, Remove duplicates from sorted list (0) | 2021.01.13 |
백준 1009 분산처리, 백준 1076 저항, 백준 1052 물병 (0) | 2021.01.07 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- DB
- 월간회고
- 내부코드
- JS
- 개발공부일지
- TCP/IP
- 우아한테크코스
- 알고리즘
- 마스터즈코스
- 인증
- python
- 운영체제
- OS
- 객체지향
- 글쓰기미션
- 우테코수업
- 회고
- 코드스쿼드
- 학습로그
- 카카오
- CS
- javascript
- 네트워크
- JPA
- React
- Transaction
- Spring
- 모의면접준비
- TIL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함