프로그래머스 - 땅따먹기 게임

dp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import java.util.Arrays; public class Hopscotch { int hopscotch(int[][] board, int size) { for(int i=1;i<size;i++) { board[i][0] += board[i-1][1] > board[i-1][2] ? (board[i-1][1] > board[i-1][3] ? board[i-1][1] : board[i-1][3]) : board[i-1][2] > board[i-1][3] ? board[i-1][2] : board[i-1][3]; board[i][1] += board[i-1][0] > board[i-1][2] ? (board[i-1][0] > board[i-1][3] ? board[i-1][0] : board[i-1][3]) : board[i-1][2] > board[i-1][3] ? board[i-1][2] : board[i-1][3]; board[i][2] += board[i-1][0] > board[i-1][1] ? (board[i-1][0] > board[i-1][3] ? board[i-1][0] : board[i-1][3]) : board[i-1][1] > board[i-1][3] ? board[i-1][1] : board[i-1][3]; board[i][3] += board[i-1][0] > board[i-1][1] ? (board[i-1][0] > board[i-1][2] ? board[i-1][0] : board[i-1][2]) : board[i-1][1] > board[i-1][2] ? board[i-1][1] : board[i-1][2]; } Arrays.sort(board[size-1]); return board[size-1][3]; } public static void main(String[] args) { Hopscotch c = new Hopscotch(); int[][] test = { { 1, 2, 3, 5 }, { 5, 6, 7, 8 }, { 4, 3, 2, 1 } }; //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(c.hopscotch(test, 3)); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 멀리 뛰기

dp dp[i] = dp[i-2] + dp[i-1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class JumpCase { static int[] dp = new int[10000]; public int jumpCase(int num) { int answer = 0; dp[0] = 1; dp[1] = 2; for(int i=2;i<num;i++) { dp[i] = dp[i-2]+dp[i-1]; } return dp[num-1]; } public static void main(String[] args) { JumpCase c = new JumpCase(); int testCase = 4; //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(c.jumpCase(testCase)); } } 자연스레 dp로 풀었는데 재귀적으로 푼 코드가 있어서 풀이첨부 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 문자열 내림차순으로 배치하기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.Arrays; public class ReverseStr { public String reverseStr(String str){ String ans = ""; char[] temp = str.toCharArray(); Arrays.sort(temp); for(int i=0;i<temp.length;i++) { ans+=temp[temp.length-1-i]; } return ans; } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { ReverseStr rs = new ReverseStr(); System.out.println( rs.reverseStr("Zbcdefg") ); } } 지금 쓰는 방법으로도 풀 수 있긴한데, 스트림버퍼사용법좀 배워야 될듯 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 삼각형 출력하기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class PrintTriangle { public String printTriangle(int num){ String ret = ""; for(int i=0;i<num;i++) { for(int j=0;j<=i;j++) { ret+="*"; } ret+="\n"; } return ret; } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { PrintTriangle pt = new PrintTriangle(); System.out.println( pt.printTriangle(3) ); } } 아래와 같은 풀이가 있는데 참신하다. 당연히 2중 반복문으로 풀었는데 하나로 풀어내다니.. 배워야겠다. ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 서울에서김서방찾기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class FindKim { public String findKim(String[] seoul){ //x에 김서방의 위치를 저장하세요. int x = 0; for(int i=0;i<seoul.length;i++) { if(seoul[i].equals("Kim")) { x=i; break; } } return "김서방은 "+ x + "에 있다"; } // 실행을 위한 테스트코드입니다. public static void main(String[] args) { FindKim kim = new FindKim(); String[] names = {"Queen", "Tod","Kim"}; System.out.println(kim.findKim(names)); } } 아래와 같은 풀이도 있다. ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 소수 찾기

label이 break, continue와 사용가능해서 좋은 자바:) 좀 더 좋은 소수찾는 방법으로 풀어봐야지 한번 더 시도해봐야지 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class NumOfPrime { int numberOfPrime(int n) { int result = 0; loop1: for(int i=2;i<=n;i++) { for(int j=2;j<i;j++) { if(i%j==0) { continue loop1; } } result++; } // 함수를 완성하세요. return result; } public static void main(String[] args) { NumOfPrime prime = new NumOfPrime(); System.out.println(prime.numberOfPrime(10)); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 숫자의 표현

내 코드 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Expressions { public int expressions(int num) { int sum=0; int first = 0; int cnt=0; for(int i=1;i<=num/2;i++) { sum = 0; first = i; while(true) { sum+=first++; if(sum==num) { cnt++; break; } else if(sum>num) { break; } } } return cnt+1; } public static void main(String args[]) { Expressions expressions = new Expressions(); // 아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(expressions.expressions(15)); } } 이 코드는 뭐냐.. 내 코드가 초라하다 시간날때봐야지 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 스트링을 숫자로 바꾸기

1 2 3 4 5 6 7 8 9 10 11 public class StrToInt { public int getStrToInt(String str) { return Integer.parseInt(str); } //아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String args[]) { StrToInt strToInt = new StrToInt(); System.out.println(strToInt.getStrToInt("-1234")); } } 직접 변환하는 것이 문제의 의도이거 같지만, 그냥 제공되는 메소드를 사용했다. 다음에 parseInt의 소스코드를 분석해 봐야겠다. ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 시저 암호

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Caesar { String caesar(String s, int n) { n%=26; String result = ""; char[] temp = s.toCharArray(); for(int i=0;i<temp.length;i++) { if(temp[i]>='a' &&temp[i]<='z') temp[i] = (char) ((temp[i]+n)>'z' ? (temp[i]+n-1)%'z'+'a' : (temp[i]+n)); else if(temp[i]>='A' &&temp[i]<='Z') temp[i] = (char) ((temp[i]+n)>'Z' ? (temp[i]+n-1)%'Z'+'A' : (temp[i]+n)); result+=temp[i]; } return result; } public static void main(String[] args) { Caesar c = new Caesar(); System.out.println("s는 'a B z', n은 4인 경우: " + c.caesar("a B z", 4)); } } 알파벳의 대소문자를 조건문의 범위로 확인하는 방법 말고 Character.isLowerCase()가 있음 삼항 연산자를 사용한 라인에서 temp[i] = (char) ((temp[i] - ‘a’ + n) % 26 + ‘a’); 로 하면 분기를 줄일 수 있음 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 야근 지수

좀 지저분하긴해도 시간초과 까지 개선한 코드.. N^2으로 풀어도 5초가 넘어서(정답 처리는 됨) 좀더 최적화 해봄 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import java.util.Arrays; public class NoOvertime { public int noOvertime(int no, int[] works) { int result = 0; Arrays.sort(works); int i = works.length-1; Loop1: while(i!=0 && no!=0) { if(works[i]>works[i-1]) { for(int j=i;j<works.length;j++) { if(no==0) { break Loop1; } works[j]--; no--; } continue Loop1; } i--; } while(no!=0) { works[no--%works.length]--; } for(int j=0;j<works.length;j++) { result+=works[j]*works[j]; } return result; } public static void main(String[] args) { NoOvertime c = new NoOvertime(); int []test = {4,3,3}; System.out.println(c.noOvertime(4,test)); } }

12월 23, 2017 · Jaejin Jang