fork download
  1. class Solution {
  2. public static boolean check(int k, int[]piles, int h){
  3. int n=piles.length;
  4. int sum=0;
  5.  
  6. for(int i=0;i<n;i++){
  7. if(piles[i]%k ==0){
  8. sum+=piles[i]/k;
  9. }
  10. else if(piles[i]%k !=0){
  11. sum+=(piles[i]/k +1);
  12. }
  13. }
  14. if(sum <=h){
  15. return true;
  16. }
  17. else{
  18. return false;
  19. }
  20. }
  21. public int minEatingSpeed(int[] piles, int h) {
  22.  
  23. //binary s0earch solution :
  24. int max=piles[0];
  25.  
  26. for(int i=0;i<piles.length;i++){
  27. if(piles[i] > max){
  28. max=piles[i];
  29. }
  30. }
  31.  
  32. int low=1;
  33. int high=max;
  34.  
  35. int ans =max; //in worst case
  36. while(low<=high){
  37. int mid =(low+high)/2;
  38. if(check(mid,piles,h) == false){
  39. low=mid+1;
  40.  
  41. }
  42. else if(check(mid,piles,h)==true){
  43. if(mid == 0){
  44. ans=0;
  45. break;
  46. }
  47. if(mid ==1){
  48. ans =1;
  49. break;
  50. }
  51. if(check(mid-1,piles,h)==false){
  52. ans=mid;
  53. break;
  54. }
  55. else{
  56. high=mid-1;
  57. }
  58.  
  59. }
  60. }
  61. return ans;
  62.  
  63. }
  64. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty