Skip to content

Commit ee6a233

Browse files
Merge pull request #24 from algorithm-cote-study/yeji/leetCode
leetCode : DP
2 parents 2f99476 + e1429fc commit ee6a233

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sgyj.inflearn.yeji.section9;
2+
3+
import java.awt.print.Pageable;
4+
import java.util.Arrays;
5+
import java.util.Scanner;
6+
7+
// 동전교환
8+
public class Solution5 {
9+
private static int n, m;
10+
private static int[] dy;
11+
12+
private static int solution(int n, int[] coins, int result){
13+
Arrays.fill( dy,Integer.MAX_VALUE );
14+
dy[0] = 0;
15+
for(int i=0; i<n; i++){
16+
for(int j=coins[i]; j<=m; j++){
17+
dy[j]=Math.min( dy[j],dy[j-coins[i]]+1 );
18+
}
19+
}
20+
21+
return dy[m];
22+
}
23+
24+
public static void main ( String[] args ) {
25+
Scanner sc = new Scanner( System.in );
26+
int n = sc.nextInt();
27+
int[] arr = new int[n];
28+
for(int i=0; i<n; i++){
29+
arr[i] = sc.nextInt();
30+
}
31+
m = sc.nextInt();
32+
dy = new int[m+1];
33+
System.out.println(solution( n,arr,m ));
34+
}
35+
36+
}

src/main/java/sgyj/leetcode/Readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@
2626

2727
- [200. Number of Islands](https://leetcode.com/problems/number-of-islands/description/)
2828
- [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
29-
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)
29+
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)
30+
31+
## DP
32+
33+
- [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
34+
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
35+
- [322. Coin Change](https://leetcode.com/problems/coin-change/description/)
36+
- [62. Unique Paths](https://leetcode.com/problems/unique-paths/description/)
37+
- [198. House Robber](https://leetcode.com/problems/house-robber/description/)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
// House Robber
4+
public class Solution198 {
5+
private static int[] stillMoney;
6+
7+
public static int rob(int[] nums) {
8+
int answer = 0;
9+
stillMoney = new int[nums.length];
10+
for(int n=0; n<nums.length; n++){
11+
stillMoney[n] = nums[n];
12+
}
13+
14+
for(int n=2; n<nums.length; n++){
15+
int max = 0;
16+
for(int i=n-2; i>=0; i--){
17+
max= Math.max(max,stillMoney[i]);
18+
}
19+
stillMoney[n]+=max;
20+
}
21+
22+
for(int money:stillMoney){
23+
answer = Math.max(answer,money);
24+
}
25+
26+
27+
return answer;
28+
}
29+
30+
public static void main ( String[] args ) {
31+
int[] nums = {1,2,3,1};
32+
33+
System.out.println(rob(nums));
34+
}
35+
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
import java.util.Arrays;
4+
5+
// coin change
6+
public class Solution322 {
7+
8+
public int coinChange(int[] coins, int amount) {
9+
int[] result = new int[amount+1];
10+
Arrays.fill( result, Integer.MAX_VALUE - 1);
11+
result[0] = 0;
12+
13+
for(int coin : coins){
14+
for(int n = coin; n<=amount; n++){
15+
int tmp = result[n-coin] + 1;
16+
if(result[n]>tmp) result[n] = tmp;
17+
}
18+
}
19+
20+
return result[result.length-1] == Integer.MAX_VALUE-1 ? -1 : result[result.length-1];
21+
}
22+
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
// 62. Unique Paths
4+
/*
5+
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]).
6+
The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
7+
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
8+
The test cases are generated so that the answer will be less than or equal to 2 * 109.
9+
*/
10+
11+
public class Solution62 {
12+
private static int[][] visited;
13+
private static int answer = 0;
14+
public static int uniquePaths(int m, int n) {
15+
16+
visited = new int[m][n];
17+
visited[0][0] = 1;
18+
19+
for(int i=0; i<m; i++){
20+
for(int j=0; j<n; j++){
21+
int dx = 0;
22+
int dy = 0;
23+
if(i-1>=0){
24+
dx = visited[i-1][j];
25+
}
26+
if(j-1>=0){
27+
dy = visited[i][j-1];
28+
}
29+
visited[i][j] = dx+dy == 0 ? 1 : dx+dy;
30+
}
31+
}
32+
33+
return visited[m-1][n-1];
34+
}
35+
36+
public static void main ( String[] args ) {
37+
int m = 3;
38+
int n = 2;
39+
40+
System.out.println(uniquePaths(m,n));
41+
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
public class Solution746 {
4+
5+
public static int minCostClimbingStairs(int[] cost) {
6+
int[] minCost = new int[cost.length+1];
7+
int answer = 0;
8+
9+
for(int i=0; i<cost.length; i++){
10+
minCost[i] = cost[i];
11+
}
12+
13+
for(int i=2; i<minCost.length; i++){
14+
int target1 = minCost[i] + minCost[i-1];
15+
int target2 = minCost[i] + minCost[i-2];
16+
minCost[i] = Math.min(target1,target2);
17+
}
18+
19+
return minCost[minCost.length-1];
20+
}
21+
22+
public static void main ( String[] args ) {
23+
int[] cost = {1,100,1,1,1,100,1,1,100,1};
24+
System.out.println(minCostClimbingStairs( cost ));
25+
}
26+
}

0 commit comments

Comments
 (0)