Question: 1 2 3 Task 2 Java 8 A group of friends is going on holiday together. They have come to a meeting point (the
1 2 3 Task 2 Java 8 A group of friends is going on holiday together. They have come to a meeting point (the start of the journey) using N cars. There are P[K] people and S[K] seats in the K-th car for K in range [0..N-1]. Some of the seats in the cars may be free, so it is possible for some of the friends to change the car they are in. The friends have decided that, in order to be ecological, they will leave some cars parked at the meeting point and travel with as few cars as possible. Write a function: class Solution { public int solution(int[] P, int[] S); } that, given two arrays P and S, consisting of N integers each, returns the minimum number of cars needed to take all of the friends on holiday. Examples: 1. Given P = [1, 4, 1] and S = [1, 5, 1], the function should return 2. A person from car number 0 can travel in car number 1 instead. This way, car number 0 can be left parked at the meeting point. 2. Given P = [4, 4, 2, 4] and S = [5, 5, 2, 5], the function should return 3. One person from car number 2 can travel in car number 0 and the other person from car number 2 can travel in car number 3. 3. Given P = [2, 3, 4, 2] and S = [2, 5, 7, 2], the function should return 2. Passengers from car number 0 can travel in car number 1 and passengers from car number 3 can travel in car number 2. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; each element of arrays P and S is an integer within the range [1..9]; every friend had a seat in the car they came in; that is, P[K] S[K] for each K within the range [0..N-1]. Copyright 2009-2022 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. Autocomplete is connected | All changes saved Files task2 solution.java test-input.txt Test Output || solution.java x // you can also use imports, for example: // import java.util.*; 1 2 3 4 5 6 7 8 9 10 11 12 // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { } public int solution (int[] P, int[] 5) { // write your code in Java SE 8 To leave editor use Ctrl + M Any problems with the editor? Switch to basic editor ? Run Code Give Feedback | 6:14 PM
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
