Skip to content

Commit 711b6fc

Browse files
Merge branch 'acesdit:main' into main
2 parents afe8812 + 72fa75c commit 711b6fc

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int constrainedSubsetSum(vector<int>& v, int k) {
4+
int n=v.size();
5+
vector<int>dp(n,0);
6+
priority_queue<pair<int,int>>pq;
7+
pq.push({v[0],0});
8+
dp[0]=v[0];
9+
int ans=v[0];
10+
for(int i=1;i<n;i++){
11+
while(!pq.empty()){
12+
auto p=pq.top();
13+
if(i-p.second>k){
14+
pq.pop();
15+
}
16+
else{
17+
break;
18+
}
19+
}
20+
dp[i]=max(v[i],v[i]+pq.top().first);
21+
ans=max(ans,dp[i]);
22+
pq.push({dp[i],i});
23+
}
24+
return ans;
25+
}
26+
};

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ We are open to new additions and suggestions. Contribute by adding more snippets
171171
<br />
172172
<sub><b>Vardhaman619</b></sub>
173173
</a>
174+
</td>
175+
<td align="center">
176+
<a href="https://github.com/nikhil730">
177+
<img src="https://avatars.githubusercontent.com/u/79016958?v=4" width="100;" alt="nikhil730"/>
178+
<br />
179+
<sub><b>nikhil730</b></sub>
180+
</a>
174181
</td></tr>
175182
</table>
176183
<!-- readme: collaborators,contributors -end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
extension: cpp
3+
author: nikhil730
4+
category: Algorithms
5+
layout: '../../layouts/SubmissionLayout.astro'
6+
title: Constrained Subsequence Sum
7+
---
8+
```cpp
9+
class Solution {
10+
public:
11+
int constrainedSubsetSum(vector<int>& v, int k) {
12+
int n=v.size();
13+
vector<int>dp(n,0);
14+
priority_queue<pair<int,int>>pq;
15+
pq.push({v[0],0});
16+
dp[0]=v[0];
17+
int ans=v[0];
18+
for(int i=1;i<n;i++){
19+
while(!pq.empty()){
20+
auto p=pq.top();
21+
if(i-p.second>k){
22+
pq.pop();
23+
}
24+
else{
25+
break;
26+
}
27+
}
28+
dp[i]=max(v[i],v[i]+pq.top().first);
29+
ans=max(ans,dp[i]);
30+
pq.push({dp[i],i});
31+
}
32+
return ans;
33+
}
34+
};
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
extension: cpp
3+
author: AnkurSalunkhe11
4+
category: Algorithms
5+
layout: '../../layouts/SubmissionLayout.astro'
6+
title: Huffman Compression Algorithm
7+
---
8+
```cpp
9+
#include <iostream>
10+
#include <string>
11+
#include <queue>
12+
#include <unordered_map>
13+
using namespace std;
14+
15+
#define EMPTY_STRING ""
16+
17+
// A Tree node
18+
struct Node
19+
{
20+
char ch;
21+
int freq;
22+
Node *left, *right;
23+
};
24+
25+
// Function to allocate a new tree node
26+
Node* getNode(char ch, int freq, Node* left, Node* right)
27+
{
28+
Node* node = new Node();
29+
30+
node->ch = ch;
31+
node->freq = freq;
32+
node->left = left;
33+
node->right = right;
34+
35+
return node;
36+
}
37+
38+
// Comparison object to be used to order the heap
39+
struct comp
40+
{
41+
bool operator()(const Node* l, const Node* r) const
42+
{
43+
// the highest priority item has the lowest frequency
44+
return l->freq > r->freq;
45+
}
46+
};
47+
48+
// Utility function to check if Huffman Tree contains only a single node
49+
bool isLeaf(Node* root) {
50+
return root->left == nullptr && root->right == nullptr;
51+
}
52+
53+
// Traverse the Huffman Tree and store Huffman Codes in a map.
54+
void encode(Node* root, string str, unordered_map<char, string> &huffmanCode)
55+
{
56+
if (root == nullptr) {
57+
return;
58+
}
59+
60+
// found a leaf node
61+
if (isLeaf(root)) {
62+
huffmanCode[root->ch] = (str != EMPTY_STRING) ? str : "1";
63+
}
64+
65+
encode(root->left, str + "0", huffmanCode);
66+
encode(root->right, str + "1", huffmanCode);
67+
}
68+
69+
// Traverse the Huffman Tree and decode the encoded string
70+
void decode(Node* root, int &index, string str)
71+
{
72+
if (root == nullptr) {
73+
return;
74+
}
75+
76+
// found a leaf node
77+
if (isLeaf(root))
78+
{
79+
cout << root->ch;
80+
return;
81+
}
82+
83+
index++;
84+
85+
if (str[index] == '0') {
86+
decode(root->left, index, str);
87+
}
88+
else {
89+
decode(root->right, index, str);
90+
}
91+
}
92+
93+
// Builds Huffman Tree and decodes the given input text
94+
void buildHuffmanTree(string text)
95+
{
96+
// base case: empty string
97+
if (text == EMPTY_STRING) {
98+
return;
99+
}
100+
101+
// count the frequency of appearance of each character
102+
// and store it in a map
103+
unordered_map<char, int> freq;
104+
for (char ch: text) {
105+
freq[ch]++;
106+
}
107+
108+
// Create a priority queue to store live nodes of the Huffman tree
109+
priority_queue<Node*, vector<Node*>, comp> pq;
110+
111+
// Create a leaf node for each character and add it
112+
// to the priority queue.
113+
for (auto pair: freq) {
114+
pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
115+
}
116+
117+
// do till there is more than one node in the queue
118+
while (pq.size() != 1)
119+
{
120+
// Remove the two nodes of the highest priority
121+
// (the lowest frequency) from the queue
122+
123+
Node* left = pq.top(); pq.pop();
124+
Node* right = pq.top(); pq.pop();
125+
126+
// create a new internal node with these two nodes as children and
127+
// with a frequency equal to the sum of the two nodes' frequencies.
128+
// Add the new node to the priority queue.
129+
130+
int sum = left->freq + right->freq;
131+
pq.push(getNode('\0', sum, left, right));
132+
}
133+
134+
// `root` stores pointer to the root of Huffman Tree
135+
Node* root = pq.top();
136+
137+
// Traverse the Huffman Tree and store Huffman Codes
138+
// in a map. Also, print them
139+
unordered_map<char, string> huffmanCode;
140+
encode(root, EMPTY_STRING, huffmanCode);
141+
142+
cout << "Huffman Codes are:\n" << endl;
143+
for (auto pair: huffmanCode) {
144+
cout << pair.first << " " << pair.second << endl;
145+
}
146+
147+
cout << "\nThe original string is:\n" << text << endl;
148+
149+
// Print encoded string
150+
string str;
151+
for (char ch: text) {
152+
str += huffmanCode[ch];
153+
}
154+
155+
cout << "\nThe encoded string is:\n" << str << endl;
156+
cout << "\nThe decoded string is:\n";
157+
158+
if (isLeaf(root))
159+
{
160+
// Special case: For input like a, aa, aaa, etc.
161+
while (root->freq--) {
162+
cout << root->ch;
163+
}
164+
}
165+
else {
166+
// Traverse the Huffman Tree again and this time,
167+
// decode the encoded string
168+
int index = -1;
169+
while (index < (int)str.size() - 1) {
170+
decode(root, index, str);
171+
}
172+
}
173+
}
174+
175+
// Huffman coding algorithm implementation in C++
176+
int main()
177+
{
178+
string text = "Huffman coding is a data compression algorithm.";
179+
buildHuffmanTree(text);
180+
181+
return 0;
182+
}```

0 commit comments

Comments
 (0)