From 32614a5956a570c7f2de2e8ccc6de87a56079b46 Mon Sep 17 00:00:00 2001 From: Aayush Jain Date: Fri, 23 Apr 2021 10:11:02 +0530 Subject: [PATCH] chore: allocate minimum number of pages (#252) --- algorithms/Java/README.md | 13 +++- .../Java/searching/allocate-min-pages.java | 77 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 algorithms/Java/searching/allocate-min-pages.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index 53349c99..879c8d46 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -1,15 +1,18 @@ # Python ## Arrays + 1. [Counting Inversions](arrays/counting-inversions.java) 2. [Kadanes Algorithm](arrays/kadanes-algorithm.java) 3. [Left Rotation](arrays/left-rotation.java) 4. [Unique Digits of Large Number](arrays/unique-digits-of-large-number.java) ## Graphs + 1. [Dijkstras](graphs/dijkstras.java) ## Linked Lists + 1. [Circular](linked-lists/circular.java) 2. [Clone Linked List](linked-lists/clone-linkedlist.java) 3. [Doubly](linked-lists/doubly.java) @@ -17,19 +20,24 @@ 5. [Singly](linked-lists/singly.java) ## Queues + 1. [Circular Queue using Linked List](queues/circular-queue-linked-list.java) 2. [Queue using Linked List](queues/queue-linked-list.java) ## Scheduling + 1. [Multi-Level Queue Scheduling](scheduling/multi-level-queue-scheduling.java) 2. [Rund Robin](scheduling/round-robin.java) ## Searching + 1. [Binary Search](searching/binary-search.java) 2. [Jump Search](searching/jump-search.java) 3. [Linear Search](searching/linear-search.java) +4. [Allocate minimum number of pages](searching/allocate-min-pages.java) ## Sorting + 1. [Bubble Sort](sorting/bubble-sort.java) 2. [Counting Sort](sorting/counting-sort.java) 3. [Heap Sort](sorting/heap-sort.java) @@ -39,11 +47,13 @@ 7. [Selection Sort](sorting/selection-sort.java) ## Stacks + 1. [Balanced Parenthesis](stacks/balanced-paranthesis.java) 2. [Stack](stacks/stack.java) 3. [The Stock Span Problem](stacks/the-stock-span-problem.java) ## Strings + 1. [KMP](strings/kmp.java) 2. [Palindrome](strings/palindrome.java) 3. [Rabin Krap](strings/rabin-karp.java) @@ -52,4 +62,5 @@ 6. [Tokenizer](strings/tokenizer.java) ## Trees -1. [Pre in Post Traversal](trees/pre-in-post-traversal.java) \ No newline at end of file + +1. [Pre in Post Traversal](trees/pre-in-post-traversal.java) diff --git a/algorithms/Java/searching/allocate-min-pages.java b/algorithms/Java/searching/allocate-min-pages.java new file mode 100644 index 00000000..3108f3ac --- /dev/null +++ b/algorithms/Java/searching/allocate-min-pages.java @@ -0,0 +1,77 @@ +/* QUESTION: +You are given N number of books. Every ith book has pages[i] number of pages. +You have to allocate books to M number of students. There can be many ways or permutations to do so. +In each permutation, one of the M students will be allocated the maximum number of pages. +Out of all these permutations, the task is to find that particular permutation in which the maximum number of pages allocated to a student is minimum of those in all the other permutations and print this minimum value. +Each book will be allocated to exactly one student. Each student has to be allocated at least one book. */ + +import java.io.*; +import java.util.*; +class GFG { + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); //number of test cases + while(t-->0){ + int N=sc.nextInt(); + int pages[]=new int[N]; + + for(int i=0;icurr_min) return false; + if(curr_sum+arr[i]>curr_min){ + student++; + curr_sum = arr[i]; + if(student>m) return false; + } + else curr_sum+=arr[i]; + } + return true; + } +} + +/* +Input: +N = 4 +A[] = {12,34,67,90} +M = 2 + +Output: +113 + +Explanation: +Allocation can be done in following ways: +{12} and {34, 67, 90} Maximum Pages = 191 +{12, 34} and {67, 90} Maximum Pages = 157 +{12, 34, 67} and {90} Maximum Pages =113 +Therefore, the minimum of these cases is +113, which is selected as the output. + */