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; +} + + 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; +}