본문 바로가기

전체 글139

[Algorithm] 6월 27일 알고리즘 연습 -에이젠 Lv. 1 숫자 문자열과 영단어 - 성공 0를 빠뜨리는 실수. String.replace()를 적용한 문자열을 따로 할당을 안 하는 실수. class Solution { public int solution(String s) { int answer = 0; String arr = s; arr = arr.replace("one","1"); arr = arr.replace("two","2"); arr = arr.replace("three","3"); arr = arr.replace("four","4"); arr = arr.replace("five","5"); arr = arr.replace("six","6"); arr = arr.replace("seven", "7"); arr = arr.replace("eig.. 2022. 6. 27.
[Algorithm] 6월 24일 알고리즘 연습 - 에이젠 내풀이(1) : 효율성테스트를 통과하지 못함. 수많은 사람들의 점수를 일일이 검사하면서 비효율 발생 -> 참고한 코드를 통해 이분탐색을 통해 해결하는 것을 확인. 듣기로 이문제가 LEVEL2중 상위권 문제라고 많이 언급하는 것 같다. 이 이상의 수준은 두개 이상의 알고리즘을 사용하는 경우가 많을 것같다. class Solution { public int[] solution(String[] info, String[] query) { int[] answer = {}; answer = new int[query.length]; ArrayList arr = new ArrayList(); //생성 for(String str : info){ String[] input = str.split(" "); //사람 생성 /.. 2022. 6. 24.
[Algorithm] 6월23일 알고리즘 연습 - 에이젠 Lv. 2 단체사진 찍기 참고한 풀이 class Solution { private int answer = 0; private String[] friends = {"A", "C", "F", "J", "M", "N", "R", "T"}; public int solution(int n, String[] data) { boolean[] isVisited = new boolean[8]; dfs("", isVisited, data); System.out.println(answer); return answer; } private void dfs(String names, boolean[] isVisited, String[] datas) { if (names.length() == 7) { if (check(names, .. 2022. 6. 23.
[Algorithm] 6월21일 알고리즘 연습(프로그래머스 LEVEL2) - 에이젠 Lv. 2 괄호 변환 - 실패 내 풀이(1) : 정확도 20% 중간에 문제를 안 읽은 것이 원인... 처음부터 다시 구현했어야 한다. import java.io.*; import java.util.*; class Solution { public String solution(String p) { String answer = ""; StringBuilder sb = new StringBuilder(); StringBuilder tempSb = new StringBuilder(); Stack stack = new Stack(); char[] arr = p.toCharArray(); int reverseCount = 0; //괄호 반전시킬 개수 for(int i = 0; i < p.length(); i++){ .. 2022. 6. 21.
[Algorithm] 6월 19일 알고리즘 연습 (백준) - 에이젠 백준 2745 진법 변환 - 성공 직접 실행해서 볼땐 에러가 없는데 이러한 런타임에러가 계속 발생하였다. 알고보니 클래스 이름을 Main으로 지어야하는 문제... 내풀이 class Main { public static StringBuilder sb = new StringBuilder(); //숫자. public static String N = ""; //진법 public static int B = 0; static Scanner sc = new Scanner(System.in); public static void main(String[] args){ input(); System.out.println(converter()); } public static void input(){ N = sc.next(); .. 2022. 6. 19.
[Algorithm] 6월19일 알고리즘 연습 (프로그래머스 LEVEL2) - 에이젠 Lv. 2 피보나치 수 - 성공 내풀이(1) : 정확도 42.9%, 시간초과. 피보나치의 계산 결과를 기억하면서 계산할 수 있는 방법을 사용. 비슷한 문제를 푼 기억이 있음. class Solution { public int solution(int n) { int answer = 0; answer = fibo(n); return answer; } public int fibo(int k){ int result = 0; if(k == 1) return 1; if(k == 0) return 0; result = (fibo(k-2) + fibo(k-1)) % 1234567; return result; } } 내풀이(2) class Solution { public int solution(int n) { int a.. 2022. 6. 19.
[Algorithm] 6월 18일 알고리즘 연습(프로그래머스 LEVEL2) - 에이젠 Lv. 2 올바른 괄호 - 성공 //스택 이용 class Solution { boolean solution(String s) { boolean answer = true; char[] arr = s.toCharArray(); // [ (,),(,)] Stack stack = new Stack(); for(char ch : arr){ if(ch == ')' && !stack.isEmpty() && stack.peek() == '('){ stack.pop(); continue; } stack.push(ch); } if(stack.isEmpty()){ answer = true; }else{ answer = false; } return answer; } } Lv. 2 다음 큰 숫자 - 실패 내풀이 (1) : 정확.. 2022. 6. 18.
728x90