Skip to content

Commit 6f4bdda

Browse files
committed
.
1 parent 6ef0019 commit 6f4bdda

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: knapsack","url":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\knapsack.cpp","tests":[{"id":1617723537351,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\knapsack.cpp","group":"local","local":true}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"Local: Maximum Product","url":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","tests":[{"id":1617679766823,"input":"1 10\n","output":"9\n"},{"id":1617679777541,"input":"51 62\n","output":"59"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","group":"local","local":true}
1+
{"name":"Local: Maximum Product","url":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","tests":[{"id":1617679766823,"input":"1 10\n","output":"9\n"},{"id":1617679777541,"input":"51 62","output":"59"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","group":"local","local":true}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
//------------------------------------------------------------------------------
3+
#include <iostream>
4+
#include <vector>
5+
// #include <bits/stdc++.h>
6+
// #include <cmath>
7+
// #include <algorithm>
8+
// #include <unordered_map>
9+
// #include <map>
10+
// #include <set>
11+
// #include <unordered_set>
12+
//------------------------------------------------------------------------------
13+
using namespace std;
14+
//------------------------------------------------------------------------------
15+
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
16+
#define v(Type) vector<Type>
17+
#define w(T) \
18+
int T; \
19+
cin >> T; \
20+
while (T--)
21+
#define int long long int
22+
#define mod 1000000007ll
23+
#define endl "\n"
24+
//------------------------------------------------------------------------------
25+
// Any fucntion can be called using Math.function_name();
26+
//------------------------------------------------------------------------------
27+
class Math
28+
{
29+
public:
30+
//Returns gcd of two numbers
31+
int gcd(int a, int b)
32+
{
33+
return (a % b == 0) ? b : gcd(b, a % b);
34+
}
35+
36+
//Returns lcm of two numbers
37+
int lcm(int a, int b)
38+
{
39+
return a * (b / gcd(a, b));
40+
}
41+
42+
// Returns flag array isPrime
43+
// isPrime[i] = true (if i is Prime)
44+
// isPrime[i] = false (if i is not Prime)
45+
vector<bool> *seiveOfEratosthenes(const int N)
46+
{
47+
vector<bool> *isPrime = new vector<bool>(N + 1, true);
48+
(*isPrime)[0] = (*isPrime)[1] = false;
49+
for (int i = 2; i * i <= N; ++i)
50+
if ((*isPrime)[i])
51+
for (int j = i * i; j <= N; j += i)
52+
(*isPrime)[j] = false;
53+
54+
return isPrime;
55+
}
56+
57+
//Returns (x ^ n)
58+
int pow(const int &x, int n)
59+
{
60+
if (n == 0)
61+
return 1;
62+
int h = pow(x, n / 2);
63+
return (n & 1) ? h * h * x : h * h;
64+
}
65+
66+
//Returns (x ^ n) % M
67+
int pow(const int &x, int n, const int &M)
68+
{
69+
if (n == 0)
70+
return 1;
71+
int h = pow(x, n / 2) % M;
72+
return (n & 1) ? (h * h * x) % M : (h * h) % M;
73+
}
74+
75+
//Returns all Primes <= N
76+
vector<int> *primesUptoN(const int N)
77+
{
78+
vector<bool> *isPrime = seiveOfEratosthenes(N);
79+
vector<int> *Primes = new vector<int>;
80+
if (2 <= N)
81+
(*Primes).push_back(2);
82+
for (int i = 3; i <= N; i += 2)
83+
if ((*isPrime)[i])
84+
(*Primes).push_back(i);
85+
return Primes;
86+
}
87+
88+
} Math;
89+
//------------------------------------------------------------------------------
90+
void solve()
91+
{
92+
93+
}
94+
//------------------------------------------------------------------------------
95+
int32_t main()
96+
{
97+
FastIO;
98+
99+
// w(T)
100+
solve();
101+
102+
return 0;
103+
}
104+
//------------------------------------------------------------------------------
105+

Practice/knapsack.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
pair<int, string> dp[1000][1000];
5+
6+
pair<int, string> solve(int A[], int W[], int n, int capacity)
7+
{
8+
if (capacity == 0 or n == 0)
9+
return {0, ""};
10+
pair<int, string> temp = {-1, ""};
11+
auto ans2 = solve(A, W, n - 1, capacity);
12+
auto ans1 = (W[n - 1] <= capacity) ? solve(A, W, n - 1, capacity - W[n - 1]) : temp;
13+
if (ans1 != temp)
14+
{
15+
ans1.first += A[n - 1];
16+
ans1.second += "1";
17+
}
18+
ans2.second += "0";
19+
auto ans = (ans1.first > ans2.first) ? ans1 : ans2;
20+
if (ans1.first > ans2.first)
21+
{
22+
return ans1;
23+
}
24+
return ans2;
25+
}
26+
27+
int main()
28+
{
29+
int A[] = {100, 90, 70, 50, 40};
30+
int W[] = {20, 21, 50, 54, 100};
31+
32+
int n = 5;
33+
int capacity = 100;
34+
auto ans = solve(A, W, n, capacity);
35+
36+
cout << ans.first << endl;
37+
cout << ans.second << endl;
38+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
|| <strong>CodeChef | ⭐⭐⭐⭐ | 134 | [<strong>Click Here](https://www.codechef.com/users/pasricha_dhruv) |
88
|| <strong>CodeForces | ⭐⭐ | 163 | [<strong>Click Here](https://codeforces.com/profile/pasricha_dhruv) |
99
|| <strong>HackerRank | ⭐⭐⭐⭐⭐⭐ | 139 | [<strong>Click Here](https://www.hackerrank.com/dhruv_pasricha?hr_r=1) |
10-
|| <strong>LeetCode | ⭐⭐ | 120 | [<strong>Click Here](https://leetcode.com/pasricha_dhruv/) |
10+
|| <strong>LeetCode | ⭐⭐ | 126 | [<strong>Click Here](https://leetcode.com/pasricha_dhruv/) |
1111

1212
<hr>

0 commit comments

Comments
 (0)