From 0fcdbf2bdccdd4dc29ef65e623bda93d27100b02 Mon Sep 17 00:00:00 2001 From: Nimish Shah Date: Tue, 1 Sep 2020 00:25:49 +0530 Subject: [PATCH 1/6] fix: Remove repeated algorithm (#1067) * Improve code and add support for 46+ fibb numbers * Docs: Fibbonacci docs added * fix: Add c++ suggested code * fix: remove repeated algorithm --- dynamic_programming/fibonacci_top_down.cpp | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 dynamic_programming/fibonacci_top_down.cpp diff --git a/dynamic_programming/fibonacci_top_down.cpp b/dynamic_programming/fibonacci_top_down.cpp deleted file mode 100644 index 3c0c9a1a3..000000000 --- a/dynamic_programming/fibonacci_top_down.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -using namespace std; -int arr[1000000]; -int fib(int n) { - if (arr[n] == -1) { - if (n <= 1) - arr[n] = n; - else - arr[n] = fib(n - 1) + fib(n - 2); - } - return arr[n]; -} -int main(int argc, char const *argv[]) { - int n; - cout << "Enter n: "; - cin >> n; - for (int i = 0; i < n + 1; ++i) { - arr[i] = -1; - } - cout << "Fibonacci number is " << fib(n) << endl; - return 0; -} \ No newline at end of file From a9da541127da107f4b43546e99620417f7ee3f33 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 31 Aug 2020 18:56:34 +0000 Subject: [PATCH 2/6] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 178e88499..567460083 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -58,7 +58,6 @@ * [Edit Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/edit_distance.cpp) * [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp) * [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp) - * [Fibonacci Top Down](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_top_down.cpp) * [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp) * [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp) * [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp) From d4047b80c52eae64019f9bfc244412a3f9bb8475 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 10:56:15 +0530 Subject: [PATCH 3/6] Delete linked list without head pointer You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Head pointer is not given. --- .../linked_list_delete_without_head.cpp | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 data_structures/linked_list_delete_without_head.cpp diff --git a/data_structures/linked_list_delete_without_head.cpp b/data_structures/linked_list_delete_without_head.cpp new file mode 100644 index 000000000..a2564ddc9 --- /dev/null +++ b/data_structures/linked_list_delete_without_head.cpp @@ -0,0 +1,109 @@ +/** + * @file + * \brief Delete a node in a linked list without the head pointer + * \details + * This algortihm helps delete a node in a linked list without the access of a head pointer + * @author rakshaa2000 +*/ + +//Delete without a head pointer. +/*You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Pointer/ reference to head node is not given. +Note: No head reference is given to you.*/ + +/* +Example 1: + +Input: +N = 3 +value[] = {1,2,3} +node = 1 +Output: 2 3 +Explanation: After deleting 1 from the +linked list, we have remaining nodes +as 2 and 3. + +Example 2: + +Input: +N = 4 +value[] = {70,20,60,30} +node = 20 +Output: 40 60 30 +Explanation: After deleting 20 from +the linked list, we have remaining +nodes as 40, 60 and 30. + +*/ + +#include +//Driver Code starts +//Linked list node structure +struct Node{ + int data; + Node *next; + Node(int x){ + data=x; + next=NULL; + } +} *head; + +//To search for the given node +Node *findNode(Node *head, int search){ + Node *cur= head; + while(cur!=NULL){ + if(cur->data==search) + break; + cur=cur->next; + } +} + +//To populate the linked list +void insert(){ + int i, n, value; + Node *temp; + scanf("%d",&n); + for(int i=0; inext=new Node(value); + temp=temp->next; + new Node(value); + temp->next=NULL; + } + } +} + +//To print the entire list +void printList (Node *node){ + while(node!=NULL){ + printf("%d",node->data); + node=node->next; + } + std::cout<next; +} + +//Main function +int main(){ + int k, n, val; + insert(); + scanf("%d",&k); + Node *del= findNode(head,k); + if(del!=NULL && del->next!=NULL){ + deleteNode(del); + } + printList(head); + return 0; +} From d0e1f715f63537bfd57d58fa578f414d457944c2 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:03:56 +0530 Subject: [PATCH 4/6] fixed bugs --- data_structures/linked_list_delete_without_head.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list_delete_without_head.cpp b/data_structures/linked_list_delete_without_head.cpp index a2564ddc9..53b9f288d 100644 --- a/data_structures/linked_list_delete_without_head.cpp +++ b/data_structures/linked_list_delete_without_head.cpp @@ -41,16 +41,16 @@ nodes as 40, 60 and 30. struct Node{ int data; Node *next; - Node(int x){ + explicit Node(int x){ data=x; - next=NULL; + next=nullptr; } } *head; //To search for the given node Node *findNode(Node *head, int search){ Node *cur= head; - while(cur!=NULL){ + while(cur!=nullptr){ if(cur->data==search) break; cur=cur->next; @@ -59,7 +59,7 @@ Node *findNode(Node *head, int search){ //To populate the linked list void insert(){ - int i, n, value; + int i=0, n, value; Node *temp; scanf("%d",&n); for(int i=0; i Date: Fri, 4 Sep 2020 11:16:15 +0530 Subject: [PATCH 5/6] documentation added, testing yet to be added --- greedy_algorithms/jumpgame.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 6f7a2dd18..b3d2c4420 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -6,10 +6,6 @@ * This solution takes in input as a vector and output as a boolean to check if you can reach the last position. * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) */ -//Jump Game: -/*Given an array of non-negative integers, you are initially positioned at the first index of the array. -Each element in the array represents your maximum jump length at that position. -Determine if you are able to reach the last index.*/ #include #include @@ -22,6 +18,11 @@ Determine if you are able to reach the last index.*/ *If yes, then that is the last position you can reach starting from the back. *After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position. */ + /** + * This function implements the above algorithm + * @param vector of nums containing the maximum jump (in steps) from that index + * @return returns bool value whether final index can be reached or not + */ bool canJump(std::vector nums) { auto lastPos = nums.size() - 1; for (auto i = nums.size() - 1; i >= 0; i--) { From 20aaca07a98f56456aa2cbcabef6e20626f5d351 Mon Sep 17 00:00:00 2001 From: Rakshaa Viswanathan <46165429+rakshaa2000@users.noreply.github.com> Date: Fri, 4 Sep 2020 11:26:42 +0530 Subject: [PATCH 6/6] Added test function --- greedy_algorithms/jumpgame.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index b3d2c4420..94b3b2685 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -9,6 +9,7 @@ #include #include +#include //Implements the algorithm /*We name the indices good and bad based on whether we can reach the destination if we start at that position. @@ -19,7 +20,7 @@ *After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position. */ /** - * This function implements the above algorithm + * @brief This function implements the above algorithm * @param vector of nums containing the maximum jump (in steps) from that index * @return returns bool value whether final index can be reached or not */ @@ -33,13 +34,35 @@ bool canJump(std::vector nums) { return lastPos == 0; } + +/** + * @brief Function to test above algorithm + */ +void test(){ + //Test 1 + std::vector num1={4,3,1,0,5}; + assert(canJump(num1)==true); + std::cout<<"Input: "; + for(auto i: num1){ + std::cout< num2={3,2,1,0,4}; + assert(canJump(num2)==false); + std::cout<<"Input: "; + for(auto i: num2){ + std::cout< num={4,3,1,0,5}; - std::cout<