From 58752361010ea8cee5e5c28301736d5a93b03924 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 16 Sep 2024 21:21:50 +0530 Subject: [PATCH 1/5] Minimizing coins removed condition --- solutions/gold/cses-1634.mdx | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/solutions/gold/cses-1634.mdx b/solutions/gold/cses-1634.mdx index 9a29bc52e8..a624a08c05 100644 --- a/solutions/gold/cses-1634.mdx +++ b/solutions/gold/cses-1634.mdx @@ -82,10 +82,8 @@ int main() { for (int i = 0; i <= x; i++) { dp[i] = INT_MAX; } dp[0] = 0; for (int i = 1; i <= n; i++) { - for (int weight = 0; weight <= x; weight++) { - if (weight - coins[i - 1] >= 0) { - dp[weight] = min(dp[weight], dp[weight - coins[i - 1]] + 1); - } + for (int weight = coins[i - 1]; weight <= x; weight++) { + dp[weight] = min(dp[weight], dp[weight - coins[i - 1]] + 1); } } cout << (dp[x] == INT_MAX ? -1 : dp[x]) << '\n'; @@ -126,10 +124,8 @@ public class cses1634 { If the state curWeight - coin[i] is possible DP[curWeight] = min(DP[curWeight], DP[curWeight - coin[i]] + 1). */ for (int i = 1; i <= N; i++) { - for (int sum = 0; sum <= X; sum++) { - if (sum - coins[i - 1] >= 0) { - dp[sum] = Integer.min(dp[sum], dp[sum - coins[i - 1]] + 1); - } + for (int sum = coins[i - 1]; sum <= X; sum++) { + dp[sum] = Integer.min(dp[sum], dp[sum - coins[i - 1]] + 1); } } @@ -155,17 +151,13 @@ c = list(map(int, input().split())) dp = [INF] * (x + 1) dp[0] = 0 # Base case: 0 coins are needed for a sum of 0 -for i in range(x): - # Continue if the state is impossible - if i == INF: - continue - for coin in c: - if i + coin <= x: - """ - DP transition: state i needs dp[i] coins, - so state i + coin can be made with dp[i] + 1 coins. - """ - dp[i + coin] = min(dp[i + coin], dp[i] + 1) +for coin in c: + for i in range(x - coin + 1): + """ + DP transition: state i needs dp[i] coins, + so state i + coin can be made with dp[i] + 1 coins. + """ + dp[i + coin] = min(dp[i + coin], dp[i] + 1) print(dp[x] if dp[x] != INF else -1) From d4e0c8704443e92b4944abd3e8a19fa79b7d0fd8 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 16 Sep 2024 21:33:27 +0530 Subject: [PATCH 2/5] Update cses-1634.mdx Python formatting --- solutions/gold/cses-1634.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/gold/cses-1634.mdx b/solutions/gold/cses-1634.mdx index a624a08c05..f5ab1348a8 100644 --- a/solutions/gold/cses-1634.mdx +++ b/solutions/gold/cses-1634.mdx @@ -140,8 +140,8 @@ public class cses1634 { } ``` - + ```py INF = 1000000000 # Using float('inf') results in a TLE @@ -162,6 +162,6 @@ for coin in c: print(dp[x] if dp[x] != INF else -1) ``` - + From ead7e158b827d163cbd6670d614749bc4eff161c Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 16 Sep 2024 23:33:27 +0530 Subject: [PATCH 3/5] Clean C++ code --- solutions/gold/cses-1634.mdx | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/solutions/gold/cses-1634.mdx b/solutions/gold/cses-1634.mdx index f5ab1348a8..d285ea6f68 100644 --- a/solutions/gold/cses-1634.mdx +++ b/solutions/gold/cses-1634.mdx @@ -53,22 +53,6 @@ you used, and print $-1$ if so (since it's impossible to achieve a sum of $x$). using namespace std; using ll = long long; using vi = vector; -#define pb push_back -#define rsz resize -#define all(x) begin(x), end(x) -#define sz(x) (int)(x).size() -using pi = pair; -#define f first -#define s second -#define mp make_pair -void setIO(string name = "") { // name is nonempty for USACO file I/O - ios_base::sync_with_stdio(0); - cin.tie(0); // see Fast Input & Output - if (sz(name)) { - freopen((name + ".in").c_str(), "r", stdin); // see Input & Output - freopen((name + ".out").c_str(), "w", stdout); - } -} ll dp[1000001]; From 3bf4f4ce1a44d460c097c7953eb3332412ab92f6 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 16 Sep 2024 23:59:37 +0530 Subject: [PATCH 4/5] Update cses-1634.mdx --- solutions/gold/cses-1634.mdx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/solutions/gold/cses-1634.mdx b/solutions/gold/cses-1634.mdx index d285ea6f68..09cc99d449 100644 --- a/solutions/gold/cses-1634.mdx +++ b/solutions/gold/cses-1634.mdx @@ -52,16 +52,15 @@ you used, and print $-1$ if so (since it's impossible to achieve a sum of $x$). #include using namespace std; using ll = long long; -using vi = vector; ll dp[1000001]; -const int MOD = (int)1e9 + 7; +const int MOD = 1e9 + 7; int main() { int n, x; cin >> n >> x; - vi coins(n); + vector coins(n); for (int i = 0; i < n; i++) { cin >> coins[i]; } for (int i = 0; i <= x; i++) { dp[i] = INT_MAX; } dp[0] = 0; From ac8daa67e674be70fcaf98ff6c29458b10320698 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:02:39 -0700 Subject: [PATCH 5/5] Update cses-1634.mdx --- solutions/gold/cses-1634.mdx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solutions/gold/cses-1634.mdx b/solutions/gold/cses-1634.mdx index 09cc99d449..7e3116a440 100644 --- a/solutions/gold/cses-1634.mdx +++ b/solutions/gold/cses-1634.mdx @@ -5,12 +5,12 @@ title: Minimizing Coins author: Michael Cao, Sofia Yang, Paul Chen --- +## Explanation + In this problem, we're asked the minimum number of coins of distinct weights needed to achieve some weight, $x$. You can read about the solution to this classical problem in [CPH Chapter 7](/CPH.pdf) under "Coin Problem". -# Main Idea - For this problem, we'll define $\texttt{dp[w]}$ as the minimum number of coins to achieve some weight, $w$. Then, at some $w$, we can try to use every coin. Using the $i$-th coin represents transitioning from the state @@ -33,6 +33,8 @@ you add 1 for the transitions. +## Implementation + Don't forget to check if $\texttt{dp[x]} = MX$, where $MX$ is the large value @@ -40,12 +42,9 @@ you used, and print $-1$ if so (since it's impossible to achieve a sum of $x$). -## Implementation - **Time Complexity**: $\mathcal O(N\cdot X)$ - ```cpp @@ -62,6 +61,7 @@ int main() { cin >> n >> x; vector coins(n); for (int i = 0; i < n; i++) { cin >> coins[i]; } + for (int i = 0; i <= x; i++) { dp[i] = INT_MAX; } dp[0] = 0; for (int i = 1; i <= n; i++) { @@ -69,13 +69,12 @@ int main() { dp[weight] = min(dp[weight], dp[weight - coins[i - 1]] + 1); } } - cout << (dp[x] == INT_MAX ? -1 : dp[x]) << '\n'; + + cout << (dp[x] == INT_MAX ? -1 : dp[x]) << endl; } ``` - - ```java @@ -84,7 +83,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; -public class cses1634 { +public class MinCoins { public static int MAX = (int)10e6 + 2; public static void main(String[] args) throws IOException { @@ -122,6 +121,7 @@ public class cses1634 { } } ``` +