From dbb93b241219f3cd426cab7361d766722b947e08 Mon Sep 17 00:00:00 2001 From: CHANDRAMANI KUMAR PATEL <68929800+CHANDRAMANI70@users.noreply.github.com> Date: Fri, 2 Oct 2020 17:44:31 +0530 Subject: [PATCH 1/2] Create candies_greedy_problems.cpp --- CPP/candies_greedy_problems.cpp | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 CPP/candies_greedy_problems.cpp diff --git a/CPP/candies_greedy_problems.cpp b/CPP/candies_greedy_problems.cpp new file mode 100644 index 0000000..778a253 --- /dev/null +++ b/CPP/candies_greedy_problems.cpp @@ -0,0 +1,62 @@ +/*Alice is a kindergarten teacher. She wants to give some candies to the children in her class. All the children sit in a line and each of them has a rating score according to his or her performance in the class. Alice wants to give at least 1 candy to each child. If two children sit next to each other, then the one with the higher rating must get more candies. Alice wants to minimize the total number of candies she must buy. +For example, assume her students' ratings are [4, 6, 4, 5, 6, 2]. She gives the students candy in the following minimal amounts: [1, 2, 1, 2, 3, 1]. She must buy a minimum of 10 candies.*? + + +#include + +using namespace std; + +long candies(int n, vector arr) { + long *help1 = new long[n]; + for(int i=0;i=0;i--){ + if(arr[i]>arr[i+1]){ + help1[i]+=help1[i+1]; + continue; + } + } + for(int i=1;iarr[i-1]){ + help2[i]+=help2[i-1]; + continue; + } + } + for(int i=0; i> n; + cin.ignore(numeric_limits::max(), '\n'); + + vector arr(n); + + for (int i = 0; i < n; i++) { + int arr_item; + cin >> arr_item; + cin.ignore(numeric_limits::max(), '\n'); + + arr[i] = arr_item; + } + + long result = candies(n, arr); + + cout << result << "\n"; + + return 0; +} + + From 0744e3080b6a90d75e147dfe199cec90e0a6706e Mon Sep 17 00:00:00 2001 From: CHANDRAMANI KUMAR PATEL <68929800+CHANDRAMANI70@users.noreply.github.com> Date: Fri, 30 Oct 2020 19:30:50 +0530 Subject: [PATCH 2/2] Add files via upload --- Detection of Linked list.cpp | 126 +++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 Detection of Linked list.cpp diff --git a/Detection of Linked list.cpp b/Detection of Linked list.cpp new file mode 100644 index 0000000..81e211e --- /dev/null +++ b/Detection of Linked list.cpp @@ -0,0 +1,126 @@ +/*A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.*/ + +#include + +using namespace std; + +class SinglyLinkedListNode { + public: + int data; + SinglyLinkedListNode *next; + + SinglyLinkedListNode(int node_data) { + this->data = node_data; + this->next = nullptr; + } +}; + +class SinglyLinkedList { + public: + SinglyLinkedListNode *head; + SinglyLinkedListNode *tail; + + SinglyLinkedList() { + this->head = nullptr; + this->tail = nullptr; + } + + void insert_node(int node_data) { + SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); + + if (!this->head) { + this->head = node; + } else { + this->tail->next = node; + } + + this->tail = node; + } +}; + +void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { + while (node) { + fout << node->data; + + node = node->next; + + if (node) { + fout << sep; + } + } +} + +void free_singly_linked_list(SinglyLinkedListNode* node) { + while (node) { + SinglyLinkedListNode* temp = node; + node = node->next; + + free(temp); + } +} + +bool has_cycle(SinglyLinkedListNode* head) { + SinglyLinkedListNode* temp1=head; + SinglyLinkedListNode* temp2=head; + if(head==NULL){ + return false; + } + while(temp1!=NULL&&temp2!=NULL&&temp2->next!=NULL){ + temp1=temp1->next; + temp2=temp2->next->next; + if(temp1==temp2){ + return true; + } + } + return false; +} + + +int main() +{ + + int tests; + cin >> tests; + cin.ignore(numeric_limits::max(), '\n'); + + for (int tests_itr = 0; tests_itr < tests; tests_itr++) { + int index; + cin >> index; + cin.ignore(numeric_limits::max(), '\n'); + + SinglyLinkedList* llist = new SinglyLinkedList(); + + int llist_count; + cin >> llist_count; + cin.ignore(numeric_limits::max(), '\n'); + + for (int i = 0; i < llist_count; i++) { + int llist_item; + cin >> llist_item; + cin.ignore(numeric_limits::max(), '\n'); + + llist->insert_node(llist_item); + } + + SinglyLinkedListNode* extra = new SinglyLinkedListNode(-1); + SinglyLinkedListNode* temp = llist->head; + + for (int i = 0; i < llist_count; i++) { + if (i == index) { + extra = temp; + } + + if (i != llist_count-1) { + temp = temp->next; + } + } + + temp->next = extra; + + bool result = has_cycle(llist->head); + + cout << result << "\n"; + } + + return 0; +}