Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions CPP/candies_greedy_problems.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>

using namespace std;

long candies(int n, vector<int> arr) {
long *help1 = new long[n];
for(int i=0;i<n;i++){
help1[i]=1;
}
long *help2 = new long[n];
for(int i=0;i<n;i++){
help2[i]=1;
}
long long sum=0;
for(int i=n-2;i>=0;i--){
if(arr[i]>arr[i+1]){
help1[i]+=help1[i+1];
continue;
}
}
for(int i=1;i<n;i++){
if(arr[i]>arr[i-1]){
help2[i]+=help2[i-1];
continue;
}
}
for(int i=0; i<n ;i++){
sum+=max(help1[i], help2[i]);
}
delete []help1;
delete []help2;
return sum;
}

int main()
{
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

vector<int> arr(n);

for (int i = 0; i < n; i++) {
int arr_item;
cin >> arr_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

arr[i] = arr_item;
}

long result = candies(n, arr);

cout << result << "\n";

return 0;
}


126 changes: 126 additions & 0 deletions Detection of Linked list.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>

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<streamsize>::max(), '\n');

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
int index;
cin >> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

SinglyLinkedList* llist = new SinglyLinkedList();

int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::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;
}