diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f00af9b --- /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/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/Card.class b/bin/codingTest/Card.class new file mode 100644 index 0000000..6d01471 Binary files /dev/null and b/bin/codingTest/Card.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$1.class b/bin/codingTest/Solution$1.class new file mode 100644 index 0000000..513a042 Binary files /dev/null and b/bin/codingTest/Solution$1.class differ diff --git a/bin/codingTest/Solution$2.class b/bin/codingTest/Solution$2.class new file mode 100644 index 0000000..82701e3 Binary files /dev/null and b/bin/codingTest/Solution$2.class differ diff --git a/bin/codingTest/Solution$3.class b/bin/codingTest/Solution$3.class new file mode 100644 index 0000000..578bfe7 Binary files /dev/null and b/bin/codingTest/Solution$3.class differ diff --git a/bin/codingTest/Solution$4.class b/bin/codingTest/Solution$4.class new file mode 100644 index 0000000..47e733a Binary files /dev/null and b/bin/codingTest/Solution$4.class differ diff --git a/bin/codingTest/Solution.class b/bin/codingTest/Solution.class new file mode 100644 index 0000000..0666773 Binary files /dev/null and b/bin/codingTest/Solution.class differ diff --git a/bin/codingTest/module.class b/bin/codingTest/module.class new file mode 100644 index 0000000..740b31b Binary files /dev/null and b/bin/codingTest/module.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..22e8582 --- /dev/null +++ b/src/codingTest/module.java @@ -0,0 +1,857 @@ +package codingTest; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Queue; +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.solution01(rooms, target)); + +// 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; + } +} + + 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