Programming/C C++

    [DS] Delete a Node

    www.hackerrank.com/challenges/delete-a-node-from-a-linked-list/problem position이 주어지고 해당 position의 노드를 삭제하는 코드를 작성해야 한다. SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) { SinglyLinkedListNode* temp = head; SinglyLinkedListNode* tmp; if(position == 0) { head = head->next; return head; } for(int i=0;inext; } tmp = temp->next; temp->next = tmp->next; free(tmp); return hea..

    Migratory Birds

    www.hackerrank.com/challenges/migratory-birds/problem 배열을 이용해 값을 비교해 최대의 값을 가지는 타입을 출력하는 프로그램이다. // Complete the migratoryBirds function below. int migratoryBirds(int arr_count, int* arr) { int common=0; int nums[5] = {0,}; for(int i=0; i

    [DS] Insert a node at a specific position in a linked list

    link /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) { SinglyLinkedListNode *node = malloc(sizeof(SinglyLinkedListNode)); node->data = data; node->next=NULL; SinglyLinkedListNode *tmp = head; for(int i=0;inext; } node->next = tmp->next; tmp->next=..

    Divisible Sum Pairs

    www.hackerrank.com/challenges/divisible-sum-pairs/problem int divisibleSumPairs(int n, int k, int ar_count, int* ar) { int pair=0; int sum=0; for(int i=0;i

    [DS] Insert a node at the head of a linked list

    www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem 지난 번 DS 문제는 말단 노드에 새로운 노드를 삽입하는 문제였다면 이번 문제는 head 노드에 새로운 노드를 삽입하는 문제이다. 이 경우가 말단에 삽입할 때보다 더 간단한 것 같다. SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode)); node->data = data; node->next = NULL; if(llist == NULL) llist = nod..

    Birthday Chocolate

    www.hackerrank.com/challenges/the-birthday-bar/problem m(month)와 d(day) 값을 이용해서 정수배열 s에서 연속된 m개의 숫자의 합이 d인 경우의 수를 출력하는 코드이다. 전체적으로 쉽고 주의해야 할 부분은 반복문의 횟수(조건 부분)이다. #include #include #include #include #include #include #include #include #include #include char* readline(); char* ltrim(char*); char* rtrim(char*); char** split_string(char*); // Complete the birthday function below. int birthday(int s..

    [DS] Insert a Node at the Tail of a Linked List

    https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem 연결리스트의 tail, 끝 부분에 노드를 삽입하는 함수를 작성해야 한다. SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { SinglyLinkedListNode* node = head; SinglyLinkedListNode* tmp = malloc(sizeof(SinglyLinkedList)); tmp->data = data; tmp->next = NULL; if(head==NULL){ head = tmp; return head; } while(node->n..

    Breaking the Records

    https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem 문제는 쉬운 문제인데 함수의 리턴 타입을 고려해서 풀어야하는 문제이다. int* breakingRecords(int scores_count, int* scores, int* result_count) { int *breaking=malloc(sizeof(int)*2); breaking[0]=0; breaking[1]=0; int min=scores[0], max=scores[0]; for(int i=1;iscores[i]) { breaking[1]++; min=scores[i]; } if(max

    Print the Elements of a Linked List

    https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ void printLinkedList(SinglyLinkedListNode* head) { SinglyLinkedListNode *node=head; while(node!=NULL){ printf("%d\n",node->data); node=node->next; } } △ 작성한 부분 연결 리스트의 원소들의 값을 출력하는 함수로, 해당 노드의 값이 NULL이 되기 전까지 연결 ..

    Kangaroo

    https://www.hackerrank.com/challenges/kangaroo/problem 두 캥거루가 같은 시간에 같은 지점에 도착하는 경우에 대해 판단하는 문제이다. 어려운 문제는 아니지만 캥거루들이 움직일 수 있는 길이의 한도가 정해져있지 않아 그 부분을 어떻게 해야하나 고민했다. char* kangaroo(int x1, int v1, int x2, int v2) { int s1=0, s2=0; if((x1v2)) return "NO"; else{ for(int i=0;;i++){ s1= x1+v1*i; s2 = x2 + v2 *i; if(s1 == s2) return "YES"; if(x1s2) return "NO"; } else{ if(s1> return "NO" 그 다음 만날 수도 있고..