diff --git a/README.md b/README.md index acd8b55..6ec84d7 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,21 @@ The index below is auto-generated. See [update-challenge-list.py](util/update-ch Index ====== -* [Algorithms](#algorithms) (60/409) -* [Data Structures](#data-structures) (11/114) +* [Algorithms](#algorithms) (61/409) +* [Data Structures](#data-structures) (17/114) * [Mathematics](#mathematics) (1/250) Algorithms ------ * [Warmup](#warmup) (9/9) -* [Implementation](#implementation) (8/59) +* [Implementation](#implementation) (10/59) * [Strings](#strings) (11/45) * [Sorting](#sorting) (5/18) * [Search](#search) (3/23) * [Graph Theory](#graph-theory) (2/58) * [Greedy](#greedy) (8/22) -* [Dynamic Programming](#dynamic-programming) (8/94) +* [Dynamic Programming](#dynamic-programming) (10/94) * [Constructive Algorithms](#constructive-algorithms) (2/11) * [Bit Manipulation](#bit-manipulation) (0/25) * [Recursion](#recursion) (3/10) @@ -55,13 +55,13 @@ Algorithms 8 | [Bon App¨¦tit](https://www.hackerrank.com/challenges/bon-appetit) | [Solution & Comment](solution/practice/algorithms/implementation/bon-appetit/solution.py) 9 | [Sock Merchant](https://www.hackerrank.com/challenges/sock-merchant) | [Solution & Comment](solution/practice/algorithms/implementation/sock-merchant/solution.py) 10 | [Drawing Book ](https://www.hackerrank.com/challenges/drawing-book) | WIP -11 | [Cats and a Mouse](https://www.hackerrank.com/challenges/cats-and-a-mouse) | WIP +11 | [Cats and a Mouse](https://www.hackerrank.com/challenges/cats-and-a-mouse) | [Solution & Comment](solution/practice/algorithms/implementation/Cats-and-a-mouse/solution.py) 12 | [Picking Numbers](https://www.hackerrank.com/challenges/picking-numbers) | WIP 13 | [Climbing the Leaderboard](https://www.hackerrank.com/challenges/climbing-the-leaderboard) | WIP -14 | [The Hurdle Race](https://www.hackerrank.com/challenges/the-hurdle-race) | WIP -15 | [Designer PDF Viewer](https://www.hackerrank.com/challenges/designer-pdf-viewer) | WIP -16 | [Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree) | WIP -17 | [Angry Professor](https://www.hackerrank.com/challenges/angry-professor) | WIP +14 | [The Hurdle Race](https://www.hackerrank.com/challenges/the-hurdle-race) | [Solution & Comment](solution/practice/algorithms/implementation/TheHurdleRace/solution.py) +15 | [Designer PDF Viewer](https://www.hackerrank.com/challenges/designer-pdf-viewer) | [Solution & Comment](solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py) +16 | [Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree) | [Solution & Comment](solution/practice/algorithms/implementation/Utopian-Tree/solution.py) +17 | [Angry Professor](https://www.hackerrank.com/challenges/angry-professor) | [Solution & Comment](solution/practice/algorithms/implementation/AngryProfessor/solution.py) 18 | [Beautiful Days at the Movies](https://www.hackerrank.com/challenges/beautiful-days-at-the-movies) | WIP 19 | [Viral Advertising](https://www.hackerrank.com/challenges/strange-advertising) | WIP 20 | [Save the Prisoner!](https://www.hackerrank.com/challenges/save-the-prisoner) | WIP @@ -70,8 +70,8 @@ Algorithms 23 | [Jumping on the Clouds: Revisited](https://www.hackerrank.com/challenges/jumping-on-the-clouds-revisited) | WIP 24 | [Find Digits](https://www.hackerrank.com/challenges/find-digits) | WIP 25 | [Extra Long Factorials](https://www.hackerrank.com/challenges/extra-long-factorials) | WIP -26 | [Append and Delete](https://www.hackerrank.com/challenges/append-and-delete) | WIP -27 | [Sherlock and Squares](https://www.hackerrank.com/challenges/sherlock-and-squares) | WIP +26 | [Append and Delete](https://www.hackerrank.com/challenges/append-and-delete) | [Solution & Comment](solution/practice/algorithms/implementation/Append-and-Delete/solution.py) +27 | [Sherlock and Squares](https://www.hackerrank.com/challenges/sherlock-and-squares) | [Solution & Comment](solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py) 28 | [Library Fine](https://www.hackerrank.com/challenges/library-fine) | WIP 29 | [Cut the sticks](https://www.hackerrank.com/challenges/cut-the-sticks) | WIP 30 | [Non-Divisible Subset](https://www.hackerrank.com/challenges/non-divisible-subset) | WIP @@ -294,7 +294,7 @@ Algorithms ### Dynamic Programming \# | Challenge | Solution :---:|:---:|:---: -1 | [The Coin Change Problem](https://www.hackerrank.com/challenges/coin-change) | WIP +1 | [The Coin Change Problem](https://www.hackerrank.com/challenges/coin-change) | [Solution & Comment](solution/practice/algorithms/dynamic-programming/Coin-Change-Problem/solution.py) 2 | [Equal](https://www.hackerrank.com/challenges/equal) | WIP 3 | [Sherlock and Cost](https://www.hackerrank.com/challenges/sherlock-and-cost) | [Solution & Comment](solution/practice/algorithms/dynamic-programming/sherlock-and-cost/solution.py) 4 | [Kingdom Division](https://www.hackerrank.com/challenges/kingdom-division) | WIP @@ -305,7 +305,7 @@ Algorithms 9 | [Abbreviation](https://www.hackerrank.com/challenges/abbr) | [Solution & Comment](solution/practice/algorithms/dynamic-programming/abbr/solution.py) 10 | [Decibinary Numbers](https://www.hackerrank.com/challenges/decibinary-numbers) | WIP 11 | [Fair Cut](https://www.hackerrank.com/challenges/fair-cut) | WIP -12 | [The Maximum Subarray](https://www.hackerrank.com/challenges/maxsubarray) | WIP +12 | [The Maximum Subarray](https://www.hackerrank.com/challenges/maxsubarray) | [Solution & Comment](solution/practice/algorithms/dynamic-programming/TheMaximumSubarray/solution.py) 13 | [Angry Children 2](https://www.hackerrank.com/challenges/angry-children-2) | WIP 14 | [Sherlock's Array Merging Algorithm](https://www.hackerrank.com/challenges/sherlocks-array-merging-algorithm) | WIP 15 | [Prime Digit Sums](https://www.hackerrank.com/challenges/prime-digit-sums) | WIP @@ -494,14 +494,14 @@ Data Structures ------ * [Arrays](#arrays) (6/6) -* [Linked Lists](#linked-lists) (0/15) -* [Trees](#trees) (0/15) +* [Linked Lists](#linked-lists) (1/15) +* [Trees](#trees) (2/15) * [Balanced Trees](#balanced-trees) (0/3) * [Stacks](#stacks) (0/9) * [Queues](#queues) (0/5) * [Heap](#heap) (0/4) * [Disjoint Set](#disjoint-set) (4/4) -* [Multiple Choice](#multiple-choice) (0/3) +* [Multiple Choice](#multiple-choice) (3/3) * [Trie](#trie) (0/2) * [Advanced](#advanced) (1/48) @@ -518,7 +518,7 @@ Data Structures ### Linked Lists \# | Challenge | Solution :---:|:---:|:---: -1 | [Print the Elements of a Linked List](https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list) | WIP +1 | [Print the Elements of a Linked List](https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list) | [Solution & Comment](solution/practice/data-structures/linked-list/print-the-elements-of-a-linked-list/solution.py) 2 | [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) | WIP 3 | [Insert a node at the head of a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list) | WIP 4 | [Insert a node at a specific position in a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list) | WIP @@ -537,9 +537,9 @@ Data Structures ### Trees \# | Challenge | Solution :---:|:---:|:---: -1 | [Tree: Preorder Traversal](https://www.hackerrank.com/challenges/tree-preorder-traversal) | WIP +1 | [Tree: Preorder Traversal](https://www.hackerrank.com/challenges/tree-preorder-traversal) | [Solution & Comment](solution/practice/data-structures/trees/TreePreorderTraversal/solution.py) 2 | [Tree: Postorder Traversal](https://www.hackerrank.com/challenges/tree-postorder-traversal) | WIP -3 | [Tree: Inorder Traversal](https://www.hackerrank.com/challenges/tree-inorder-traversal) | WIP +3 | [Tree: Inorder Traversal](https://www.hackerrank.com/challenges/tree-inorder-traversal) | [Solution & Comment](solution/practice/data-structures/trees/TreeInorderTraversal/solution.py) 4 | [Tree: Height of a Binary Tree](https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree) | WIP 5 | [Tree : Top View](https://www.hackerrank.com/challenges/tree-top-view) | WIP 6 | [Tree: Level Order Traversal](https://www.hackerrank.com/challenges/tree-level-order-traversal) | WIP @@ -601,9 +601,9 @@ Data Structures ### Multiple Choice \# | Challenge | Solution :---:|:---:|:---: -1 | [ Data Structures MCQ 1](https://www.hackerrank.com/challenges/how-well-do-you-know-trees) | WIP -2 | [ Data Structures MCQ 2](https://www.hackerrank.com/challenges/are-you-an-expert-on-data-structures) | WIP -3 | [ Data Structures MCQ 3](https://www.hackerrank.com/challenges/are-you-an-expert-on-data-structures-1) | WIP +1 | [ Data Structures MCQ 1](https://www.hackerrank.com/challenges/how-well-do-you-know-trees) | [Solution & Comment](solution/practice/data-structures/multiple-choice/how-well-do-you-know-trees/solution.py) +2 | [ Data Structures MCQ 2](https://www.hackerrank.com/challenges/are-you-an-expert-on-data-structures) | [Solution & Comment](solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures/solution.py) +3 | [ Data Structures MCQ 3](https://www.hackerrank.com/challenges/are-you-an-expert-on-data-structures-1) | [Solution & Comment](solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures-1/solution.py) ### Trie \# | Challenge | Solution diff --git a/challenges.json b/challenges.json index 13c1853..35a5bb4 100644 --- a/challenges.json +++ b/challenges.json @@ -3297,4 +3297,4 @@ "name": "mathematics" } ] -} \ No newline at end of file +} diff --git a/solution/practice/algorithms/dynamic-programming/Coin-Change-Problem/solution.py b/solution/practice/algorithms/dynamic-programming/Coin-Change-Problem/solution.py new file mode 100644 index 0000000..fe9861e --- /dev/null +++ b/solution/practice/algorithms/dynamic-programming/Coin-Change-Problem/solution.py @@ -0,0 +1,28 @@ +#!/bin/python3 + +import sys + +def getWays(n, c): + workArea = [0 for i in range(n+1)] + workArea[0] = 1 + countCoin = 0 + + + while(countCoin < len(c)): + + for i in range(1,n+1): + + if(c[countCoin]<=i): + workArea[i] = workArea[i]+workArea[i-c[countCoin]] + countCoin += 1 + + return(workArea[-1]) + + + +n, m = input().strip().split(' ') +n, m = [int(n), int(m)] +c = list(map(int, input().strip().split(' '))) +# Print the number of ways of making change for 'n' units using coins having the values given by 'c' +ways = getWays(n, c) +print(ways) diff --git a/solution/practice/algorithms/dynamic-programming/TheMaximumSubarray/solution.py b/solution/practice/algorithms/dynamic-programming/TheMaximumSubarray/solution.py new file mode 100644 index 0000000..5295908 --- /dev/null +++ b/solution/practice/algorithms/dynamic-programming/TheMaximumSubarray/solution.py @@ -0,0 +1,46 @@ +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# Complete the maxSubarray function below. +def maxSubarray(arr): + sub_array_sum = -sys.maxsize + sub_sequ_sum = arr[0] + current_sum=0 + + for i in arr: + current_sum += i + if sub_array_sum < current_sum: + sub_array_sum = current_sum + if current_sum < 0: + current_sum=0 + for i in range(1, len(arr)): + if arr[i] > 0 and sub_sequ_sum >= 0 : + sub_sequ_sum += arr[i] + elif sub_sequ_sum < 0 and sub_sequ_sum < arr[i]: + sub_sequ_sum = arr[i] + return [sub_array_sum,sub_sequ_sum] + + + + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + t = int(input()) + + for t_itr in range(t): + n = int(input()) + + arr = list(map(int, input().rstrip().split())) + + result = maxSubarray(arr) + + fptr.write(' '.join(map(str, result))) + fptr.write('\n') + + fptr.close() diff --git a/solution/practice/algorithms/implementation/AngryProfessor/solution.py b/solution/practice/algorithms/implementation/AngryProfessor/solution.py new file mode 100644 index 0000000..efb9a70 --- /dev/null +++ b/solution/practice/algorithms/implementation/AngryProfessor/solution.py @@ -0,0 +1,13 @@ +testcase = int(input()) +for i in range(testcase): + n , k = list(map(int,input().split(" "))) + count = 0 + s = list(map(int,input().split(" "))) + for j in s: + if(j <= 0): + count +=1 + if(count >= k): + print("NO") + else: + print("YES") + diff --git a/solution/practice/algorithms/implementation/Append-and-Delete/solution.py b/solution/practice/algorithms/implementation/Append-and-Delete/solution.py new file mode 100644 index 0000000..dda0412 --- /dev/null +++ b/solution/practice/algorithms/implementation/Append-and-Delete/solution.py @@ -0,0 +1,17 @@ +def appendAndDelete(s, t, k): + numSameChars = min(len(s), len(t)) + for i in range(len(t)): + if s[:i] != t[:i]: + numSameChars = i - 1 + break + + diff = len(s) - numSameChars + len(t) - numSameChars + return 'Yes' if (diff <= k and diff % 2 == k % 2) or len(s) + len(t) < k else 'No' + + +if __name__ == '__main__': + s = input() + t = input() + k = int(input()) + result = appendAndDelete(s, t, k) + print(result) \ No newline at end of file diff --git a/solution/practice/algorithms/implementation/Cats-and-a-mouse/solution.py b/solution/practice/algorithms/implementation/Cats-and-a-mouse/solution.py new file mode 100644 index 0000000..6d302b4 --- /dev/null +++ b/solution/practice/algorithms/implementation/Cats-and-a-mouse/solution.py @@ -0,0 +1,16 @@ +def catAndMouse(ca, cb, m): + if abs(m - ca) == abs(m - cb): + return "Mouse C" + else: + return "Cat A" if abs(m - ca) < abs(m - cb) else "Cat B" + +if __name__ == '__main__': + q = int(input()) + for q_itr in range(q): + xyz = input().split() + x = int(xyz[0]) + y = int(xyz[1]) + z = int(xyz[2]) + result = catAndMouse(x, y, z) + + print(result) \ No newline at end of file diff --git a/solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py b/solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py new file mode 100644 index 0000000..f138fb2 --- /dev/null +++ b/solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py @@ -0,0 +1,10 @@ +def designerPdfViewer(h, word): + word_height = [h[ord(c)-ord("a")] for c in word ] + return max(word_height)*len(word) + + +if __name__ == '__main__': + h = list(map(int, input().rstrip().split())) + word = input() + result = designerPdfViewer(h, word) + print(result) diff --git a/solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py b/solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py new file mode 100644 index 0000000..750f47d --- /dev/null +++ b/solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py @@ -0,0 +1,15 @@ +import math + + +def squares(a, b): + return math.floor(math.sqrt(b)) - math.ceil(math.sqrt(a)) + 1 + + +if __name__ == '__main__': + q = int(input()) + for q_itr in range(q): + ab = input().split() + a = int(ab[0]) + b = int(ab[1]) + result = squares(a, b) + print(result) diff --git a/solution/practice/algorithms/implementation/TheHurdleRace/solution.py b/solution/practice/algorithms/implementation/TheHurdleRace/solution.py new file mode 100644 index 0000000..f0a73e6 --- /dev/null +++ b/solution/practice/algorithms/implementation/TheHurdleRace/solution.py @@ -0,0 +1,17 @@ +#!/bin/python3 + +import sys + +def hurdleRace(k, height): + temp = max(height)-k + if (temp > 0): + return temp + else: + return 0 + +if __name__ == "__main__": + n, k = input().strip().split(' ') + n, k = [int(n), int(k)] + height = list(map(int, input().strip().split(' '))) + result = hurdleRace(k, height) + print(result) diff --git a/solution/practice/algorithms/implementation/Utopian-Tree/solution.py b/solution/practice/algorithms/implementation/Utopian-Tree/solution.py new file mode 100644 index 0000000..92e0e2e --- /dev/null +++ b/solution/practice/algorithms/implementation/Utopian-Tree/solution.py @@ -0,0 +1,16 @@ +def utopianTree(n): + height = 1 + for i in range(n): + if i % 2 == 0: + height *= 2 + else: + height += 1 + return height + + +if __name__ == '__main__': + t = int(input()) + for t_itr in range(t): + n = int(input()) + result = utopianTree(n) + print(result) \ No newline at end of file diff --git a/solution/practice/data-structures/advanced/kindergarten-adventures/solution.py b/solution/practice/data-structures/advanced/kindergarten-adventures/solution.py new file mode 100644 index 0000000..adbf3e1 --- /dev/null +++ b/solution/practice/data-structures/advanced/kindergarten-adventures/solution.py @@ -0,0 +1,22 @@ +# For student i, we can find two (possibly empty) ranges +# of time when they are available: +# * [0, i - t_i] +# * the first one wrapped around 0, which is (i, i - t_i + n] +# +# To get the available student number, we then have to add +# one to the counting array for each minute mark in these intervals. +# This can be done in O(n) time using the awesome algorithm +# described at http://stackoverflow.com/a/18410273. + +n = int(input()) +A = [0] * (n + 1) +for i, x in enumerate(map(int, input().split())): + r1 = max(-1, i - x) + 1 + r2 = min(max(i, i - x + n) + 1, n) + A[0] += 1 + A[r1] -= 1 + A[i + 1] += 1 + A[r2] -= 1 +for i in range(n): + A[i + 1] += A[i] +print(A.index(max(A)) + 1) diff --git a/solution/practice/data-structures/linked-list/print-the-elements-of-a-linked-list/solution.py b/solution/practice/data-structures/linked-list/print-the-elements-of-a-linked-list/solution.py new file mode 100644 index 0000000..0d5d476 --- /dev/null +++ b/solution/practice/data-structures/linked-list/print-the-elements-of-a-linked-list/solution.py @@ -0,0 +1,17 @@ +# Complete the printLinkedList function below. + +# +# For your reference: +# +# SinglyLinkedListNode: +# int data +# SinglyLinkedListNode next +# +# +def printLinkedList(head): + if head == None: + return; + n = head + while(n != None): + print(n.data) + n = n.next \ No newline at end of file diff --git a/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures-1/solution.py b/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures-1/solution.py new file mode 100644 index 0000000..5216b03 --- /dev/null +++ b/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures-1/solution.py @@ -0,0 +1 @@ +print("Stack") diff --git a/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures/solution.py b/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures/solution.py new file mode 100644 index 0000000..2c9d9f0 --- /dev/null +++ b/solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures/solution.py @@ -0,0 +1 @@ +print("Segment Tree") diff --git a/solution/practice/data-structures/multiple-choice/how-well-do-you-know-trees/solution.py b/solution/practice/data-structures/multiple-choice/how-well-do-you-know-trees/solution.py new file mode 100644 index 0000000..fc9723e --- /dev/null +++ b/solution/practice/data-structures/multiple-choice/how-well-do-you-know-trees/solution.py @@ -0,0 +1 @@ +print("n-1") diff --git a/solution/practice/data-structures/trees/TreeInorderTraversal/solution.py b/solution/practice/data-structures/trees/TreeInorderTraversal/solution.py new file mode 100644 index 0000000..2697ee1 --- /dev/null +++ b/solution/practice/data-structures/trees/TreeInorderTraversal/solution.py @@ -0,0 +1,12 @@ +""" +Node is defined as +self.left (the left child of the node) +self.right (the right child of the node) +self.info (the value of the node) +""" +def inOrder(root): + if root != None : + inOrder(root.left) + print(root.info,end=" ") + inOrder(root.right) + \ No newline at end of file diff --git a/solution/practice/data-structures/trees/TreePreorderTraversal/solution.py b/solution/practice/data-structures/trees/TreePreorderTraversal/solution.py new file mode 100644 index 0000000..bf6e856 --- /dev/null +++ b/solution/practice/data-structures/trees/TreePreorderTraversal/solution.py @@ -0,0 +1,12 @@ +""" +Node is defined as +self.left (the left child of the node) +self.right (the right child of the node) +self.info (the value of the node) +""" +def preOrder(root): + if root != None : + print(root.info,end=" ") + preOrder(root.left) + preOrder(root.right) + return \ No newline at end of file