목록java (14)
JS's IT
무지 간단하다 넘어가자 public class Solution { // Complete the alternatingCharacters function below. static int alternatingCharacters(String s) { int count = 0 ; String tmpS = ""; for(int i = 0 ; i < s.length() ; i++) { if(s.substring(i,i+1).equals(tmpS)) { count++; }else { tmpS = s.substring(i,i+1); } } return count; } private static final Scanner scanner = new Scanner(System.in); public static void main..
아나그램.. 이게 뭔지 이제는 알수있다..easy 난이도! 얼른 넘어가자.. public class Solution { // Complete the makeAnagram function below. static int makeAnagram(String a, String b) { int count = 0; HashMap tmpAna = new HashMap(); for(int i = 0 ; i < a.length() ; i++) { if(tmpAna.get(a.substring(i,i+1)) == null ) { tmpAna.put(a.substring(i,i+1), 1); }else { tmpAna.put(a.substring(i,i+1), tmpAna.get(a.substring(i,i+1))+1); ..
최적화의 답이 안떠오르는 문제였다.. 중앙값을 구하는 로직이 핵심알고리즘 같다 하루하루 마다 d개의 숫자를 꺼내어 중앙값을 계산한 다음 처리를 했었는데..이건 속도가 너무 늦다.. 숫자가 어느정도되면 timeout.. 핵심은 지출되는 숫자가 200까지라는건데.. 200개의 배열을 만든 후 각 지출숫자마다 +1씩 해준다..그리고 0부터 for돌면서 각카운팅 숫자 더하면서 d/2번째의 숫자가 중앙값이다..홀수,짝수일때 처리를 다르게 해준 뒤 구해진 중앙값으로 나머지 처리하면 끝.. 처리 후 배열에서 처음 숫자 카운팅 -1, 다음숫자 카운팅+1 public class Solution { static double CalMedian(int[] tmpArr, int d) {int count = 0;double re..
생각보다..어렵진 않았다..숫자 비교후 1,0,-1 반환하고.. 0일때 글자 비교해서 1,0,-1 반환 해주면 된다.. 금방이네.. class Player {String name;int score; Player(String name, int score) {this.name = name;this.score = score;}} class Checker implements Comparator { // complete this methodpublic int compare(Player a, Player b) { int result = 0;if(a.score b.score) {result = -1;}else {return a.name.toS..
easy라 그런지 쉽게 패스 오름차순 정렬하면서 더하기.. 그러다 예산 넘으면 스탑 public class Solution { // Complete the maximumToys function below. static int maximumToys(int[] prices, int k) { int sum = 0; int count = 0; for(int i = 0 ; i prices[j]) { int tmp = prices[i]; prices[i] = prices[j]; prices[j] = tmp; } } if(sum+prices[i] > k) { ..
이런 문제는 어떻게 내는걸까.. 이해하기 힘든 문제다..한쌍의 숫자배열을 여러개 받아 앞의 숫자는 처리구분 숫자 뒤의 숫자는 입력숫자 Operation Array Output(1,1) [1](2,2) [1](3,2) 0(1,1) [1,1](1,1) [1,1,1](2,1) [1,1](3,2) 1 1이면 입력, 2면 제거, 3이면 주파수?가 정확히 일치하는 정수가 있는지 확인해서 맞으면 1 아니면 0아직도 정확한 이 주파수?라는걸 모르겠다.. 일단 입력된 숫자와 갯수를 hashmap에 묶어서 넣어주고3일때의 숫자가 넣어준 숫자의 갯수와 일치되는것이 있을때 1을 리턴해줬다. 원래는 freqQuery 만 수정하면 무조건 timeout 에러가난다.. (도대체 얼마나 더 줄여야되는건지....)그래서 메인함수 출력부..
간단한 버블소트.. 해커랭크에 올라온 소스로 그냥 간단히 짰다.. public class Solution { // Complete the countSwaps function below. static void countSwaps(int[] a) { int count = 0; for (int i = 0; i a[j + 1]) { int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; count++; tag = false;..
내가 HashMap 활용을 많이 안하고있구나..라고 느꼈던 단계..숫자 갯수와 배수 숫자 하나 그리고 숫자배열이 주어지고.. 숫자배열에서 배수로된 3쌍의 숫자조합이 몇개있냐..를 묻는거같다.. static boolean chkNum(long a,long b,long c,long r) { if(a*r == b) { if(a*r*r == c) { return true; }else { return false; } }else { return false; } } // Complete the countTriplets function below. static long countTriplets(List arr, long r) { int count=0; int aTCnt = 0; HashMap aHTmp = new Ha..
아.. 토나오는 문제였다.. 일단 문제 이해가 어려웠다..아나그램이라니..이게 뭔가 싶었는데.. 한 문자열의 문자가 다른 문자열을 형성하기 위해 재배치 될 수 있다면 두 개의 문자열은 서로의 아나그램 입니다그렇단다.. 그리고 생소한 문제에다가..문자열을 뜯어서 비교 하고 횟수 카운트 해야된다. 일단 입력값중 도움이 되었던것은 입력값: 1abba 입력값:1kkkk 두개가 도움이 되었다.abba 는 [a,a], [b,b], [ab,ba], [abb,bba] 네개가 나오고kkkk는 같은 k라도 위치값에 따라 다른 k로 취급해야된다. 따라서 [k,k] 6개와 [kk,kk] 3개와 [k,k,k] 1개가 나온다. 일단 문자열 2개를 비교하는 compareTo 함수를 만들었다. 문자 2개의 비교이지만 갯수는 같아야 ..
단순 string 비교, 하나의 string 을 다른 string 과 포함되어있는지 여부만 나타나면 된다.금방 끝날....줄 알았는데... timeout 이 떠서 당황했다...(항상 timeout이 나오는건 기분탓인가..) string 하나를 for돌면서 나머지 문자 contains하는데 contains 다돌면 timeout 떨어진다..hashmap 하나둬서 했던 문자면 pass public class Solution { // Complete the twoStrings function below. static String twoStrings(String s1, String s2) { String result = "NO"; HashMap sameChk = new HashMap(); for(int i = 0..