Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/com/example/algorithmstudy/week19/MyAnswer055.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.algorithmstudy.week19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

//백준 2579번 계단 오르기
public class MyAnswer055 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int stairCount = Integer.parseInt(br.readLine());
int[] stairScore = new int[stairCount + 1];
int[] dp = new int[stairCount + 1];

for (int i = 1; i <= stairCount; i++) {
stairScore[i] = Integer.parseInt(br.readLine());
}

dp[0] = 0;
dp[1] = stairScore[1];

if(stairCount >= 2){
dp[2] = stairScore[1] + stairScore[2];
}

for (int i = 3; i <= stairCount; i++) {
dp[i] = Math.max(dp[i - 2], dp[i - 3] + stairScore[i - 1]) + stairScore[i];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다풀고나서 이거 보니까 하 이사람 깔끔하게 한줄컷했네 생각들었음 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

}

System.out.println(dp[stairCount]);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/example/algorithmstudy/week19/MyAnswer056.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비슷한 풀이라 잘보감치고 도망칠게요

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.algorithmstudy.week19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

//백준 1994번 등차수열
public class MyAnswer056 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCount = Integer.parseInt(br.readLine());
int[] numArr = new int[numCount];

for (int i = 0; i < numCount; i++) {
numArr[i] = Integer.parseInt(br.readLine());
}

Arrays.sort(numArr);

int maxCount = 1;

Map<Integer, Map<Integer, Integer>> dp = new HashMap<>();

for (int i = 0; i < numCount; i++) {
dp.put(i, new HashMap<>());
for (int j = 0; j < i; j++) {
int diff = numArr[i] - numArr[j];
int len = dp.get(j).getOrDefault(diff, 1) + 1;
dp.get(i).put(diff, len);
maxCount = Math.max(maxCount, len);
}
}

System.out.println(maxCount);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/example/algorithmstudy/week19/MyAnswer057.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.algorithmstudy.week19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

//백준 1695번 팰린드롬 만들기
public class MyAnswer057 {
static int numCount;
static int[] numArr;
static int[][] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
numCount = Integer.parseInt(br.readLine());
numArr = new int[numCount];
dp = new int[numCount][numCount];
for (int[] nums : dp) {
Arrays.fill(nums, -1);
}

StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < numCount; i++) {
numArr[i] = Integer.parseInt(st.nextToken());
}

int left = 0;
int right = numCount - 1;
System.out.println(checkPalindrome(left, right));
}
static int checkPalindrome (int left, int right) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 첨에 양끝 잡고
왼쪽에 추가하기 or 오른쪽에 추가하기 대충 이런식으로 풀까하다가
재혁햄 풀이보고 겹치길래 끙끙앓았는데
그냥 이게 더 직관적이긴 한 거 같아요

if(left > right) return 0;

if(dp[left][right] != -1) return dp[left][right];

if(numArr[left] == numArr[right]) {
dp[left][right] = checkPalindrome(left + 1, right-1);
}else{
dp[left][right] = Math.min(checkPalindrome(left + 1, right) + 1, checkPalindrome(left, right-1) + 1);
}

return dp[left][right];
}
}