├── Assignment Solutions ├── 1-a-addition-and-subtraction │ ├── addition_and_subtraction.cpp │ └── addition_and_subtraction.exe ├── 1-b-erasing_maximum │ └── erasing_maximum.cpp ├── 1-c-increment │ └── increment.cpp ├── 1-d-straight_flush │ └── straight_flush.cpp ├── 2-C │ └── 2c.cpp ├── 2-D │ └── 2d.cpp ├── 2-a-cheapest_permutation │ └── cheapest_permutation.cpp ├── 2-b-king │ └── king.cpp ├── 3-a-compare_sums │ └── compare_sums.cpp ├── 3-b-round_up │ ├── round_up.cpp │ └── test.cpp ├── 3-c │ ├── test.cpp │ └── test.exe ├── 3-d │ └── 3-d.cpp ├── 4-B-i │ └── 4-b.cpp ├── 4-C-multiset │ └── 4-d.cpp ├── 4-D │ └── 4-E.cpp ├── 4-a-frequent_symbol │ └── frequent_symbol.cpp ├── 4-b-ii │ └── manhattan.cpp ├── 5-C-SumOFDigits │ ├── 5_C.cpp │ └── 5_C.exe ├── 5-D │ └── make-it-sorted.cpp ├── 5-a-lis │ └── lis.cpp ├── 5-b-edit_distance │ └── edit_distance.cpp ├── 6-D-Maximal-Sum-Square │ └── 6-D.cpp ├── 6-a-knapsack │ └── knapsack.cpp ├── 6-b-chain_matrix │ ├── chain_matrix.cpp │ └── maximumvalue.cpp └── 6-c-longest-common-subsequence │ └── 6-C.cpp ├── LICENSE ├── Lecture slides ├── 1_programming_competitions │ ├── competitions.pdf │ └── testing.pdf ├── 2_correctness_first │ ├── 1_structuring_code │ │ └── Structuring Code final.pdf │ ├── 2_brute_force_solutions │ │ ├── backtracking.pdf │ │ ├── bruteforce_final.pdf │ │ ├── proofs.pdf │ │ └── solutionset.pdf │ └── 3_time_complexity │ │ ├── bigO.pdf │ │ ├── time_final.pdf │ │ └── worst_average.pdf ├── 3_common_struggles │ ├── 1_insidious_numbers │ │ └── numbers.pdf │ ├── 2_greedy_algorithms │ │ └── greedy.pdf │ ├── 3_language_specifics │ │ └── language.pdf │ └── 4_getting_unstuck │ │ └── Getting Unstuck.pptx ├── 4_technical_problems │ ├── 1_testing │ │ └── testing.pdf │ └── 2_segment_tree │ │ └── segment_final.pdf ├── 5_dynamic_programming │ └── dynprog.pdf └── 6_dynamic_programming2 │ └── dynprog2.pdf ├── Problem_statements.pdf └── README.md /Assignment Solutions/1-a-addition-and-subtraction/addition_and_subtraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int x,y,z,n1,n2; 8 | while(cin>>x>>y>>z){ 9 | if(z==0) { 10 | cout<<"0\n"; 11 | continue; 12 | } 13 | if((x-y)==0){ 14 | if(z==x) cout<<"1\n"; 15 | else cout<<"-1\n"; 16 | continue; 17 | } 18 | if(z%(x-y)==0){ 19 | n1 = z/(x-y) + 1; 20 | n1 = 2*(n1-1); 21 | } 22 | else 23 | n1 = 100000000; 24 | if((z-x)%(x-y)==0){ 25 | n2 = (z-x)/(x-y) + 1; 26 | n2 = 2*n2-1; 27 | } 28 | else 29 | n2 = 100000000; 30 | if(n1<0) n1=100000000; 31 | if(n2<0) n2=100000000; 32 | if(n1 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int n,i,max_ele,count,index,flag; 8 | int A[101]; 9 | //while(cin>>n){ 10 | //input 11 | cin>>n; 12 | for(i=0;i>A[i]; 14 | } 15 | //compute 16 | max_ele = -1; 17 | for(i=0;imax_ele){ 19 | max_ele = A[i]; 20 | count = 1; 21 | index = i; 22 | } 23 | else if(A[i] == max_ele){ 24 | count++; 25 | if(count==3){ 26 | index = i; 27 | } 28 | } 29 | } 30 | //output 31 | flag=0; 32 | if(index!=0){ 33 | cout< 3 | int main(){ 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | string s; 7 | int l,flag; 8 | cin>>s; 9 | l = s.length(); 10 | flag=0; 11 | for(auto x:s){ 12 | if(x!='9'){ 13 | flag=1; 14 | break; 15 | } 16 | } 17 | if(flag) cout< 3 | 4 | string deck; 5 | string str; 6 | 7 | bool evaluate(){ 8 | return deck.find("11111")!=std::string::npos; 9 | } 10 | bool process(){ 11 | if((str[1]==str[4])&&(str[4]==str[7])&&(str[7]==str[10])){ 12 | for(int i=0;i<13;i+=3){ 13 | if(str[i]<58&&str[i]>49) deck[str[i]-48]='1'; 14 | else if(str[i]=='A') { deck[1]='1'; deck[14]='1';} 15 | else if(str[i]=='T') deck[10]='1'; 16 | else if(str[i]=='J') deck[11]='1'; 17 | else if(str[i]=='Q') deck[12]='1'; 18 | else if(str[i]=='K') deck[13]='1'; 19 | } 20 | //cout< 2 | using namespace std; 3 | 4 | long long int sumSubarrayMins(long long int A[],long long int n) 5 | { 6 | long long int left[n], right[n]; 7 | 8 | stack > s1, s2; 9 | for (int i = 0; i < n; ++i) { 10 | int cnt = 1; 11 | 12 | while (!s1.empty() && (s1.top().first) > A[i]) { 13 | cnt += s1.top().second; 14 | s1.pop(); 15 | } 16 | 17 | s1.push({ A[i], cnt }); 18 | left[i] = cnt; 19 | } 20 | for (int i = n - 1; i >= 0; --i) { 21 | int cnt = 1; 22 | 23 | while (!s2.empty() && (s2.top().first) >= A[i]) { 24 | cnt += s2.top().second; 25 | s2.pop(); 26 | } 27 | 28 | s2.push({ A[i], cnt }); 29 | right[i] = cnt; 30 | } 31 | 32 | long long result = 0; 33 | 34 | for (int i = 0; i < n; ++i) 35 | result = (result + A[i] * left[i] * right[i]); 36 | 37 | return result; 38 | } 39 | int main() 40 | { 41 | ios_base::sync_with_stdio(false); 42 | cin.tie(NULL); 43 | 44 | 45 | long long int n ; 46 | cin>>n; 47 | long long int A[n]; 48 | for(int i=0;i>A[i]; 50 | } 51 | 52 | cout << sumSubarrayMins(A, n); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Assignment Solutions/2-D/2d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | cin.sync_with_stdio(false); 10 | cin.tie(0); 11 | 12 | string s; 13 | cin >> s; 14 | long long ans = 0; 15 | int last = 0; 16 | int sign = 1; 17 | 18 | for (int i = 0; i < s.length(); ++i) { 19 | char c = s[i]; 20 | if (c == '+' || c == '-') { 21 | ans += sign * last; 22 | last = 0; 23 | if (c == '+') { 24 | sign = 1; 25 | } else { 26 | sign = -1; 27 | } 28 | } else { 29 | last = 10 * last + (c - '0'); 30 | } 31 | } 32 | 33 | ans += sign * last; 34 | cout << ans << '\n'; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Assignment Solutions/2-a-cheapest_permutation/cheapest_permutation.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | #include 3 | int n; 4 | int arr[9]={1,2,3,4,5,6,7,8,9}; 5 | int result[9]={0,0,0,0,0,0,0,0,0}; 6 | int mat[9][9]; 7 | int find_cost(){ 8 | int cost=0; 9 | for(int i=0;i<(n-1);i++){ 10 | cost+=mat[arr[i]-1][arr[i+1]-1]; 11 | } 12 | return cost; 13 | } 14 | void get_result(){ 15 | for(int i=0;i>n){ 40 | for(int i=0;i>mat[i][j]; 43 | process(); 44 | print_result(); 45 | } 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Assignment Solutions/2-b-king/king.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | #include 3 | int r,c; 4 | int find_val(int x){ 5 | int mod = x%3; 6 | int val; 7 | if(mod==0 || mod==1 ) val = (x/3)*2; 8 | else val = 1 + (x/3)*2; 9 | return val; 10 | } 11 | int process(){ 12 | int ans = 0; 13 | int val_r = find_val(r); 14 | int val_c = find_val(c); 15 | if(r*c==1) ans = 0; 16 | else if(r==1) ans = val_c; 17 | else if(c==1) ans = val_r; 18 | else ans = val_r*c + (r-val_r)*val_c; 19 | return ans; 20 | } 21 | int main(){ 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | while(cin>>r>>c){ 25 | cout< 3 | double a[100], b[100]; 4 | void read_vector(int n){ 5 | double x; 6 | for (int i = 0; i < n; ++i) 7 | cin >> a[i]; 8 | for (int i = 0; i < n; ++i) 9 | cin >> b[i]; 10 | } 11 | void print_vector(int n){ 12 | for (int i = 0; i < n; ++i){ 13 | cout<y+error) 25 | cout << "SUM(A)>SUM(B)" << "\n"; 26 | else 27 | cout << "SUM(A)> n){ 42 | read_vector(n); 43 | // print_vector(n); 44 | sum_a = accumulate(a,a+n,0.0); 45 | sum_b = accumulate(b,b+n,0.0); 46 | error = (double)n/(200000.0); 47 | // cout< 3 | int main(){ 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | int x, y,result; 7 | while(cin >> x >> y){ 8 | result = x/y; 9 | if((x%y==0)||(long long)x*y<0); 10 | else result+=1; 11 | cout< 3 | int main(){ 4 | #ifndef ONLINE_JUDGE 5 | freopen("testInput.txt", "r", stdin); 6 | freopen("testOutput.txt", "w", stdout); 7 | #endif 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int x, y,result; 11 | while(cin >> x >> y){ 12 | cout< 2 | using namespace std; 3 | int main(){ 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | int n; 7 | cin>>n; 8 | long double r=0,t; 9 | for(int i=0;i>t; 11 | r+=t+(1/t); 12 | } 13 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct item { 8 | int weight, value; 9 | bool operator < (const item& other) const { 10 | return weight < other.weight; 11 | } 12 | }; 13 | 14 | int main() { 15 | cin.sync_with_stdio(false); 16 | cin.tie(0); 17 | 18 | int n, totalWeight; 19 | cin >> n >> totalWeight; 20 | 21 | vector items; 22 | for (int i = 0; i < n; ++i) { 23 | int weight, value; 24 | cin >> weight >> value; 25 | items.push_back({weight, value}); 26 | } 27 | 28 | sort(items.begin(), items.end()); 29 | reverse(items.begin(), items.end()); 30 | 31 | int totalValue = 0; 32 | vector candidates; 33 | 34 | for (int power = 0; power <= 30; ++power) { 35 | int weight = 1 << power; 36 | while (items.size() > 0 && items.back().weight == weight) { 37 | candidates.push_back(items.back().value); 38 | items.pop_back(); 39 | } 40 | 41 | sort(candidates.begin(), candidates.end()); 42 | 43 | if (totalWeight & weight) { 44 | if (candidates.size() > 0) { 45 | totalValue += candidates.back(); 46 | candidates.pop_back(); 47 | } 48 | } 49 | 50 | vector newCandidates; 51 | while (candidates.size() >= 2) { 52 | int combinedValue = candidates.back(); 53 | candidates.pop_back(); 54 | combinedValue += candidates.back(); 55 | candidates.pop_back(); 56 | newCandidates.push_back(combinedValue); 57 | } 58 | 59 | if (candidates.size() >= 1) { 60 | newCandidates.push_back(candidates.back()); 61 | candidates.pop_back(); 62 | } 63 | 64 | candidates = newCandidates; 65 | } 66 | 67 | cout << totalValue << '\n'; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Assignment Solutions/4-B-i/4-b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lint long long int 3 | using namespace std; 4 | 5 | int main() { 6 | cin.sync_with_stdio(false); 7 | cin.tie(0); 8 | 9 | lint n; 10 | cin >> n; 11 | lint arr[n+1]; 12 | cin>>arr[1]; 13 | lint maxI,minI; 14 | maxI=minI=1; 15 | lint maxv,minv; 16 | maxv=minv=arr[1]; 17 | cout<>arr[i]; 20 | if(arr[i]maxv){ 25 | maxv=arr[i]; 26 | maxI=i; 27 | } 28 | if(minI 2 | #define lint long long int 3 | using namespace std; 4 | 5 | 6 | 7 | int main() { 8 | cin.sync_with_stdio(false); 9 | cin.tie(0); 10 | 11 | lint n; 12 | cin >> n; 13 | lint max=100007; 14 | lint arr[max]; 15 | lint range=-100007; 16 | memset(arr,0,sizeof(arr)); 17 | while(n--){ 18 | lint l,r; 19 | cin>>l>>r; 20 | arr[l-1]++; 21 | arr[r]--; 22 | if(r>range){range=r;} 23 | } 24 | for(int i=1;i<=range;i++){ 25 | arr[i]+=arr[i-1]; 26 | } 27 | for(int i=0;i<=range;i++){ 28 | if(arr[i])cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() { 8 | int n; cin >> n; 9 | vector a(n); for (int i = 0; i < n; ++i) cin >> a[i]; 10 | vector p(n + 1), s(n + 1); 11 | for (int i = 0; i < n; ++i) p[i + 1] = p[i] + a[i]; 12 | for (int i = n - 1; i >= 0; --i) s[i] = s[i + 1] + a[i]; 13 | long long S = p[n]; 14 | for (int i = 0; i < n; ++i) p[i + 1] = min(p[i + 1], p[i]); 15 | for (int i = n - 1; i >= 0; --i) s[i] = min(s[i + 1], s[i]); 16 | 17 | for (int i = 0; i < n; ++i) { 18 | if (i) cout << ' '; 19 | cout << S - p[i] - s[i + 1] << endl; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | solve(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Assignment Solutions/4-a-frequent_symbol/frequent_symbol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #define endl '\n' 15 | #define FOR(i,a,n,c) for(int i=a;i<=n;i+=c) 16 | using namespace std; 17 | #define MOD 1000000007 18 | #define ll long long 19 | #define pb push_back 20 | typedef pair pll; 21 | int main() 22 | { 23 | //========Fast I/O 24 | // ios_base::sync_with_stdio(false); 25 | // cin.tie(0); 26 | //========== 27 | /* 28 | for reading from input.txt and writing on output.txt 29 | freopen("input.txt","r",stdin); 30 | freopen("output.txt","w",stdout); 31 | */ 32 | string s;cin>>s; 33 | ll n=s.length(); 34 | map> m; 35 | for(char c='a'; c<='z';c++) 36 | m[c].resize(n+1,0); 37 | for(char c='a'; c<='z';c++) 38 | { 39 | for(ll i=1;i<=n;i++) 40 | { 41 | if(s[i-1]==c) 42 | m[c][i]=1+m[c][i-1]; 43 | else 44 | m[c][i]=m[c][i-1]; 45 | } 46 | } 47 | ll q;cin>>q; 48 | while(q--) 49 | { 50 | ll l,r;cin>>l>>r; 51 | char ans;ll res=0; 52 | for(char c='a';c<='z';c++) 53 | { 54 | ll temp=m[c][r]-m[c][l-1]; 55 | if(temp>res) 56 | { 57 | res=temp; 58 | ans=c; 59 | } 60 | } 61 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #define endl '\n' 15 | #define FOR(i,a,n,c) for(int i=a;i<=n;i+=c) 16 | using namespace std; 17 | #define MOD 1000000007 18 | #define ll long long 19 | typedef pair pll; 20 | int main() 21 | { 22 | //========Fast I/O 23 | // ios_base::sync_with_stdio(false); 24 | // cin.tie(0); 25 | //========== 26 | /* 27 | for reading from input.txt and writing on output.txt 28 | freopen("input.txt","r",stdin); 29 | freopen("output.txt","w",stdout); 30 | */ 31 | ll n;cin>>n; 32 | ll maxSum=INT_MIN,maxDif=INT_MIN,minSum=INT_MAX,minDif=INT_MAX; 33 | ll a=0,b=0,c=0,d=0; 34 | for(ll i=1;i<=n;i++) 35 | { 36 | ll x,y;cin>>x>>y; 37 | if(x+y>maxSum) 38 | { 39 | maxSum=x+y; 40 | a=i; 41 | } 42 | if(x-y>maxDif) 43 | { 44 | maxDif=x-y; 45 | b=i; 46 | } 47 | if(x+y maxDif-minDif) 59 | cout< 2 | #define MOD 1000000007 3 | #define test int t; cin>>t; while(t--) 4 | #define init(arr,val) memset(arr,val,sizeof(arr)) 5 | #define loop(i,a,b) for(int i=a;i=b;i--) 7 | #define loops(i,a,b,step) for(int i=a;i=b;i-=step) 9 | #define ull unsigned long long int 10 | #define ll long long int 11 | #define P pair 12 | #define PLL pair 13 | #define PII pair 14 | #define PUU pair 15 | #define L list 16 | #define V vector 17 | #define D deque 18 | #define ST set 19 | #define MS multiset 20 | #define M map 21 | #define UM unordered_map 22 | #define mp make_pair 23 | #define pb push_back 24 | #define pf push_front 25 | #define MM multimap 26 | #define F first 27 | #define S second 28 | #define IT iterator 29 | #define RIT reverse_iterator 30 | #define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie(); 31 | #define FILE_READ_IN freopen("input.txt","r",stdin); 32 | #define FILE_READ_OUT freopen("output.txt","w",stdout); 33 | using namespace std; 34 | 35 | const ll maxn = 1e5; 36 | int s,l; 37 | ll dp[200][20]; 38 | 39 | ll cal(ll sum, ll dig){ 40 | if(sum > s)return 0; 41 | if(dig == l){ 42 | if(sum == s)return 1; 43 | return 0; 44 | } 45 | if(dp[sum][dig] != -1)return dp[sum][dig]; 46 | ll res = 0; 47 | loop(i,0,10){ 48 | if(dig == 0 && i == 0)continue; 49 | res+=cal(sum+i,dig+1); 50 | } 51 | dp[sum][dig] = res; 52 | return dp[sum][dig]; 53 | } 54 | 55 | int main(){ 56 | memset(dp,-1,sizeof(dp)); 57 | cin >> s >> l; 58 | if(s == 0 && l == 1)cout << "1"; 59 | else cout << cal(0,0); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Assignment Solutions/5-C-SumOFDigits/5_C.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D3v3sh5ingh/Competitive-Programmer-s-Core-Skills-Solutions/2c7fd6a61c30d49124d7326aaf160f8753e3c864/Assignment Solutions/5-C-SumOFDigits/5_C.exe -------------------------------------------------------------------------------- /Assignment Solutions/5-D/make-it-sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | cin.sync_with_stdio(false); 9 | cin.tie(0); 10 | 11 | int n; 12 | cin >> n; 13 | 14 | vector a(n); 15 | for (int i = 0; i < n; ++i) { 16 | cin >> a[i]; 17 | } 18 | 19 | int m = *max_element(a.begin(), a.end()); 20 | vector> dp(n + 1, vector(m + 1)); 21 | dp[0][0] = 0; 22 | for (int i = 0; i < n; ++i) { 23 | int bestBelow = 1e9; 24 | for (int j = 0; j <= m; ++j) { 25 | bestBelow = min(bestBelow, dp[i][j]); 26 | dp[i + 1][j] = bestBelow + abs(a[i] - j); 27 | } 28 | } 29 | 30 | cout << *min_element(dp[n].begin(), dp[n].end()) << '\n'; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Assignment Solutions/5-a-lis/lis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define int long long int 6 | int32_t main() { 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | vector d(n+1, 1000000000); 14 | for (int i = 0; i < n; i++) { 15 | *lower_bound(d.begin(), d.end(), a[i]) = a[i]; 16 | } 17 | for (int i = 0; i <= n; i++) { 18 | if (d[i] == 1000000000) { 19 | cout << i << endl; 20 | break; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Assignment Solutions/5-b-edit_distance/edit_distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define rep(i,n) f(i,0,n) 6 | #define f(i,a,b) for(int i=a;i=b;i--) 8 | #define pb push_back 9 | #define mp make_pair 10 | #define vl vector< ll > 11 | #define vi vector< int > 12 | #define ss second 13 | #define ff first 14 | #define ll long long 15 | #define pii pair< int,int > 16 | #define pll pair< ll,ll > 17 | #define sz(a) a.size() 18 | #define inf (1000*1000*1000+5) 19 | #define all(a) a.begin(),a.end() 20 | #define tri pair 21 | #define vii vector 22 | #define vll vector 23 | #define viii vector 24 | #define mod (1000*1000*1000+7) 25 | 26 | int main(){ 27 | std::ios::sync_with_stdio(false); cin.tie(NULL); 28 | int x,y; 29 | cin>>x>>y; 30 | string a,b; 31 | cin>>a; 32 | cin>>b; 33 | int q,d,s; 34 | cin>>q>>d>>s; 35 | int dp[x+1][y+1]; 36 | rep(i,x+1){ 37 | rep(j,y+1){ 38 | if(i==0&&j==0){ 39 | dp[i][j]=0; 40 | } 41 | else if(i==0){ 42 | dp[i][j]=dp[0][j-1]+q; 43 | } 44 | else if(j==0){ 45 | dp[i][j]=dp[i-1][0]+d; 46 | } 47 | else if(a[i-1]==b[j-1]){ 48 | dp[i][j]=dp[i-1][j-1]; 49 | } 50 | else { 51 | 52 | dp[i][j]= min( 53 | dp[i][j-1]+q, 54 | min( 55 | dp[i-1][j]+d, 56 | dp[i-1][j-1]+s 57 | ) 58 | ); 59 | } 60 | } 61 | } 62 | // rep(i,x+1){ 63 | // rep(j,y+1){ 64 | // cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | cin.sync_with_stdio(false); 9 | cin.tie(0); 10 | 11 | int n, k; 12 | cin >> n >> k; 13 | vector> prefixSum(n + 1, vector(n + 1)); 14 | int ans = 0; 15 | 16 | for (int i = 1; i <= n; ++i) { 17 | for (int j = 1; j <= n; ++j) { 18 | int x; 19 | cin >> x; 20 | prefixSum[i][j] = x + prefixSum[i][j - 1] + prefixSum[i - 1][j] - prefixSum[i - 1][j - 1]; 21 | if (i >= k && j >= k) { 22 | ans = max(ans, prefixSum[i][j] - prefixSum[i][j - k] - prefixSum[i - k][j] + prefixSum[i - k][j - k]); 23 | } 24 | } 25 | } 26 | 27 | cout << ans << '\n'; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Assignment Solutions/6-a-knapsack/knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int max(int a, int b) { return (a > b)? a : b; } 6 | 7 | void printknapSack(int W, int wt[], int val[], int n) 8 | { 9 | int i, w; 10 | int K[n + 1][W + 1]; 11 | 12 | for (i = 0; i <= n; i++) { 13 | for (w = 0; w <= W; w++) { 14 | if (i == 0 || w == 0) 15 | K[i][w] = 0; 16 | else if (wt[i - 1] <= w) 17 | K[i][w] = max(val[i - 1] + 18 | K[i - 1][w - wt[i - 1]], K[i - 1][w]); 19 | else 20 | K[i][w] = K[i - 1][w]; 21 | } 22 | } 23 | 24 | int res = K[n][W]; 25 | 26 | w = W; 27 | vector ans; 28 | for (i = n; i > 0 && res > 0; i--) { 29 | 30 | if (res == K[i - 1][w]) 31 | continue; 32 | else { 33 | ans.push_back(i ); 34 | 35 | res = res - val[i - 1]; 36 | w = w - wt[i - 1]; 37 | } 38 | } 39 | cout<=0;i--){ 41 | cout<> n >> W; 49 | int w[n],v[n]; 50 | for (int i = 0; i < n; ++i) 51 | cin >> w[i] >> v[i]; 52 | 53 | printknapSack(W, w, v, n); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Assignment Solutions/6-b-chain_matrix/chain_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | int MatrixChainOrder(int p[], int n) 5 | { 6 | int m[n][n]; 7 | 8 | int i, j, k, L, q; 9 | for (i = 1; i < n; i++) 10 | m[i][i] = 0; 11 | for (L = 2; L < n; L++) 12 | { 13 | for (i = 1; i < n - L + 1; i++) 14 | { 15 | j = i + L - 1; 16 | m[i][j] = INT_MAX; 17 | for (k = i; k <= j - 1; k++) 18 | { 19 | q = m[i][k] + m[k + 1][j] + 20 | p[i - 1] * p[k] * p[j]; 21 | if (q < m[i][j]) 22 | m[i][j] = q; 23 | } 24 | } 25 | } 26 | 27 | return m[1][n - 1]; 28 | } 29 | 30 | int32_t main() { 31 | int n; 32 | cin >> n; 33 | int m[n+1]; 34 | for (int i = 0; i <= n; ++i) 35 | cin >> m[i]; 36 | cout << MatrixChainOrder(m,n+1) << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Assignment Solutions/6-b-chain_matrix/maximumvalue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::vector; 3 | using std::string; 4 | using std::max; 5 | using std::min; 6 | using namespace std; 7 | 8 | long long eval(long long a, long long b, char op) { 9 | if (op == '*') { 10 | return a * b; 11 | } else if (op == '+') { 12 | return a + b; 13 | } else if (op == '-') { 14 | return a - b; 15 | } else { 16 | assert(0); 17 | } 18 | } 19 | 20 | long long get_maximum_value(const string &exp) { 21 | int length = exp.size(); 22 | int numOfnum = (length + 1) / 2; 23 | long long minArray[numOfnum][numOfnum]; 24 | long long maxArray[numOfnum][numOfnum]; 25 | memset(minArray,0,sizeof(minArray)); // initialize to 0 26 | memset(maxArray,0,sizeof(maxArray)); 27 | for (int i = 0; i < numOfnum; i++) 28 | { 29 | //The values on the main diagonal is just the number themselves 30 | minArray[i][i] = stoll(exp.substr(2*i,1)); 31 | maxArray[i][i] = stoll(exp.substr(2*i,1)); 32 | } 33 | 34 | for (int s = 0; s < numOfnum - 1; s++) 35 | { 36 | for (int i = 0; i < numOfnum - s - 1; i++) 37 | { 38 | int j = i + s + 1; 39 | long long minVal = LLONG_MAX; 40 | long long maxVal = LLONG_MIN; 41 | // find the minimum and maximum values for the expression 42 | // between the ith number and jth number 43 | for (int k = i; k < j; k++ ) 44 | { 45 | long long a = eval(minArray[i][k],minArray[k + 1][j],exp[2 * k + 1]); 46 | long long b = eval(minArray[i][k],maxArray[k + 1][j],exp[2 * k + 1]); 47 | long long c = eval(maxArray[i][k],minArray[k + 1][j],exp[2 * k + 1]); 48 | long long d = eval(maxArray[i][k],maxArray[k + 1][j],exp[2 * k + 1]); 49 | minVal = min(minVal,min(a,min(b,min(c,d)))); 50 | maxVal = max(maxVal,max(a,max(b,max(c,d)))); 51 | } 52 | minArray[i][j] = minVal; 53 | maxArray[i][j] = maxVal; 54 | } 55 | } 56 | 57 | return maxArray[0][numOfnum - 1]; 58 | } 59 | 60 | int main() { 61 | string s; 62 | std::cin >> s; 63 | std::cout << get_maximum_value(s) << '\n'; 64 | } -------------------------------------------------------------------------------- /Assignment Solutions/6-c-longest-common-subsequence/6-C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | int *array1 = new int[n]; 9 | int *array2 = new int[n]; 10 | for(int i = 0; i < n; i++) { 11 | cin>>array1[i]; 12 | } 13 | for(int i = 0; i < n; i++) { 14 | cin>>array2[i]; 15 | } 16 | 17 | int **matrix = new int*[n+1]; 18 | for(int i = 0; i <= n; i++) { 19 | matrix[i] = new int[n+1]; 20 | for(int j = 0; j <= n; j++) { 21 | matrix[i][j] = 0; 22 | } 23 | } 24 | for(int i = 1; i <= n; i++) { 25 | for(int j = 1; j <= n; j++) { 26 | if(array1[i-1] == array2[j-1]) { 27 | 28 | matrix[i][j] = matrix[i-1][j-1] + 1; 29 | } else { 30 | matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1]); 31 | } 32 | } 33 | } 34 | int *lcs = new int[matrix[n][n]]; 35 | int count = matrix[n][n]; 36 | cout< 0) { 39 | if(matrix[i-1][j] == matrix[i][j]) { 40 | // Value came from smaller array1 41 | i--; 42 | } else if(matrix[i][j-1] == matrix[i][j]) { 43 | // Value came from smaller array2 44 | j--; 45 | } else { 46 | // Value came from the letter at the current index 47 | lcs[count-1] = array1[i-1]; 48 | i--; 49 | j--; 50 | count--; 51 | } 52 | } 53 | int ind=0; 54 | for(int i = 0; i < n; i++) { 55 | if(array1[i]==lcs[ind]){ 56 | cout<