728x90 코딩 테스트30 2667번 단지번호붙이기(자바, java) - DFS import java.io.*; import java.util.*; public class Main { public static int count = 0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[][] matrix = new int[n][n]; boolean[][] visited = new boolean[n][n]; for(i.. 2023. 1. 9. 2606번 바이러스(자바, java) - DFS 런타임(인덱스 아웃) 에러가 떳었는데 문제는 배열사이즈 때문임. computer와 n 두개 받을때 혼동말것 import java.io.*; import java.util.*; public class Main { public static int count = 0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int computer = Integer.parseInt(st.nextToken()); st = new St.. 2023. 1. 9. 프로그래머스 네트워크 - DFS 문제를 잘못읽어서 오래걸림. 네트워크가 나뉘어잇는 개수를 찾는문제 class Solution { public int solution(int n, int[][] computers) { int answer = 0; boolean[] visited = new boolean[n]; for(int ii = 0 ; ii < n ; ii++) { /* 같은 네트워크의 조사가 끝날때마다 카운트 */ if(dfs(n, ii, computers, visited)) answer++; } return answer; } public static boolean dfs(int n, int start, int[][] computers, boolean[] visited) { if(visited[start] == true) return.. 2023. 1. 9. 프로그래머스 - 문자열 압축 class Solution { public int solution(String s) { int answer = s.length(); for (int ii = 1; ii 2023. 1. 9. 이진탐색 샘플 import java.util.*; public class Solution { public static void main(String[] args) { //리스트 ArrayList list = new ArrayList(); list.add(1); list.add(5); list.add(4); list.add(2); list.add(3); Collections.sort(list); // 정렬필수 System.out.println(Collections.binarySearch(list, 5)); // 이진탐색 //배열 int[] array = {5,4,3,2,1}; Arrays.sort(array); // 정렬필수 System.out.println(Arrays.binarySearch(array, 5)); //.. 2023. 1. 9. 프로그래머스 - 로또의 최고 순위와 최저 순위 자바 이진탐색을 생각할 수 있느냐 없느냐의 문제 import java.util.*; class Solution { public int[] solution(int[] lottos, int[] win_nums) { int[] answer = new int[2]; boolean[] visited = new boolean[45]; int zero_cnt = 0; int correct_cnt = 0; Arrays.sort(win_nums); for(int ii = 0 ; ii = 0) correct_cnt++; } else zero_cnt++; } if.. 2023. 1. 9. 프로그래머스 - 다단계 칫솔 판매 자바 java 10번부터 시간초과로 고생한 문제. 밑에 함수를 사용해서 index를 가져왔는데 시간초과 계속발생. hashmap으로 만들어서 서치하는게 훨씬빠른가보다.. public static int searchIndex(String[] enroll, String target) { int index = -1; for(int ii = 0; ii < enroll.length ; ii++) { if(target.equals(enroll[ii])) index = ii; } return index; } 정답소스코드 import java.util.*; class Solution { public static int[] solution(String[] enroll, String[] referral, String[] seller, .. 2023. 1. 9. 프로그래머스 - 헤비 유저가 소유한 장소 sql 문제 SELECT A.ID, A.NAME, A.HOST_ID FROM PLACES AS A, (select HOST_ID, ID, COUNT(*) AS CNT from places GROUP BY HOST_ID ORDER BY CNT DESC, ID DESC) AS B WHERE A.HOST_ID = B.HOST_ID AND B.CNT > 1 ORDER BY A.ID ASC, B.CNT DESC ; https://programmers.co.kr/learn/courses/30/lessons/77487 2023. 1. 9. 프로그래머스 - 행렬 테두리 회전하기 자바 java 실전이였다면 시간내에 못풀었을 듯. 어렵다기보단 값을 잡아나가는게 힘들었다. 사진상으로 10번 28번 26번 위치를 기억해놓고 움직인다. 정답 class Solution { public static int[] solution(int rows, int columns, int[][] queries) { int[] answer = new int[queries.length]; int[][] matrix = new int[rows][columns]; int value = 1; for(int ii = 0 ; ii < rows; ii++) { for(int jj = 0 ; jj < columns ; jj++) matrix[ii][jj] = (value++); } for(int ii = 0 ; ii < querie.. 2023. 1. 9. 프로그래머스 - 신규 아이디 추천 자바 java 문제 예시로만 풀다가 계속 틀려서 수정보완. import java.util.*; class Solution { public String solution(String new_id) { String answer = ""; char[] char_array = new_id.toCharArray(); Queue que = new LinkedList(); Queue que2 = new LinkedList(); int continue_check = 0; int size = 0; StringBuilder sb = new StringBuilder(); for(int ii = 0 ; ii = 'A' && char_.. 2023. 1. 9. 이전 1 2 3 다음 728x90