From 455be01dfd04d601cc6d13bbec3d48003b54c283 Mon Sep 17 00:00:00 2001 From: Abhishek2019 Date: Tue, 6 Feb 2018 22:51:29 +0530 Subject: [PATCH 01/20] Dynamic Programming: Coint-Change-Problem --- README.md | 4 +-- .../Coin-Change-Problem/solution.py | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 solution/practice/algorithms/dynamic-programming/Coin-Change-Problem/solution.py diff --git a/README.md b/README.md index acd8b55..732c788 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Algorithms * [Search](#search) (3/23) * [Graph Theory](#graph-theory) (2/58) * [Greedy](#greedy) (8/22) -* [Dynamic Programming](#dynamic-programming) (8/94) +* [Dynamic Programming](#dynamic-programming) (9/94) * [Constructive Algorithms](#constructive-algorithms) (2/11) * [Bit Manipulation](#bit-manipulation) (0/25) * [Recursion](#recursion) (3/10) @@ -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 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) From 5f6531162f40d95a07de15eb9dbcbde0bf897038 Mon Sep 17 00:00:00 2001 From: Abhishek2019 Date: Tue, 6 Feb 2018 22:59:01 +0530 Subject: [PATCH 02/20] README.md file modified --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 732c788..9f4ab1b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The index below is auto-generated. See [update-challenge-list.py](util/update-ch Index ====== -* [Algorithms](#algorithms) (60/409) +* [Algorithms](#algorithms) (61/409) * [Data Structures](#data-structures) (11/114) * [Mathematics](#mathematics) (1/250) From 4bb49cf1b7f818cedcf4f9ee0145b4a14dec3ad4 Mon Sep 17 00:00:00 2001 From: satyam857 Date: Mon, 22 Oct 2018 17:34:23 +0530 Subject: [PATCH 03/20] Angry Professor Solution Added --- README.md | 4 ++-- .../implementation/AngryProfessor/solution.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 solution/practice/algorithms/implementation/AngryProfessor/solution.py diff --git a/README.md b/README.md index 9f4ab1b..602f0e7 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Algorithms ------ * [Warmup](#warmup) (9/9) -* [Implementation](#implementation) (8/59) +* [Implementation](#implementation) (9/59) * [Strings](#strings) (11/45) * [Sorting](#sorting) (5/18) * [Search](#search) (3/23) @@ -61,7 +61,7 @@ Algorithms 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 +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 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") + From 2edd5aa8aa4994561d7b94bebe0ddceb49f75033 Mon Sep 17 00:00:00 2001 From: satyam857 Date: Mon, 22 Oct 2018 17:43:52 +0530 Subject: [PATCH 04/20] The Hurdle Race Solution is Added --- README.md | 4 ++-- .../implementation/TheHurdleRace/solution.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 solution/practice/algorithms/implementation/TheHurdleRace/solution.py diff --git a/README.md b/README.md index 602f0e7..7da5692 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Algorithms ------ * [Warmup](#warmup) (9/9) -* [Implementation](#implementation) (9/59) +* [Implementation](#implementation) (10/59) * [Strings](#strings) (11/45) * [Sorting](#sorting) (5/18) * [Search](#search) (3/23) @@ -58,7 +58,7 @@ Algorithms 11 | [Cats and a Mouse](https://www.hackerrank.com/challenges/cats-and-a-mouse) | WIP 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 +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) | WIP 16 | [Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree) | WIP 17 | [Angry Professor](https://www.hackerrank.com/challenges/angry-professor) | [Solution & Comment](solution/practice/algorithms/implementation/AngryProfessor/solution.py) 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) From 26f50d513f01c4902994b1c743780a450686542c Mon Sep 17 00:00:00 2001 From: satyam857 Date: Mon, 22 Oct 2018 18:12:48 +0530 Subject: [PATCH 05/20] The Maximum Subarray Solution is Added --- README.md | 4 +- .../TheMaximumSubarray/solution.py | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 solution/practice/algorithms/dynamic-programming/TheMaximumSubarray/solution.py diff --git a/README.md b/README.md index 7da5692..c19830e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Algorithms * [Search](#search) (3/23) * [Graph Theory](#graph-theory) (2/58) * [Greedy](#greedy) (8/22) -* [Dynamic Programming](#dynamic-programming) (9/94) +* [Dynamic Programming](#dynamic-programming) (10/94) * [Constructive Algorithms](#constructive-algorithms) (2/11) * [Bit Manipulation](#bit-manipulation) (0/25) * [Recursion](#recursion) (3/10) @@ -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 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() From b4e114f6b4055b75fc7a85f5b6cf024e93c78b3e Mon Sep 17 00:00:00 2001 From: satyam857 Date: Mon, 22 Oct 2018 18:35:13 +0530 Subject: [PATCH 06/20] Linked List Print the Element problem solved --- README.md | 4 ++-- .../solution.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 solution/practice/data-structures/linked-list/print-the-elements-of-a-linked-list/solution.py diff --git a/README.md b/README.md index c19830e..5787319 100644 --- a/README.md +++ b/README.md @@ -494,7 +494,7 @@ Data Structures ------ * [Arrays](#arrays) (6/6) -* [Linked Lists](#linked-lists) (0/15) +* [Linked Lists](#linked-lists) (1/15) * [Trees](#trees) (0/15) * [Balanced Trees](#balanced-trees) (0/3) * [Stacks](#stacks) (0/9) @@ -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 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 From c244f81e29a00ee991628ea524297ad4ed773312 Mon Sep 17 00:00:00 2001 From: satyam857 Date: Mon, 22 Oct 2018 19:00:57 +0530 Subject: [PATCH 07/20] Solution of Tree Preorder traversal --- README.md | 4 ++-- .../trees/TreePreorderTraversal/solution.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 solution/practice/data-structures/trees/TreePreorderTraversal/solution.py diff --git a/README.md b/README.md index 5787319..4f48599 100644 --- a/README.md +++ b/README.md @@ -495,7 +495,7 @@ Data Structures * [Arrays](#arrays) (6/6) * [Linked Lists](#linked-lists) (1/15) -* [Trees](#trees) (0/15) +* [Trees](#trees) (1/15) * [Balanced Trees](#balanced-trees) (0/3) * [Stacks](#stacks) (0/9) * [Queues](#queues) (0/5) @@ -537,7 +537,7 @@ 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 4 | [Tree: Height of a Binary Tree](https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree) | WIP 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 From a9e472aaab7d548392ae33d15530c23ee822a509 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 Oct 2018 05:57:41 +0000 Subject: [PATCH 08/20] Tree Inorder Traversal Solution is Added --- README.md | 4 ++-- .../trees/TreeInorderTraversal/solution.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 solution/practice/data-structures/trees/TreeInorderTraversal/solution.py diff --git a/README.md b/README.md index 4f48599..c6b17d1 100644 --- a/README.md +++ b/README.md @@ -495,7 +495,7 @@ Data Structures * [Arrays](#arrays) (6/6) * [Linked Lists](#linked-lists) (1/15) -* [Trees](#trees) (1/15) +* [Trees](#trees) (2/15) * [Balanced Trees](#balanced-trees) (0/3) * [Stacks](#stacks) (0/9) * [Queues](#queues) (0/5) @@ -539,7 +539,7 @@ Data Structures :---:|:---:|:---: 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 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 From 312a15a2786df757abc1184c3b3ab3ca6f0c6062 Mon Sep 17 00:00:00 2001 From: Vikas Vishwakarma Date: Tue, 30 Oct 2018 21:40:01 +0530 Subject: [PATCH 09/20] Added Sherlock and squares in inplementation --- .../Sherlock And Squares/solution.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solution/practice/algorithms/implementation/Sherlock And Squares/solution.py 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) From 221dde7a22f2247e96ac0e389c585cad0fee668b Mon Sep 17 00:00:00 2001 From: Vikas Vishwakarma Date: Tue, 30 Oct 2018 21:56:18 +0530 Subject: [PATCH 10/20] Adder Designer Pdf Viewer in implementation --- .../implementation/Designer PDF Viewer/solution.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 solution/practice/algorithms/implementation/Designer PDF Viewer/solution.py 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) From 754a083996223462c714090b6595a8cded175cbf Mon Sep 17 00:00:00 2001 From: Vikas Vishwakarma Date: Tue, 30 Oct 2018 22:18:12 +0530 Subject: [PATCH 11/20] Added Cats and mouse in implementation --- .../implementation/Cats and a mouse/solution.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 solution/practice/algorithms/implementation/Cats and a mouse/solution.py 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 From cec7fbfd7a1104964c8061db1c046315a5dcd18e Mon Sep 17 00:00:00 2001 From: Vikas Vishwakarma Date: Tue, 30 Oct 2018 22:27:36 +0530 Subject: [PATCH 12/20] Added Utopian tree in implementation --- .../implementation/Utopian Tree/solution.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 solution/practice/algorithms/implementation/Utopian Tree/solution.py 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 From aa84612c83e8c04b7afcc2eff748fc327892f7e8 Mon Sep 17 00:00:00 2001 From: Vikas Vishwakarma Date: Tue, 30 Oct 2018 22:47:06 +0530 Subject: [PATCH 13/20] Added Append and delete in implementation and updated the readme file --- README.md | 10 +++++----- .../Append-and-Delete/solution.py | 17 +++++++++++++++++ .../solution.py | 0 .../solution.py | 0 .../solution.py | 0 .../{Utopian Tree => Utopian-Tree}/solution.py | 0 6 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 solution/practice/algorithms/implementation/Append-and-Delete/solution.py rename solution/practice/algorithms/implementation/{Cats and a mouse => Cats-and-a-mouse}/solution.py (100%) rename solution/practice/algorithms/implementation/{Designer PDF Viewer => Designer-PDF-Viewer}/solution.py (100%) rename solution/practice/algorithms/implementation/{Sherlock And Squares => Sherlock-And-Squares}/solution.py (100%) rename solution/practice/algorithms/implementation/{Utopian Tree => Utopian-Tree}/solution.py (100%) diff --git a/README.md b/README.md index c6b17d1..23ee39f 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,12 @@ 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) | [Solution & Comment](solution/practice/algorithms/implementation/TheHurdleRace/solution.py) -15 | [Designer PDF Viewer](https://www.hackerrank.com/challenges/designer-pdf-viewer) | WIP -16 | [Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree) | WIP +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 @@ -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 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 similarity index 100% rename from solution/practice/algorithms/implementation/Cats and a mouse/solution.py rename to solution/practice/algorithms/implementation/Cats-and-a-mouse/solution.py diff --git a/solution/practice/algorithms/implementation/Designer PDF Viewer/solution.py b/solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py similarity index 100% rename from solution/practice/algorithms/implementation/Designer PDF Viewer/solution.py rename to solution/practice/algorithms/implementation/Designer-PDF-Viewer/solution.py diff --git a/solution/practice/algorithms/implementation/Sherlock And Squares/solution.py b/solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py similarity index 100% rename from solution/practice/algorithms/implementation/Sherlock And Squares/solution.py rename to solution/practice/algorithms/implementation/Sherlock-And-Squares/solution.py diff --git a/solution/practice/algorithms/implementation/Utopian Tree/solution.py b/solution/practice/algorithms/implementation/Utopian-Tree/solution.py similarity index 100% rename from solution/practice/algorithms/implementation/Utopian Tree/solution.py rename to solution/practice/algorithms/implementation/Utopian-Tree/solution.py From 6e82644da04e2a948008a7896ee07c6f8dc22f92 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Oct 2019 15:46:46 +0000 Subject: [PATCH 14/20] MCQ1 Answer is added --- .../kindergarten-adventures/solution.py | 22 +++++++++++++++++++ .../how-well-do-you-know-trees/solution.py | 1 + 2 files changed, 23 insertions(+) create mode 100644 solution/practice/data-structures/advanced/kindergarten-adventures/solution.py create mode 100644 solution/practice/data-structures/multiple-choice/how-well-do-you-know-trees/solution.py 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/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") From 7121b665c5feba37854ada1f8e8c3a94aeb0594d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Oct 2019 15:54:32 +0000 Subject: [PATCH 15/20] MCQ solution location updated in Readme.md file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c6b17d1..3ebe292 100644 --- a/README.md +++ b/README.md @@ -601,7 +601,7 @@ Data Structures ### Multiple Choice \# | Challenge | Solution :---:|:---:|:---: -1 | [ Data Structures MCQ 1](https://www.hackerrank.com/challenges/how-well-do-you-know-trees) | 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) | WIP 3 | [ Data Structures MCQ 3](https://www.hackerrank.com/challenges/are-you-an-expert-on-data-structures-1) | WIP From a46b9e9a12c7df70ab94488634555b67657c5a70 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Oct 2019 16:09:56 +0000 Subject: [PATCH 16/20] Data Structure MCQ 2 Solution is added --- README.md | 4 ++-- challenges.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3ebe292..6ca53f4 100644 --- a/README.md +++ b/README.md @@ -501,7 +501,7 @@ Data Structures * [Queues](#queues) (0/5) * [Heap](#heap) (0/4) * [Disjoint Set](#disjoint-set) (4/4) -* [Multiple Choice](#multiple-choice) (0/3) +* [Multiple Choice](#multiple-choice) (2/3) * [Trie](#trie) (0/2) * [Advanced](#advanced) (1/48) @@ -602,7 +602,7 @@ Data Structures \# | Challenge | Solution :---:|:---:|:---: 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) | WIP +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) | WIP ### Trie 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 +} From 76212e454d1a322e507cb9d86850bc409f3d8cc2 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Oct 2019 16:15:29 +0000 Subject: [PATCH 17/20] Readmi.md fix --- .../are-you-an-expert-on-data-structures/solution.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures/solution.py 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") From 3d02d9b467cd5784a933204a34c8fb1f403314c8 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Oct 2019 16:42:57 +0000 Subject: [PATCH 18/20] MCQ3 Solution is added --- .../are-you-an-expert-on-data-structures-1/solution.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 solution/practice/data-structures/multiple-choice/are-you-an-expert-on-data-structures-1/solution.py 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") From af5d642a66fc9c14bfba201dc50507216dda7c1b Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Oct 2019 16:41:27 +0000 Subject: [PATCH 19/20] README.md Updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ca53f4..59f89b6 100644 --- a/README.md +++ b/README.md @@ -603,7 +603,7 @@ Data Structures :---:|:---:|:---: 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) | WIP +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 From b6c0c199a5e320b1b59fdedafda2630258314b76 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Oct 2019 17:17:08 +0000 Subject: [PATCH 20/20] Finla README.md Update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 59f89b6..1190725 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Index ====== * [Algorithms](#algorithms) (61/409) -* [Data Structures](#data-structures) (11/114) +* [Data Structures](#data-structures) (17/114) * [Mathematics](#mathematics) (1/250) Algorithms @@ -501,7 +501,7 @@ Data Structures * [Queues](#queues) (0/5) * [Heap](#heap) (0/4) * [Disjoint Set](#disjoint-set) (4/4) -* [Multiple Choice](#multiple-choice) (2/3) +* [Multiple Choice](#multiple-choice) (3/3) * [Trie](#trie) (0/2) * [Advanced](#advanced) (1/48)