From 7d7a304ecb277773669592a7f9577ed41cd58e69 Mon Sep 17 00:00:00 2001 From: hemishv111 <44539596+hemishv111@users.noreply.github.com> Date: Thu, 24 Oct 2019 09:33:37 +0530 Subject: [PATCH 1/3] maxheap.py --- Data Structures/Heap/Python/maxheap.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Data Structures/Heap/Python/maxheap.py diff --git a/Data Structures/Heap/Python/maxheap.py b/Data Structures/Heap/Python/maxheap.py new file mode 100644 index 00000000..1df863af --- /dev/null +++ b/Data Structures/Heap/Python/maxheap.py @@ -0,0 +1,24 @@ +import heapq + +class Solution: + + def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]: + + maxHeap = [] + + for (x, y) in points: + distance = math.sqrt(x*x + y*y) + + if len(maxHeap) >= K: + if -1 * distance > maxHeap[0][0]: + heapq.heappushpop(maxHeap, [-1 * distance, [x, y]]) + else: + heapq.heappush(maxHeap, [-1 * distance, [x, y]]) + + resList = [] + + for _ in range(K): + resList.append(heapq.heappop(maxHeap)[1]) + + # return the list + return resList From 5fa07406f2b9df6f1ba02a8eded89e90167dbae9 Mon Sep 17 00:00:00 2001 From: hemishv111 <44539596+hemishv111@users.noreply.github.com> Date: Thu, 24 Oct 2019 09:38:37 +0530 Subject: [PATCH 2/3] Create py_palindrome_oneliner --- Data Structures/Palindrome/py_palindrome_oneliner | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Data Structures/Palindrome/py_palindrome_oneliner diff --git a/Data Structures/Palindrome/py_palindrome_oneliner b/Data Structures/Palindrome/py_palindrome_oneliner new file mode 100644 index 00000000..c815be06 --- /dev/null +++ b/Data Structures/Palindrome/py_palindrome_oneliner @@ -0,0 +1,3 @@ +s=input() +is_palindrome = lambda phrase: phrase == phrase[::-1] +print(is_palindrome(s)) From f74e66d4d4e28a9be081799d9c93afc02b35c895 Mon Sep 17 00:00:00 2001 From: hemishv111 <44539596+hemishv111@users.noreply.github.com> Date: Thu, 24 Oct 2019 09:57:20 +0530 Subject: [PATCH 3/3] Create rod_cutting_problem.c --- .../Dynamic Programming/rod_cutting_problem.c | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Algorithms/Dynamic Programming/rod_cutting_problem.c diff --git a/Algorithms/Dynamic Programming/rod_cutting_problem.c b/Algorithms/Dynamic Programming/rod_cutting_problem.c new file mode 100644 index 00000000..3edeccec --- /dev/null +++ b/Algorithms/Dynamic Programming/rod_cutting_problem.c @@ -0,0 +1,24 @@ +#include +#include + +int max(int a, int b) { return (a > b) ? a : b; } + +int cutRod(int price[], int n) +{ + if (n <= 0) + return 0; + int max_val = INT_MIN; + for (int i = 0; i < n; i++) + max_val = max(max_val, price[i] + cutRod(price, n - i - 1)); + + return max_val; +} + +int main() +{ + int arr[] = { 1, 5, 8, 9, 10, 17, 17, 20 }; + int size = sizeof(arr) / sizeof(arr[0]); + printf("Maximum Obtainable Value is %d", cutRod(arr, size)); + getchar(); + return 0; +}