diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..305deb8 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..0df9019 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + codingTest + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..bb5f665 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/codingTest/module.java=UTF-8 +encoding//src/codingTest/module2.java=UTF-8 +encoding//src/codingTest/module3.java=UTF-8 diff --git a/README.md b/README.md deleted file mode 100644 index 4d49392..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# codingTest \ No newline at end of file diff --git a/bin/codingTest/Animal.class b/bin/codingTest/Animal.class new file mode 100644 index 0000000..e39fcc6 Binary files /dev/null and b/bin/codingTest/Animal.class differ diff --git a/bin/codingTest/Eatable.class b/bin/codingTest/Eatable.class new file mode 100644 index 0000000..55b3850 Binary files /dev/null and b/bin/codingTest/Eatable.class differ diff --git a/bin/codingTest/Person.class b/bin/codingTest/Person.class new file mode 100644 index 0000000..ab2460a Binary files /dev/null and b/bin/codingTest/Person.class differ diff --git a/bin/codingTest/Solution.class b/bin/codingTest/Solution.class new file mode 100644 index 0000000..f4e92cd Binary files /dev/null and b/bin/codingTest/Solution.class differ diff --git a/bin/codingTest/module3.class b/bin/codingTest/module3.class new file mode 100644 index 0000000..29101c2 Binary files /dev/null and b/bin/codingTest/module3.class differ diff --git a/bin/module-info.class b/bin/module-info.class new file mode 100644 index 0000000..845d9c6 Binary files /dev/null and b/bin/module-info.class differ diff --git a/src/codingTest/Animal.java b/src/codingTest/Animal.java new file mode 100644 index 0000000..0b78378 --- /dev/null +++ b/src/codingTest/Animal.java @@ -0,0 +1,10 @@ +package codingTest; + +public abstract class Animal { + + public static String str12 = "3"; + + String talk() { + return "talktalktalk"; + } +} diff --git a/src/codingTest/Eatable.java b/src/codingTest/Eatable.java new file mode 100644 index 0000000..427e5c0 --- /dev/null +++ b/src/codingTest/Eatable.java @@ -0,0 +1,8 @@ +package codingTest; + +public interface Eatable { + + String str1 = "1"; + + String eat(); +} diff --git a/src/codingTest/Person.java b/src/codingTest/Person.java new file mode 100644 index 0000000..3351e7e --- /dev/null +++ b/src/codingTest/Person.java @@ -0,0 +1,12 @@ +package codingTest; + +public class Person implements Eatable { + + @Override + public + String eat() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/codingTest/module.java b/src/codingTest/module.java new file mode 100644 index 0000000..c3d7948 --- /dev/null +++ b/src/codingTest/module.java @@ -0,0 +1,1305 @@ +package codingTest; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; +import java.util.TreeMap; +import java.util.stream.Collectors; + + +class Card{ + static int cnt; + int data; + Card(int data){ + this.data = data; + cnt++; + } +} + +public class module { + +// public static void main(String[] args) { +// +// //문제풀이 기본 양식 +// Solution s = new Solution(); +// String[] answer = {}; +// +// +// //String[] rooms= {"[403]James", "[404]Azad,Louis,Andy", "[101]Azad,Guard"}; +// //String[] rooms= {"[101]Azad,Guard", "[202]Guard", "[303]Guard,Dzaz"}; +// String[] rooms= {"[1234]None,Of,People,Here", "[5678]Wow"}; +// int target = 1234; +// +//// System.out.println(s.solution11(new String[] {"I 16","D 1"})); +//// System.out.println(s.solution11(new String[] {"I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"})); +// System.out.println(s.solution13(12,6)); +// +//// answer = s.solution("JEROEN"); +//// System.out.println(" answer : " + answer); +// +// +// //answer = s.solution(new String[]{"spy","ray","spy","room","once","ray","spy","once"} ); +//// answer = s.solution(new String[]{"spy","ray","spy","room","once","ray","spy","once", "room"} ); +//// System.out.println(" answer : " + answer); +//// Arrays.stream(answer).forEach(t -> System.out.println(t)); +//// +//// int ans = 0; +//// ans = s.solution00(new int[] {20,40,65,110}, 30); +//// System.out.println(ans); +// +// // +// +// +// +// +// //dfs, bfs +//// Scanner sc = new Scanner(System.in); +//// int n = sc.nextInt(); +//// int m = sc.nextInt(); +//// int start = sc.nextInt(); +//// a = (ArrayList[]) new ArrayList[n+1]; +//// for (int i=1; i<=n; i++) { +//// a[i] = new ArrayList(); +//// } +//// for (int i=0; i true); //Stream filter + //Stream filter 처리 후 list 변환 + //ArrayList alreadyRankUser = (ArrayList) rankList.stream().filter(t -> t[0].equals(userName) && Integer.parseInt(t[1]) >= userScore).collect(Collectors.toList()); + + //=========================================================== + + + List list = new ArrayList(); + + + list.add(new Integer[][] {{1,3}}); + + //arrayList + List list1 = new ArrayList(); + int index = 0; + list1.add(index, 0); + list1.remove(index); + for (Integer integer : list1) { + System.out.println(integer); + } + + + //=========================================================== + + //map 선언 + HashMap map = new HashMap(); + + HashMap> ma2 = new HashMap>(); + + //ma2.put("aa", new List); + + for(String key : map.keySet()) { + + } + + //=========================================================== + + //Stack + Stack stack = new Stack(); + stack.add(null); //넣기 + stack.empty(); //비었는지 확인 + stack.peek(); //stack 최상단 값 + stack.pop(); //stack 빼기 + + //Queue + Queue queue = new LinkedList<>(); + queue.add(null); //넣기 + queue.isEmpty(); //비었는지 확인 + queue.peek(); //queue 나올 값 + queue.poll(); //queue 빼기 + + + //기타 + //int[] 를 integer[]로 바꾸는 방법 + Integer[] arr2 = Arrays.stream( arr ).boxed().toArray( Integer[]::new ); + + //forEach 구문 + Arrays.stream(arr).forEach(s -> System.out.println(s)); + + //한글자씩 split -- 파라미터 ""로 + String str = "12345"; + String[] result = str.split(""); + + //binarySearch + Arrays.binarySearch(result, "1"); + PriorityQueue q = new PriorityQueue(); + + //정렬 + /* + String[] result = new String[numbers.length]; + for (int i = 0; i < numbers.length; i++) { + result[i] = String.valueOf(numbers[i]); + } + // 정렬 + Arrays.sort(result, new Comparator() { + @Override + public int compare(String o1, String o2) { + return ((o2 + o1).compareTo(o1 + o2)); + } + }); + */ + + + } + public static boolean isPrime(int num){ + for(int i=2; i<=num/2; i++){ + if(num % i == 0) return false; + } + return true; + } + + + static ArrayList[] a; + static boolean[] c; + public static void dfs(int x) { + if (c[x]) { + return; + } + c[x] = true; + System.out.print(x + " "); + for (int y : a[x]) { + if (c[y] == false) { + dfs(y); + } + } + } + public static void bfs(int start) { + Queue q = new LinkedList(); + q.add(start); + c[start] = true; + while (!q.isEmpty()) { + int x = q.remove(); + System.out.print(x + " "); + for (int y : a[x]) { + if (c[y] == false) { + c[y] = true; + q.add(y); + } + } + } + } + + public boolean BinarySearch(int []arr, int len, int key){ + int start = 0; + int end = len-1; + int mid; + + while(end - start >= 0) { + mid = (start + end) / 2; //중앙 값 + + if (arr[mid] == key) { //key값을 찾았을때 + return true; + + } else if (arr[mid] > key) { //key값이 mid의 값보다 작을때 (왼쪽으로) + end = mid - 1; + + } else { //key값이 mid의 값보다 클때 (오른쪽으로) + start = mid + 1; + + } + } + return false; + } +} + + + + + + + +class Solution { + + /** + * + * @author hs + * @스택 - 주식 가격...... 얘 잘 모르겟다 + * + */ + public int[] solution1(int[] prices) { + + Stack stack = new Stack<>(); + int[] ret = new int[prices.length]; + + for (int i = prices.length - 2; i >= 0; i--) { + int day = 0; + + while (!stack.isEmpty() && stack.peek()[0] >= prices[i]) { + day += stack.pop()[1]; + } + + ret[i] = stack.push(new Integer[]{prices[i], day + 1})[1]; + } + + return ret; + } + + public int solution2(int bridge_length, int weight, int[] truck_weights) { + + int answer = 0; + + Queue queue = new LinkedList<>(); + int currentWeight = 0; + + for(int t: truck_weights){ + while(true) { + if(queue.isEmpty()){ + queue.add(t); + currentWeight += t; + answer++; + break; + } else if(queue.size() == bridge_length){ + currentWeight -= queue.poll(); + } else { + if(currentWeight + t > weight){ + queue.add(0); + answer++; + } else { + queue.add(t); + currentWeight+=t; + answer++; + break; + } + } + } + } + answer = answer + bridge_length; + + return answer; + } + + public int solution3(int[] A , int[] B) { + int answer = 0; + + Arrays.sort(A); + Integer[] arr = Arrays.stream( B ).boxed().toArray( Integer[]::new ); + + List list = Arrays.asList(arr); + + Arrays.sort(arr, Collections.reverseOrder()); + for (int i = 0; i < B.length; i++) { + B[i] = arr[i]; + System.out.println(B[i]); + } + + for(int i = 0; i< A.length; i++){ + answer += (A[i] * B[i]); + } + + return answer; + } + + public String solution4(String s) { + + String answer = ""; + + String[] stringArr = s.split(" "); + + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + + for(String str: stringArr) { + int num = 0; + + if(str.contains("-")) { + num = (-1) * Integer.parseInt(str.replace("-", "")); + } else { + num = Integer.parseInt(str); + } + + if(num > max) { + max = num; + } + + if(num < min) { + min = num; + } + } + + return answer; + } + + public String[] solution5(String[] record) { + + HashMap uidMap = new HashMap(); + + String[] answer = {}; + String answerStream = ""; + for(String str : record) { + String[] messageContent = str.split(" "); + String postFix = ""; + if(messageContent[0].equals("Leave")){ + postFix = "나갔습니다."; + } else if(messageContent[0].equals("Change")) { + uidMap.replace(messageContent[1], messageContent[2]); + } else { + postFix = "들어왔습니다."; + uidMap.put(messageContent[1], messageContent[2]); + } + + if(!messageContent[0].equals("Change")) { + answerStream += messageContent[1]+"님이 "+ postFix + ", "; + } + } + + for( String key : uidMap.keySet() ){ + System.out.println(uidMap.get(key)); + System.out.println(answerStream); + String reg = key.toString(); + answerStream.replaceAll(key, uidMap.get(key)); + } + + answer = answerStream.split(","); + + return answer; + } + + public int[] solution6(int m, int n, int[][] picture) { + int numberOfArea = 0; + int maxSizeOfOneArea = 0; + TreeMap map = new TreeMap<>(); + + int[] answer = new int[2]; + answer[0] = numberOfArea; + answer[1] = maxSizeOfOneArea; + + for(int x = 0; x < m; x++) { + for(int y = 0; y < n; y++) { + + if(picture[x][y] != 0) { + + } + } + } + + + return answer; + } + + public String solution7(String number, int k) { + + String answer = ""; + String[] arr = number.split("\\[0-9]{1}"); + + Arrays.stream(arr).forEach(s -> System.out.println(s)); + + + return answer; + } + + public int solution8(String s) { + int answer = 0; + + HashMap aMap = new HashMap<>(); + String[] str = s.split(""); + + for(int i = 0; i < str.length ; i++) { + if(!aMap.containsKey(str[i])) { + aMap.put(str[i], 0); + } else { + aMap.replace(str[i], aMap.get(str[i]) +1 ); + } + } + + for(String key : aMap.keySet()) { + if(aMap.get(key) % 2 == 1) { + answer++; + } + } + + System.out.println(answer); + return answer; + } + + public String solution9(int n) { + String answer = ""; + String[] str = new String[3]; + int[] b = new int[9999999]; + str[0] = "4"; + str[1] = "1"; + str[2] = "2"; + + int i = 0; + while(n > 0){ + int remainder = n % 3; + n /= 3; + + if(remainder == 0) n--; + + answer += str[remainder] + answer; + } + + return answer; + } + + public int solution10(int n) { + int answer = 0; + + int[] clapChecker = new int [n+1]; + + for(int i = 1 ; i <= n; i++) { + String v = Integer.toString(i); + System.out.println(i+" : " + v.matches(".*[369].*")); + + if(v.matches(".*[369].*")) { + clapChecker[i] = 1; + } else { + clapChecker[i] = 0; + } + } + + answer = Arrays.stream(clapChecker).sum(); + + return answer; + } + + public int solution11(int[] success) { + int answer = Integer.MAX_VALUE; + HashMap map = new HashMap(); + + for(int i = 0; i < success.length; i++) { + if(map.containsKey(Integer.toString(success[i]))) { + int[] org = map.get(Integer.toString(success[i])); + if(org[1] == -1) { + org[1] = i; + map.replace(Integer.toString(success[i]), org); + } + } else { + map.put(Integer.toString(success[i]), new int[] {i,-1}); + } + } + + for (String key : map.keySet()) { + int[] a = map.get(key); + System.out.println(key + " / " + a[0] + ": "+ a[1]); + if(a[1] != -1) { + answer = answer > a[1]-a[0] == true ? a[1] - a[0] : answer; + } + } + + if(answer == Integer.MAX_VALUE) { + answer = -1; + } + + return answer; + } + + public int solution99(int k, String[] user_scores) { + + int answer = 0; + + LinkedList rankList = new LinkedList(); + String[] snapShot = new String[k]; + for(int i = 0; i < k; i++) { + rankList.add(new String[] {" ","0"}); + } + + //K는 1이상 100이하인 자연수 + //user_scores의 길이는 1 이상 1000 이하 + //user_scores -> 닉네임 점수 + //바뀐다 -> 닉네임 + int z = 0; + for(String s : user_scores) { + //랭크 정보 변경 전 스냅샷 + for(int i = 0; i < k; i++) { + String[] rankUser = rankList.get(i); + snapShot[i] = rankUser[0].toString(); + } + + //읽어들인 유저 정보 스캔 + String[] user = s.split(" "); + String userName = user[0]; + int userScore = Integer.parseInt(user[1]); + + if(rankList.size() == 0) { + rankList.add(user); + } else { + for(int i = 0; i < k; i++) { + String[] rankUser = rankList.get(i); + int rankUserScore = Integer.parseInt(rankUser[1]); + + if(rankUserScore < userScore) { + + //랭크에 이미 있는 경우 -> 구 랭크를 지워준다. + boolean needRemove = false; + int removeIndex = -1; + for(int j = 0; j < k; j++) { + String[] tmpUser = rankList.get(j); + int tmpRankUserScore = Integer.parseInt(tmpUser[1]); + + + if(tmpUser[0].equals(userName) && tmpRankUserScore < userScore) { + needRemove = true; + removeIndex = j; + break; + } + } + + if(needRemove) { + //System.out.println("needRemove" + removeIndex); + rankList.remove(removeIndex); + rankList.add(i, user); + } else { + ArrayList alreadyRankUser = (ArrayList) rankList.stream().filter(t -> t[0].equals(userName) && Integer.parseInt(t[1]) >= userScore).collect(Collectors.toList()); + + //alreadyRankUser.forEach((t) -> {System.out.println(t[0]);}); + if(alreadyRankUser.size() == 0) { + rankList.add(i, user); + } + } + //rankList.remove(k); + break; + } + } + + } + + //랭크 정보 변경 전 스냅샷 + for(int i = 0; i < k; i++) { + String[] rankUser = rankList.get(i); + if(!snapShot[i].equals(rankUser[0].toString())) { + answer++; + break; + } + } +// System.out.println("========================= i : " + z++); +// rankList.forEach((t) -> { +// System.out.println(t[0] + "/ "+ t[1]); +// }); +// +// +// System.out.println("========================="); +// + } + + return answer; + } + + //정렬 + public String solution13(int[] numbers) { + + // 숫자를 문자열로 변환 + String[] result = new String[numbers.length]; + for (int i = 0; i < numbers.length; i++) { + result[i] = String.valueOf(numbers[i]); + } + + // 정렬 + Arrays.sort(result, new Comparator() { + + @Override + public int compare(String o1, String o2) { + System.out.println((o2 + o1).compareTo(o1 + o2)); + return ((o2 + o1).compareTo(o1 + o2)); + } + }); + + // 0만 여러개 있는 배열의 경우 하나의 0만 리턴 + if(result[0].equals("0")) { + return "0"; + } + + String answer = ""; + // 정렬된 문자 하나로 합치기 + for (String a : result) { + answer += a; + } + return answer; + + } + + public int solution14(int[] people, int limit) { + + //구명보트 앞뒤로 태우기 ----- 탐욕법 + int answer = 0; + + Arrays.sort(people); + + int right = people.length - 1; + int left = 0; + + while (right >= left) { + int weight = people[right--]; + if (weight + people[left] <= limit) { + left++; + } + answer++; + } + + return answer; + } + + public int solution15(String name) { + int answer = 0; + String[] arr = name.split(""); + int[] resetStack = {1,2,3,4,5,6,7,8,9,10,11,12,13,13,12,11,10,9,8,7,6,5,4,3,2,1}; + int currentPoint = 0; + + for(int i = 0; i < arr.length ; i++) { + int targetPoint = arr[i].codePointAt(0)-65; + System.out.println("move To :" + arr[i]); + System.out.println(Math.abs(targetPoint-currentPoint)); + System.out.println(resetStack[targetPoint]); + answer += Math.min(Math.abs(targetPoint-currentPoint), resetStack[targetPoint]); + if(i != arr.length-1) { + answer++; + } + } + + return answer; + } + + + public String[] solution(String[] movie) { + + String[] ans = {}; + HashMap map = new HashMap(); + + for (String m : movie) { + if(map.containsKey(m)) { + Integer orgCount = map.get(m); + map.replace(m, orgCount+1); + } else { + map.put(m, 1); + } + } + + List tmpList = new ArrayList(); + + tmpList.addAll(map.keySet()); + + Collections.sort(tmpList,new Comparator() { + public int compare(Object o1,Object o2) { + Object v1 = map.get(o1); + Object v2 = map.get(o2); + + if(v1.equals(v2)) { + return ((Comparable) o1).compareTo(o2); + } + + return ((Comparable) v2).compareTo(v1); + } + }); + + ans = tmpList.toArray(new String[0]); + + return ans; + } + + public int solution00(int[] user_times, int T) { + int answer = 0 ; + + for (int time : user_times) { + + int multiple = (int) Math.ceil((float)time/T); + int remainTime = multiple * T - time; + answer = Math.max(answer, remainTime); + } + + return answer; + } + + + public String[] solution01(String[] rooms, int target) { + + String[] answer = {}; + + HashMap> roomMap = new HashMap>(); + HashMap> humanMap = new HashMap>(); + + for( String s : rooms) { + + String roomNum = s.substring(s.indexOf("[")+1,s.indexOf("]")); + String[] nameList = s.substring(s.indexOf("]")+1).split(","); + + //System.out.println(roomNum); + //Arrays.stream(nameList).forEach(t -> System.out.println(t)); + + roomMap.put(roomNum, new ArrayList(Arrays.asList(nameList))); + + for(String h : nameList) { + ArrayList list = humanMap.getOrDefault(h, new ArrayList()); + list.add(roomNum); + humanMap.put(h, list); + } + + } + + HashMap> resultMap = new HashMap>(); + + + //우선 순위 고려 + //1. 호수 filtering + int min = 99999; + PriorityQueue pq = new PriorityQueue(new Comparator() { + @Override + public int compare(String o1, String o2) { + int a = Math.abs(target - Integer.parseInt(o1)); + int b = Math.abs(target - Integer.parseInt(o2)); + + return ((Comparable) a).compareTo(b); + } + }); + + for( String s : roomMap.keySet()) { + if(Math.abs(target - Integer.parseInt(s)) != 0) { + pq.add(s); + } + } + + PriorityQueue> pqh = new PriorityQueue>(new Comparator>() { + + @Override + public int compare(HashMap o1, HashMap o2) { + + int size1 = Integer.parseInt(o1.get("size")); + int size2 = Integer.parseInt(o2.get("size")); + String name1 = o1.get("name"); + String name2 = o2.get("name"); + + + if(size1 != size2) { + return ((Comparable) size1).compareTo(size2); + } else { + return ((Comparable) name1).compareTo(name2); + } + + } + + }); + + + for( String s : humanMap.keySet()) { + + if(humanMap.get(s).contains(String.valueOf(target))) { + } else { + HashMap tmp = new HashMap(); + tmp.put("name", s); + tmp.put("size", String.valueOf(humanMap.get(s).size())); + pqh.add(tmp); + } + } + + answer = new String[pqh.size()]; + + int i = 0; + + for (HashMap hashMap : pqh) { + answer[i++] = (hashMap.get("name")); + } + + + Arrays.stream(answer).forEach(t -> System.out.println(t)); + + return answer; + } + + static String solution(String s) { + String answer = ""; + + String[] list = s.split(" ",0); + + for(int i = 0; i < list.length; i++) { + list[i] = list[i].toLowerCase(); + if(isStringInt(list[i].substring(0,1))) { + //do Nothing + } else { + list[i] = list[i].substring(0,1).toUpperCase() + list[i].substring(1); + } + } + StringBuilder sb = new StringBuilder(); + + for(int i = 0; i < list.length; i++) { + if(i != 0) { + sb.append(" "+list[i]); + } else { + sb.append(list[i]); + } + } + + + return answer; + } + + static boolean isStringInt(String a) { + try { + Integer.parseInt(a); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + + static int solution03(int N, int number) { + + int answer = 0; + + List> listSet = new ArrayList>(); + + for (int i = 0; i < 9; i++) { + listSet.add(new HashSet()); + } + + listSet.get(1).add(N); + + + for (int i = 2; i < 9; i++) { + Set countSet = new HashSet(); + + for(int j = 1; j < 9; j++) { + Set pre = listSet.get(j); + Set post = listSet.get(i-j); + + for(Integer p : pre) { + for(Integer q : post) { + countSet.add(p+q); + countSet.add(p-q); + countSet.add(p*q); + if(p != 0 && q != 0) { + countSet.add(p/q); + } + } + } + } + countSet.add(Integer.parseInt(String.valueOf(N).repeat(i))); + } + + for(Set sub : listSet){ + if(sub.contains(number)) + return listSet.indexOf(sub); + } + + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/43105 + static int solution04(int[][] triangle) { + int answer = 0; + + int[][] addedTriangle = new int[triangle.length][triangle.length]; + + addedTriangle[0][0] = triangle[0][0]; + + for(int i = 1 ; i < triangle.length; i++) { + for(int j = 0; j < triangle[i].length; j++) { + + if(j == 0) { + addedTriangle[i][j] = triangle[i][j] + addedTriangle[i-1][0]; + } else if (j == triangle[i].length -1 ) { + addedTriangle[i][j] = triangle[i][j] + addedTriangle[i-1][j-1]; + } else { + addedTriangle[i][j] = triangle[i][j] + Math.max(addedTriangle[i-1][j-1], addedTriangle[i-1][j]); + } + } + } + + int tmp = -1; + int lastLine = triangle.length-1; + for(int i = 0 ; i < triangle[lastLine].length; i++) { + if (tmp < addedTriangle[lastLine][i]) { + tmp = addedTriangle[lastLine][i]; + } + } + + answer = tmp; + + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42898 + public int solution04(int m, int n, int[][] puddles) { + int answer = 0; + + int[][] countArr = new int[n][m]; + + countArr[0][0] = 0; + + for(int[] list : puddles){ + countArr[list[1]-1][list[0]-1] = -1; + } + + for(int i = 0; i < n ; i++){ + for(int j = 0; j < m ; j++){ + if(countArr[i][j] == -1) { + countArr[i][j] = 0; + continue; + } else { + if( (i == 0 && j == 1) || (i == 1 && j == 0)) { + countArr[i][j] = 1; + continue; + } + + if( i == 0 && j > 1) { + countArr[i][j] = countArr[i][j-1]; + continue; + } + if( j == 0 && i > 1) { + countArr[i][j] = countArr[i-1][j]; + continue; + } + if( j >= 1 && i >= 1) { + // System.out.println("i : " + i + " j : " + j); + // System.out.println(countArr[i-1][j] + " / " + countArr[i][j-1]); + countArr[i][j] = (countArr[i-1][j] + countArr[i][j-1]) % 1000000007; + } + } + } + } + + // for(int i = 0; i < n; i++) { + // for(int j = 0; j < m; j++) { + // System.out.print(countArr[i][j] + " "); + // } + // System.out.println(); + // } + + return countArr[n-1][m-1] % 1000000007; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/49189 + public int solution05(int n, int[][] edge) { + + int answer = 0; + int check[] = new int[n]; + + List> list = new ArrayList<>(); + + list.add(new ArrayList()); + + for(int[] i : edge) { + list.get(i[0]).add(i[1]); + } + + Queue q = new LinkedList<>(); + + + + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/43238 + public long solution06(int n, int[] times) { + + long answer = Long.MAX_VALUE; + + Arrays.sort(times); + + long start = times[0]; // 1분 부터 + long end = (long)times[times.length-1]*(long)n; //times에는 심사대 마다 걸리는 시간이 걸려있으니 최대시간 = 가장오래걸리는시간 * 인원수 + + long mid ; + long sum = 0; + while(start<=end) { + mid= (start+end)/2; + sum = 0; + for(int time : times) { + sum+= mid/time; + } + + if(sum>=n) { + answer = Math.min(answer, mid); + end = mid-1; + } + else { + start = mid+1; + } + } + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42860 + public int solution07(String name) { + int answer = 0; + int length = name.length(); + + int index; // 다음 값들을 확인할 때 사용 + int move = length - 1; // 좌우 움직임 수를 체크 + + for(int i = 0; i < name.length(); i++){ + answer += Math.min(name.charAt(i) - 'A', 'Z' - name.charAt(i) + 1); + + index = i + 1; + // 연속되는 A 갯수 확인 + while(index < length && name.charAt(index) == 'A'){ + index++; + } + + move = Math.min(move, i * 2 + length - index); + + move = Math.min(move, (length - index) * 2 + i); + } + return answer + move; + } + + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42883 + public String solution08(String number, int k) { + StringBuilder answer = new StringBuilder(""); + int len = number.length() - k; + int start = 0; + + while(start < number.length() && answer.length() != len) { + int leftNum = k + answer.length() + 1; + int max = 0; + for (int j = start; j < leftNum; j++) { + if (max < number.charAt(j) - '0') { + max = number.charAt(j) - '0'; + start = j + 1; + } + } + answer.append(Integer.toString(max)); + } + return answer.toString(); + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42885 + public int solution09(int[] people, int limit) { + List list = new ArrayList<>(); + for(int x : people) list.add(x); + Collections.sort(list); + ArrayDeque dq = new ArrayDeque<>(50505); + for(int x : list) dq.add(x); + + int ans = 0; + while(dq.isEmpty() == false) { + int weight = dq.pollLast(); + if(dq.isEmpty() == false && weight + dq.peekFirst() <= limit) { dq.pollFirst(); } + ans++; + } + + return ans; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42627 + public int solution10(int[][] jobs) { + int answer = 0; + + Arrays.sort(jobs, (o1, o2) -> o1[0] - o2[0]); + + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]); + + int jobsIdx = 0; + int count =0; + int end = 0; + + while(count < jobs.length) { + while(jobsIdx < jobs.length && jobs[jobsIdx][0] <= end) { + System.out.println(jobs[jobsIdx][0] + " " + jobs[jobsIdx][1]); + + pq.add(jobs[jobsIdx++]); + } + + if(pq.isEmpty()) { + end = jobs[jobsIdx][0]; + } else { + int[] temp = pq.poll(); + answer += temp[1] + end - temp[0]; + end += temp[1]; + count++; + } + } + + return (int) Math.floor(answer / jobs.length); + + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42628 + public int[] solution11(String[] operations) { + int[] answer = new int[2]; + + PriorityQueue maxPq = new PriorityQueue<>(Collections.reverseOrder()); + PriorityQueue minPq = new PriorityQueue<>(); + + List addedList = new ArrayList(); + List removedList = new ArrayList(); + + int maxDelCount = 0; + int minDelCount = 0; + + for(String s : operations) { + System.out.println(s); + + String order = s.split(" ")[0]; + int val = Integer.parseInt(s.split(" ")[1]); + + if(order.equals("I")) { + System.out.println("Insert => " + s.split(" ")[1]); + + addedList.add(Integer.parseInt(s.split(" ")[1])); + maxPq.add(val); + minPq.add(val); + + } else if (order.equals("D") && val == 1) { + System.out.println("Delete Max => " + maxPq.peek()); + if(!maxPq.isEmpty()) { + minPq.remove((maxPq.poll())); + + } + + } else if (order.equals("D") && val == -1) { + System.out.println("Delete Min =>" + minPq.peek()); + if(!minPq.isEmpty()) { + maxPq.remove(minPq.poll()); + } + } + } + + + answer[0] = maxPq.isEmpty() ? 0 : maxPq.peek(); + answer[1] = minPq.isEmpty() ? 0 : minPq.peek(); + + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42840 + public int [] solution12(int[] answers) { + int[] answer = {}; + + int[] person1 = new int[] {1,2,3,4,5}; + int[] person2 = new int[] {2,1,2,3,2,4,2,5}; + int[] person3 = new int[] {3,3,1,1,2,2,4,4,5,5}; + + int correct1 = 0; + int correct2 = 0; + int correct3 = 0; + + for(int i = 0; i < answers.length; i++) { + if(answers[i] == person1[i % (person1.length)]) { + correct1++; + } + if(answers[i] == person2[i % (person2.length)]) { + correct2++; + } + if(answers[i] == person3[i % (person3.length)]) { + correct3++; + } + } + + int max = Math.max(correct1, correct2); + max = Math.max(max, correct3); + List list = new ArrayList(); + + if(correct1 == max) { + list.add(1); + } + if(correct2 == max) { + list.add(2); + } + if(correct3 == max) { + list.add(3); + } + + list.sort((o1, o2) -> o1.compareTo(o2)); + + answer = list.stream().mapToInt(i->i).toArray(); + + System.out.println(correct1); + System.out.println(correct2); + System.out.println(correct3); + + Arrays.stream(answer).forEach(s->System.out.println(s)); + + return answer; + } + + //@Link : https://programmers.co.kr/learn/courses/30/lessons/42842 + public int[] solution13(int brown, int yellow) { + int[] answer = new int[2]; + + List arr = new ArrayList(); + int n = yellow; + int sqrt = (int) Math.sqrt(n); + + for(int i = 1; i<= sqrt; i++) { + if( n % 1 == 0) { + arr.add(i); + if(n / i != i) { + arr.add(n / i); + } + + } + } + + arr.sort(Comparator.naturalOrder()); + + for(int i : arr) { + int vertical = i; + int horizontal = yellow / i; + + if(yellow%vertical == 0 && brown == ((int)vertical+2) * ((int)horizontal+2) - yellow) { + answer[0] = horizontal+2; + answer[1] = vertical+2; + break; + } + + } + + return answer; + } + +} + + diff --git a/src/codingTest/module2.java b/src/codingTest/module2.java new file mode 100644 index 0000000..30a650c --- /dev/null +++ b/src/codingTest/module2.java @@ -0,0 +1,251 @@ +package codingTest; + +import java.util.*; +import java.util.stream.Collectors; + +public class module2 { + + public static void main(String[] args) { + + //문제풀이 기본 양식 + Solution s = new Solution(); + String[] answer = {}; + + + // + String[] rooms= {"[1234]None,Of,People,Here", "[5678]Wow"}; + int target = 1234; + + + System.out.println(s.solution01(new String[] {"I 16","D 1"})); + System.out.println(s.solution02(new String[] {"I 16","D 1"})); + System.out.println(s.solution03(new String[] {"I 16","D 1"})); + + } + + public static void tmpArchive() { + + //int array + int[] arr1 = new int [1]; + int[] arr2 = {}; + String[] arr3 = {}; + + Arrays.sort(arr1); //오름차순 + Arrays.stream(arr1).sum(); //합계 + Arrays.stream(arr1).filter(s -> true); //filter + + //stream filter 후 list 반환 + //ArrayList alreadyRankUser = (ArrayList) arr1.stream().filter(t -> t[0].equals(userName) && Integer.parseInt(t[1]) >= userScore).collect(Collectors.toList()); + + List list1 = new ArrayList(); + + + list1.add(new Integer[][] {{1,3}}); + + //List + List list2 = new ArrayList(); + int index1 = 0; + list2.add(index1, 0); + list2.remove(index1); + for (Integer integer : list2) { + System.out.println(integer); + } + + //List Sort + Collections.sort(list2,new Comparator() { + public int compare(Object o1,Object o2) { +// Object v1 = map.get(o1); +// Object v2 = map.get(o2); +// +// if(v1.equals(v2)) { +// return ((Comparable) o1).compareTo(o2); +// } + + return ((Comparable) o1).compareTo(o2); + } + }); + + //map 선언 -> key 중복 x + HashMap map = new HashMap(); + + HashMap> ma2 = new HashMap>(); + + //ma2.put("aa", new List); + + for(String key : map.keySet()) { + + } + + //HashSet -- 중복 미허용 + Set set1 = new HashSet(); + + + + + //Stack + Stack stack = new Stack(); + stack.add(null); //넣기 + stack.empty(); //비었는지 확인 + stack.peek(); //stack 최상단 값 + stack.pop(); //stack 빼기 + + //Queue + Queue queue = new LinkedList<>(); + queue.add(null); //넣기 + queue.isEmpty(); //비었는지 확인 + queue.peek(); //queue 나올 값 + queue.poll(); //queue 빼기 + + //Deque + ArrayDeque dq = new ArrayDeque<>(50505); + dq.isEmpty(); + dq.add(null); + dq.addFirst(null); + dq.addLast(null); + dq.peekFirst(); + dq.peekLast(); + dq.pollFirst(); + dq.pollLast(); + + //PriorityQueue + PriorityQueue maxPq = new PriorityQueue(Collections.reverseOrder()); //최대우선순위큐 + PriorityQueue minPq = new PriorityQueue(); //최소우선순위큐 + PriorityQueue> pqh = new PriorityQueue>(new Comparator>() { + + @Override + public int compare(HashMap o1, HashMap o2) { + + int size1 = Integer.parseInt(o1.get("size")); + int size2 = Integer.parseInt(o2.get("size")); + String name1 = o1.get("name"); + String name2 = o2.get("name"); + + + if(size1 != size2) { + return ((Comparable) size1).compareTo(size2); + } else { + return ((Comparable) name1).compareTo(name2); + } + + } + + }); + + + maxPq.add(null); + maxPq.peek(); //head + maxPq.poll(); //빼기 + maxPq.isEmpty(); //비었는지 + maxPq.remove(); //삭제 + + //변환 + //List -> int[]; + int[] arr99 = {}; + List list99 = new ArrayList(); + arr99 = list99.stream().mapToInt(i->i).toArray(); + + //int[] -> List + int[] arr100 = {}; + List list100 = Arrays.stream(arr100).boxed().collect(Collectors.toList()); + + //Integer[] -> List + Integer[] arr101 = {}; + List list101 = Arrays.asList(arr101); + + + //String + String str1 = "ABC"; + str1.charAt(1); + + + + + } + + public static boolean isPrime(int num){ + for(int i=2; i<=num/2; i++){ + if(num % i == 0) return false; + } + return true; + } + + + static ArrayList[] a; + static boolean[] c; + public static void dfs(int x) { + if (c[x]) { + return; + } + c[x] = true; + System.out.print(x + " "); + for (int y : a[x]) { + if (c[y] == false) { + dfs(y); + } + } + } + public static void bfs(int start) { + Queue q = new LinkedList(); + q.add(start); + c[start] = true; + while (!q.isEmpty()) { + int x = q.remove(); + System.out.print(x + " "); + for (int y : a[x]) { + if (c[y] == false) { + c[y] = true; + q.add(y); + } + } + } + } + + public boolean BinarySearch(int []arr, int len, int key){ + int start = 0; + int end = len-1; + int mid; + + while(end - start >= 0) { + mid = (start + end) / 2; //중앙 값 + + if (arr[mid] == key) { //key값을 찾았을때 + return true; + + } else if (arr[mid] > key) { //key값이 mid의 값보다 작을때 (왼쪽으로) + end = mid - 1; + + } else { //key값이 mid의 값보다 클때 (오른쪽으로) + start = mid + 1; + + } + } + return false; + } + + static boolean isStringInt(String a) { + try { + Integer.parseInt(a); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + + +} + +class Solution { + + public static void solution01() { + + } + + public static void solution02() { + + } + + public static void solution03() { + + } + +} \ No newline at end of file diff --git a/src/codingTest/module3.java b/src/codingTest/module3.java new file mode 100644 index 0000000..71690dd --- /dev/null +++ b/src/codingTest/module3.java @@ -0,0 +1,285 @@ +package codingTest; + +import java.util.*; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +public class module3 { + + public static void main(String[] args) { + + //문제풀이 기본 양식 + Solution s = new Solution(); + String[] answer = {}; + + + // + String[] rooms= {"[1234]None,Of,People,Here", "[5678]Wow"}; + int target = 1234; + + +// System.out.println(s.solution01(new String[] {"I 16","D 1"})); +// System.out.println(s.solution02(new String[] {"I 16","D 1"})); + + + //System.out.println(s.solution03(new int[] {2,3,1,4}, 3)); + //System.out.println(s.solution03(new int[] {1,2,3,4}, 5)); +// System.out.println(s.solution06(new int[] {2,3,1,4}, 3)); + System.out.println(s.solution06(new int[] {1,2,3,4}, 5)); +// System.out.println(s.solution06(new int[] {1,2,3,4}, 20)); + + } + +} + +class Solution { + + public static int[][] map; + public static boolean[] visited; + public static int max; + public static int n; + + +public static int solution01(int[][] board) { + int answer = 0; + + int m = board[0].length; // 가로 + int n = board.length; // 세로 + int sum = 0; // 전체 타일 과자 총합 + int min = 1001; // 포기할 타일 중 최소값 + + //전체 총합 구하기 + for (int[] line : board) { + sum += Arrays.stream(line).sum(); + } + + //전부 갈 수 있는 경우, 총합 리턴 + if(m % 2 == 1 || n % 2 == 1) { + return sum; + } + + //못 가는 타일 1개 고르기 + for(int i = 0; i < n; i++) { + for(int j = 0; j hm = new HashMap<>(); + + + for (int i=0 ; i queue = new LinkedList(); + + int sum = Arrays.stream(play_list).sum(); + + if(sum < listen_time) { + return play_list.length; + } + + int maxCount = 0; + int tmpSum = 0; + int count = 0; + for(int i = 0; i < play_list.length ; i++) { + System.out.println("start ============ : " + i ); + int j = i; + count = 0; + tmpSum = 0; + while(true) { + if(j == i) { + tmpSum += 1; + count++; + } else { + System.out.println("add : " + play_list[j%play_list.length]); + tmpSum += play_list[j%play_list.length]; + count++; + } + j++; + System.out.println(tmpSum); + + if(tmpSum > listen_time) { + break; + } + } + + maxCount = Math.max(maxCount, count); + + System.out.println("max : " + maxCount); + } + + answer = maxCount; + return answer; + + } + + + public int solution04(int[] play_list, int listen_time) { + + int answer = 0; + + int sum = Arrays.stream(play_list).sum(); + + if(listen_time <= 2) { + return Math.min(play_list.length, listen_time); + } + + if(sum < listen_time) { + return play_list.length; + } + + int maxCount = 0; + int tmpSum = 0; + int count = 0; + HashSet set = new HashSet<>(); + for(int i = 0; i < play_list.length ; i++) { + System.out.println("start ============ : " + i + " / " + play_list[i]); + int j = i; + count = 0; + tmpSum = 0; + set = new HashSet<>(); + while(true) { + if(j == i) { + tmpSum += 1; + } else { + System.out.println("add : " + play_list[j%play_list.length]); + tmpSum += play_list[j%play_list.length]; + } + j++; + System.out.println(tmpSum); + set.add(j%play_list.length); + if(tmpSum > listen_time) { + break; + } + } + + maxCount = Math.max(maxCount, set.size()); + + System.out.println("max : " + maxCount); + } + + answer = maxCount; + return answer; + + } + + public int solution05(int[] play_list, int listen_time) { + int answer = 0; + + int sum = Arrays.stream(play_list).sum(); + Queue queue = new LinkedList(); + + if(listen_time <= 2) { + return Math.min(play_list.length, listen_time); + } + + if(sum < listen_time) { + return play_list.length; + } + + for(int i = 0; i < play_list.length; i++) { + + } + + + return answer; + } + + + public int solution06(int[] play_list, int listen_time) { + + int answer = 0; + + int[] s = new int[play_list.length]; //s = 그냥 앞에서부터 음악시간 더한 배열 + int s2 = 0; + int s3 = 0; + + s[0] = play_list[0]; + + for(int i = 1; i < play_list.length; i++) { + s[i] = play_list[i-1] + play_list[i]; + } + + int start = 0; + int end = play_list.length-1; + int mid = 0; + + boolean flag = false; + + while(start<=end) { + mid = (start+end)/2; + flag = false; + System.out.println("start : " + start + " mid : " + mid + " end : " + end); + + for(int i = answer; i <= end ;i++){ + if(i-1 > -1) { + s2 = s[i-1] + 1; + } else { + s2 = 1; + } + s3 = s[i-answer] - (play_list[i] > 1 ? 1 : 0); + + if(s2 - s3 <= listen_time) { + start = mid + 1; + + if(mid >= answer) { + answer= mid; + } + flag=true; + break; + } + } + + if(!flag){ + end=mid-1; + } + + } + + return answer; + } + + +} \ No newline at end of file diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..62dc43e --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,2 @@ +module codingTest { +} \ No newline at end of file