Advertisement
CSenshi

Cut the sticks

Jan 28th, 2021
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. // Complete the cutTheSticks function below.
  2.     static int[] cutTheSticks(int[] arr) {
  3.         List<Integer> result = new ArrayList<>();
  4.         while(true){
  5.             // 1. find min
  6.             int mn = Integer.MAX_VALUE;
  7.             for(int i = 0; i < arr.length; i++){
  8.                 if(arr[i] > 0)
  9.                     mn = Math.min(mn, arr[i]);
  10.             }
  11.             if(mn == Integer.MAX_VALUE)
  12.                 break;
  13.             // 2. cur stick
  14.             int count = 0;
  15.             for(int i = 0; i < arr.length; i++){
  16.                 if(arr[i] > 0)
  17.                     count ++;
  18.                 arr[i] = arr[i] - mn;
  19.             }
  20.            
  21.             // 3. append result
  22.             result.add(count);
  23.         }
  24.         int[] res = new int[result.size()];
  25.         for(int i = 0; i < result.size(); i++){
  26.             res[i] = result.get(i);
  27.         }
  28.         return res;
  29.     }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement