chore: allocate minimum number of pages (#252)

pull/254/head
Aayush Jain 2021-04-23 10:11:02 +05:30 committed by GitHub
parent 8a3405f217
commit 32614a5956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 1 deletions

View File

@ -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)

View File

@ -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;i<N;i++){
pages[i]=sc.nextInt();
}
int M=sc.nextInt();
System.out.println(findPages(pages,N, M));
}
}
public static int findPages(int[]arr,int n,int m)
{
if(n<m) return -1;
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
int start=0, end=sum;
int res = Integer.MAX_VALUE;
while(start<=end){
int mid = start+(end-start)/2;
if(isPossible(arr, n, m, mid)){
res = Math.min(res, mid);
end = mid-1;
}
else start=mid+1;
}
return res;
}
public static boolean isPossible(int arr[], int n, int m, int curr_min){
int student=1;
int curr_sum=0;
for(int i=0;i<n;i++){
if(arr[i]>curr_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.
*/