Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit acb0c71

Browse files
committed
knapsack unbounded and Tiling Dominoes added
1 parent a30d138 commit acb0c71

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed

‎AlgorithmCode/KnapsackUnbounded.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* This file contains a dynamic programming solutions to the classic unbounded knapsack problem
3+
* where are you are trying to maximize the total profit of items selected without exceeding the
4+
* capacity of your knapsack.
5+
*
6+
* <p>Version 1: Time Complexity: O(nW) Version 1 Space Complexity: O(nW)
7+
*
8+
* <p>Version 2: Time Complexity: O(nW) Space Complexity: O(W)
9+
*
10+
* <p>Tested code against: https://www.hackerrank.com/challenges/unbounded-knapsack
11+
*
12+
* @author William Fiset, william.alexandre.fiset@gmail.com
13+
*/
14+
15+
public class KnapsackUnbounded {
16+
17+
/**
18+
* @param maxWeight - The maximum weight of the knapsack
19+
* @param W - The weights of the items
20+
* @param V - The values of the items
21+
* @return The maximum achievable profit of selecting a subset of the elements such that the
22+
* capacity of the knapsack is not exceeded
23+
*/
24+
public static int unboundedKnapsack(int maxWeight, int[] W, int[] V) {
25+
26+
if (W == null || V == null || W.length != V.length || maxWeight < 0)
27+
throw new IllegalArgumentException("Invalid input");
28+
29+
final int N = W.length;
30+
31+
// Initialize a table where individual rows represent items
32+
// and columns represent the weight of the knapsack
33+
int[][] DP = new int[N + 1][maxWeight + 1];
34+
35+
// Loop through items
36+
for (int i = 1; i <= N; i++) {
37+
38+
// Get the value and weight of the item
39+
int w = W[i - 1], v = V[i - 1];
40+
41+
// Consider all possible knapsack sizes
42+
for (int sz = 1; sz <= maxWeight; sz++) {
43+
44+
// Try including the current element
45+
if (sz >= w) DP[i][sz] = DP[i][sz - w] + v;
46+
47+
// Check if not selecting this item at all is more profitable
48+
if (DP[i - 1][sz] > DP[i][sz]) DP[i][sz] = DP[i - 1][sz];
49+
}
50+
}
51+
52+
// Return the best value achievable
53+
return DP[N][maxWeight];
54+
}
55+
56+
public static int unboundedKnapsackSpaceEfficient(int maxWeight, int[] W, int[] V) {
57+
58+
if (W == null || V == null || W.length != V.length)
59+
throw new IllegalArgumentException("Invalid input");
60+
61+
final int N = W.length;
62+
63+
// Initialize a table where we will only keep track of
64+
// the best possible value for each knapsack weight
65+
int[] DP = new int[maxWeight + 1];
66+
67+
// Consider all possible knapsack sizes
68+
for (int sz = 1; sz <= maxWeight; sz++) {
69+
70+
// Loop through items
71+
for (int i = 0; i < N; i++) {
72+
73+
// First check that we can include this item (we can't include it if
74+
// it's too heavy for our knapsack). Assumming it fits inside the
75+
// knapsack check if including this element would be profitable.
76+
if (sz - W[i] >= 0 && DP[sz - W[i]] + V[i] > DP[sz]) DP[sz] = DP[sz - W[i]] + V[i];
77+
}
78+
}
79+
80+
// Return the best value achievable
81+
return DP[maxWeight];
82+
}
83+
84+
public static void main(String[] args) {
85+
86+
int[] W = {3, 6, 2};
87+
int[] V = {5, 20, 3};
88+
int knapsackValue = unboundedKnapsackSpaceEfficient(10, W, V);
89+
System.out.println("Maximum knapsack value: " + knapsackValue);
90+
}
91+
}

‎AlgorithmCode/TilingDominoes.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Solution to Tri Tiling (https://open.kattis.com/problems/tritiling)
3+
*
4+
* <p>Explanation video: https://www.youtube.com/watch?v=yn2jnmlepY8
5+
*
6+
* <p>Solution by: William Fiset
7+
*/
8+
import java.util.*;
9+
10+
public class TilingDominoes {
11+
static Scanner sc = new Scanner(System.in);
12+
13+
public static void main(String[] args) {
14+
while (true) {
15+
int n = sc.nextInt();
16+
if (n == -1) break;
17+
// Solution1:
18+
System.out.println(solution1(n));
19+
20+
// Alternative solution:
21+
// System.out.println(solution2(n));
22+
}
23+
}
24+
25+
// Tile states 0...7 representations:
26+
//
27+
// 0: 0 1: 1 2: 0 3: 1
28+
// 0 0 1 1
29+
// 0 0 0 0
30+
//
31+
// 4: 0 5: 1 6: 0 7: 1
32+
// 0 0 1 1
33+
// 1 1 1 1
34+
private static int solution1(int n) {
35+
int[][] dp = new int[n + 1][1 << 3];
36+
dp[0][7] = 1;
37+
for (int i = 1; i < n + 1; i++) {
38+
// The number of empty states for this column is the number
39+
// of full states in the previous column.
40+
dp[i][0] += dp[i - 1][7];
41+
42+
dp[i][1] += dp[i - 1][6];
43+
44+
// State 2 doesn't contribute to the number of tilings
45+
// dp[i][2] += dp[i-1][5];
46+
47+
dp[i][3] += dp[i - 1][7];
48+
dp[i][3] += dp[i - 1][4];
49+
50+
dp[i][4] += dp[i - 1][3];
51+
52+
// State 5 doesn't contribute to the number of tilings
53+
// dp[i][5] += dp[i-1][2];
54+
55+
dp[i][6] += dp[i - 1][7];
56+
dp[i][6] += dp[i - 1][1];
57+
58+
dp[i][7] += dp[i - 1][3];
59+
dp[i][7] += dp[i - 1][6];
60+
dp[i][7] += dp[i - 1][0];
61+
}
62+
// printMatrix(dp);
63+
return dp[n][7];
64+
}
65+
66+
private static void printMatrix(int[][] dp) {
67+
for (int i = 0; i < dp.length; i++) {
68+
for (int j = 0; j < 1 << 3; j++) {
69+
System.out.printf("% 5d,", dp[i][j]);
70+
}
71+
System.out.println();
72+
}
73+
}
74+
75+
private static int solution2(int n) {
76+
int[] dp = new int[n + 4];
77+
dp[0] = 1;
78+
dp[2] = 3;
79+
for (int i = 4; i < n + 4; i += 2) {
80+
dp[i] = 4 * dp[i - 2] - dp[i - 4];
81+
}
82+
return dp[n];
83+
}
84+
}

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
9090

9191
- [0 1 Knapsack](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Knapsack_01.java) - 물건의 수량이 최대 1개인 배낭 문제
9292

93-
- [Unbounded Knapsack] - 물건의 수량이 제한 없는 배낭 문제
93+
- [Unbounded Knapsack](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/KnapsackUnbounded.java) - 물건의 수량이 제한 없는 배낭 문제
9494

9595
- [LCS(Longest Common Subsequence)](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/LCS.java) - 최장 공통 부분 순서(Compare with 3 Strings), Bottom Up Manner O(n^3)
9696

@@ -108,6 +108,8 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
108108

109109
- [Stair Case Problem](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Upstair.java)
110110

111+
- [Tiling Dominoes](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/TilingDominoes.java)
112+
111113
### Prime - 소수
112114
- [Sieve Of Eratosthenes](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Eratosthenes.java) - 에라토스테네스의 체
113115

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /