diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml index 3db88c38..c5005fed 100644 --- a/.github/workflows/codespell.yml +++ b/.github/workflows/codespell.yml @@ -8,7 +8,9 @@ jobs: codespell: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.x - run: pip install codespell - run: codespell --ignore-words-list="ans,nnumber,nin,Hel" --quiet-level=2 --skip="**/**/package-lock.json,./docs/pt,./docs/es,./docs/tr,./.github,./algorithms/CSharp/test" diff --git a/algorithms/CPlusPlus/Arrays/balanced-parenthesis.cpp b/algorithms/CPlusPlus/Arrays/balanced-parenthesis.cpp new file mode 100644 index 00000000..3f271bed --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/balanced-parenthesis.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +bool areBracketsBalanced (string expr) +{ + + stack < char >s; + char x; + + + // Traversing the Expression + for (int i = 0; i < expr.length (); i++) + if (expr[i] == '(' || expr[i] == '[' ||expr[i] == '{') + { + // Push the element in the stack + s.push (expr[i]); + continue; + } + + // IF current current character is not opening + // bracket, then it must be closing. So stack + // cannot be empty at this point. + + if (s.empty ()) + return false; + +switch (expr[i]) +{ + + case ')': // Store the top element in a + x = s.top (); + s.pop (); + + if (x == '{' || x == '[') + return false; + + break; + +case '}': // Store the top element in b + x = s.top (); + s.pop (); + if (x == '(' || x == '[') + return false; + break; +case ']': x = s.top (); + s.pop (); + if (x == '(' || x == '{') + return false; + break; + } + } +return (s.empty ()); +} +// Driver code +int main () +{ + string expr = "{()}[]"; + // Function call + if (areBracketsBalanced (expr)) + cout << "Balanced"; +else + cout << "Not Balanced"; + return 0; +} + +// Output:- +// Enter the brackets to check if its balanced or not : [{}] +// Balanced +// Enter the brackets to check if its balanced or not : {] +Not Balanced diff --git a/algorithms/CPlusPlus/Arrays/max-subarray-sum.cpp b/algorithms/CPlusPlus/Arrays/max-subarray-sum.cpp index 98100664..df8872fd 100644 --- a/algorithms/CPlusPlus/Arrays/max-subarray-sum.cpp +++ b/algorithms/CPlusPlus/Arrays/max-subarray-sum.cpp @@ -20,7 +20,7 @@ int maxSubArrSum_A(int a[],int n){ return maxSum; } -// Appraoch B - Cumulative Sum Approach O(n^2) +// Approach B - Cumulative Sum Approach O(n^2) int maxSubArrSum_B(int a[],int n){ int currSum[n+1]; currSum[0] = 0; for(int i=1;i<=n;++i){ @@ -66,4 +66,4 @@ int main(){ cout< +#include +#include + +using namespace std; + +// Function to find the maximum revenue from cutting a rod of length (len) +// where the rod of length (i) has cost (prices[i - 1]) +int RodCutting(vector &prices, int len) +{ + // (dp) stores the maximum revenue achieved from cutting a rod of length (from 1 to len) + vector dp(len + 1, 0); + // If the rod length is negative (invalid) or zero there's no revenue from it + if (len <= 0) + { + return 0; + } + // Cut a rod of length (i) + for (int i = 1; i <= len; i++) + { + // divide the rod of length (i) into two rods of lengths (j) and (i - j) + // and store the maximum revenue + for (int j = 0; j < i; j++) + { + // (dp[i]) stores the maximum revenue achieved from cutting a rod of length (i) + dp[i] = max(dp[i], prices[j] + dp[i - j - 1]); + } + } + // (dp[len]) contains the maximum revenue from cutting a rod of length (len) + return dp[len]; +} + +int main() +{ + int len; + cout << "Enter the rod length :"; + cin >> len; + + vector prices(len); + for (int i = 1; i <= len; i++) + { + cout << "Enter the price of the rod of length " << i << " :"; + cin >> prices[i - 1]; + } + + cout << "Maximum revenue = " << RodCutting(prices, len); + return 0; +} + + +// Input and output: +// 1. prices[] = [1, 5, 8, 9, 10, 17, 17, 20] +// rod length = 4 +// Best: Cut the rod into two pieces of length 2 each to gain revenue of 5 + 5 = 10 + +// 2. prices[] = [1, 5, 8, 9, 10, 17, 17, 20] +// rod length = 8 +// Best: Cut the rod into two pieces of length 2 and 6 to gain revenue of 5 + 17 = 22 + +// 3. prices[] = [3, 5, 8, 9, 10, 17, 17, 20] +// rod length = 8 +// Best: Cut the rod into eight pieces of length 1 to gain revenue of 8 * 3 = 24 diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index a11b3b58..6054718c 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -31,7 +31,7 @@ - [Next permutation](Arrays/next-permutation.cpp) - [Maximum Minimum Average of numbers](Arrays/max-min-avg.cpp) - [Sparse Matrix](Arrays/sparse_matrix.cpp) - +- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp) ## Dynamic-Programming @@ -42,6 +42,7 @@ - [Matrix chain Multiplication](Dynamic-Programming/matrix-chain-multiplication.cpp) - [Edit Distance](Dynamic-Programming/edit-distance.cpp) - [Fibonacci](Dynamic-Programming/fibonacci.cpp) +- [Rod Cutting](Dynamic-Programming/rod-cutting.cpp) ## Graphs @@ -156,7 +157,7 @@ ## Trie - [Trie for searching](Trie/trie_search.cpp) -- [Trie for insert search and prefix_search](Trie/trie_startWith.cpp) +- [Trie for prefix_search](Trie/trie_startWith.cpp) - [Trie for delete](Trie/trie_delete.cpp) # Maths diff --git a/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp b/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp new file mode 100644 index 00000000..8c73a57c --- /dev/null +++ b/algorithms/CPlusPlus/Trie/trie_insert_search_startWith.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +typedef long long ll; +#define inf 1e9; +#define inf2 2e18; + + + +struct custom_hash { + static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);} + size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } +}; + + + +struct TrieNode{ TrieNode* child[26]; bool isEnd; + TrieNode(){ isEnd = false; for(int i = 0; i<26; i++){ child[i] = NULL; } } +}; +struct TrieNode* rootTrie; +void addTrie(string& s){ TrieNode* curr = rootTrie; for(int i = 0; ichild[n] == NULL){curr->child[n] = new TrieNode();} curr = curr->child[n]; } curr->isEnd = true; } +bool searchTrie(string& s){TrieNode* curr = rootTrie;for(int i = 0; ichild[n]) return false;curr = curr->child[n];}return curr->isEnd;} +bool startsWithTrie(string s) {int n = s.length();TrieNode* curr = rootTrie;for(int i =0 ; ichild[k] == NULL) return false;curr = curr->child[k];}return true;} + + + +int main(){ +//Jai Shree Ram + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + string keys[] = {"the", "a", "there", "answer", "any", "by", "bye", "their" }; + int n = sizeof(keys)/sizeof(keys[0]); + struct TrieNode *root = getNode(); + + for (int i = 0; i < n; i++) insert(root, keys[i]); + + char output[][32] = {"Not present in trie", "Present in trie"}; + + cout<<"the"<<" --- "< arr[i] + max_sum_curr + else + max_sum_curr = max_sum_curr + arr[i] + max_sum = max_sum if max_sum > max_sum_curr + else + max_sum = max_sum_curr +end +return max_sum +""" + +def max_sub_arr_sum(arr): + arr_size = len(arr) + max_sum = arr[0] + max_sum_curr = arr[0] + + for i in range(1, arr_size): + max_sum_curr = max(arr[i], max_sum_curr + arr[i]) + max_sum = max(max_sum, max_sum_curr) + + return max_sum + + +# print("Enter array of numbers (Ex: 1 2 3 4 for [1, 2, 3, 4])") +arr = [3, 2, -4, 9] # list(map(int, input().split())) +print("Maximum Sub Array Sum is", max_sub_arr_sum(arr)) diff --git a/docs/en/README.md b/docs/en/README.md index 65b4a7cf..924afa1a 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -1,11 +1,17 @@ # Algorithms +## Backtracking +- [N-Queens](./Backtracking/N-Queens.md) + ## Lists - [Singly linked list](./Lists/singly-linked-list.md) - [Doubly linked list](./Lists/doubly-linked-list.md) -## Sorting +## Searching +- [Binary Search](./Searching/Binary-Search.MD) +- [Linear Search](./Searching/Linear-Search.md) +## Sorting - [Bubble Sort](./Sorting/Bubble-Sort.md) - [Merge Sort](./Sorting/Merge-Sort.md) - [Selection Sort](./Sorting/Selection-Sort.md) @@ -13,16 +19,13 @@ - [Heap Sort](./Sorting/Heap-Sort.md) - [Quick Sort](./Sorting/Quick-Sort.md) - [Cycle Sort](./Sorting/Cycle-Sort.md) +- [Radix Sort](./Sorting/Radix-Sort.md) ## Strings - - [Palindrome](./Strings/Palindrome.md) -## Searching - -- [Binary Search](./Searching/Binary-Search.MD) -- [Linear Search](./Searching/Linear-Search.md) +## Tree +- [Min Heap](./Tree/min-heap.md) ## Others - [How to add new algorithm documentation?](./CONTRIBUTING.md)