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; +} 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 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))