Skip to content

leetCode : DP #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2023
Merged
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
36 changes: 36 additions & 0 deletions src/main/java/sgyj/inflearn/yeji/section9/Solution5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sgyj.inflearn.yeji.section9;

import java.awt.print.Pageable;
import java.util.Arrays;
import java.util.Scanner;

// 동전교환
public class Solution5 {
private static int n, m;
private static int[] dy;

private static int solution(int n, int[] coins, int result){
Arrays.fill( dy,Integer.MAX_VALUE );
dy[0] = 0;
for(int i=0; i<n; i++){
for(int j=coins[i]; j<=m; j++){
dy[j]=Math.min( dy[j],dy[j-coins[i]]+1 );
}
}

return dy[m];
}

public static void main ( String[] args ) {
Scanner sc = new Scanner( System.in );
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
m = sc.nextInt();
dy = new int[m+1];
System.out.println(solution( n,arr,m ));
}

}
10 changes: 9 additions & 1 deletion src/main/java/sgyj/leetcode/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@

- [200. Number of Islands](https://leetcode.com/problems/number-of-islands/description/)
- [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)

## DP

- [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
- [322. Coin Change](https://leetcode.com/problems/coin-change/description/)
- [62. Unique Paths](https://leetcode.com/problems/unique-paths/description/)
- [198. House Robber](https://leetcode.com/problems/house-robber/description/)
36 changes: 36 additions & 0 deletions src/main/java/sgyj/leetcode/yeji/section6/Solution198.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sgyj.leetcode.yeji.section6;

// House Robber
public class Solution198 {
private static int[] stillMoney;

public static int rob(int[] nums) {
int answer = 0;
stillMoney = new int[nums.length];
for(int n=0; n<nums.length; n++){
stillMoney[n] = nums[n];
}

for(int n=2; n<nums.length; n++){
int max = 0;
for(int i=n-2; i>=0; i--){
max= Math.max(max,stillMoney[i]);
}
stillMoney[n]+=max;
}

for(int money:stillMoney){
answer = Math.max(answer,money);
}


return answer;
}

public static void main ( String[] args ) {
int[] nums = {1,2,3,1};

System.out.println(rob(nums));
}

}
23 changes: 23 additions & 0 deletions src/main/java/sgyj/leetcode/yeji/section6/Solution322.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sgyj.leetcode.yeji.section6;

import java.util.Arrays;

// coin change
public class Solution322 {

public int coinChange(int[] coins, int amount) {
int[] result = new int[amount+1];
Arrays.fill( result, Integer.MAX_VALUE - 1);
result[0] = 0;

for(int coin : coins){
for(int n = coin; n<=amount; n++){
int tmp = result[n-coin] + 1;
if(result[n]>tmp) result[n] = tmp;
}
}

return result[result.length-1] == Integer.MAX_VALUE-1 ? -1 : result[result.length-1];
}

}
43 changes: 43 additions & 0 deletions src/main/java/sgyj/leetcode/yeji/section6/Solution62.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sgyj.leetcode.yeji.section6;

// 62. Unique Paths
/*
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]).
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.
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.
The test cases are generated so that the answer will be less than or equal to 2 * 109.
*/

public class Solution62 {
private static int[][] visited;
private static int answer = 0;
public static int uniquePaths(int m, int n) {

visited = new int[m][n];
visited[0][0] = 1;

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
int dx = 0;
int dy = 0;
if(i-1>=0){
dx = visited[i-1][j];
}
if(j-1>=0){
dy = visited[i][j-1];
}
visited[i][j] = dx+dy == 0 ? 1 : dx+dy;
}
}

return visited[m-1][n-1];
}

public static void main ( String[] args ) {
int m = 3;
int n = 2;

System.out.println(uniquePaths(m,n));

}
}
26 changes: 26 additions & 0 deletions src/main/java/sgyj/leetcode/yeji/section6/Solution746.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sgyj.leetcode.yeji.section6;

public class Solution746 {

public static int minCostClimbingStairs(int[] cost) {
int[] minCost = new int[cost.length+1];
int answer = 0;

for(int i=0; i<cost.length; i++){
minCost[i] = cost[i];
}

for(int i=2; i<minCost.length; i++){
int target1 = minCost[i] + minCost[i-1];
int target2 = minCost[i] + minCost[i-2];
minCost[i] = Math.min(target1,target2);
}

return minCost[minCost.length-1];
}

public static void main ( String[] args ) {
int[] cost = {1,100,1,1,1,100,1,1,100,1};
System.out.println(minCostClimbingStairs( cost ));
}
}