Open amazon7737 opened 3 months ago
import java.util.*;
class Solution {
public int solution(int[] people, int limit) {
int boat = 1;
int[] boatPeople = new int[people.length];
Arrays.sort(people);
//System.out.println(Arrays.toString(people));
//int totalBoatPeople = 0;
for(int i=0; i< people.length;i++){
int totalBoatPeople = 0;
for(int j=0; j< boatPeople.length;j++){
totalBoatPeople += boatPeople[j];
}
//System.out.println("totalBoatPeople:"+totalBoatPeople);
if(totalBoatPeople+people[i] <= limit){
boatPeople[i] = people[i];
//System.out.println("boatPeople:"+ Arrays.toString(boatPeople));
}else{
boat += 1;
boatPeople = new int[people.length];
boatPeople[i] = people[i];
//System.out.println("boatPeople:"+ Arrays.toString(boatPeople));
}
}
return boat;
}
}