[Programmers / JAVA] Level 2 Feature Development (42586)
[Programmers / JAVA] Level 2 Feature Development (42586)
| Rank | Language Used |
|---|---|
| Level 2 | 🖼️ JAVA |
The Programmers team is working on feature improvements. Each feature can be shipped to the service once its progress reaches 100%.
Also, since each feature's development speed differs, a feature listed later can be finished earlier than one listed before it, in which case the later feature is deployed together when the earlier feature is deployed.
Given an integer array progresses containing the progress of each task in the order they must be deployed, and an integer array speeds containing the development speed of each task, complete the solution function to return how many features are deployed at each deployment.
- The number of tasks (the length of the progresses and speeds arrays) is 100 or fewer.
- Task progress is a natural number less than 100.
- Task speed is a natural number less than or equal to 100.
- Deployment can only happen once per day, and is assumed to occur at the end of the day. For example, if a task with 95% progress has a development speed of 4% per day, it will be deployed 2 days later.
| progresses | speeds | return |
|---|---|---|
| { 93, 30, 55 } | { 1, 30, 5 } | { 2, 1 } |
| { 95, 90, 99, 99, 80, 99 } | { 1, 1, 1, 1, 1, 1 } | { 1, 3, 2 } |
Input/Output Example #1
The first feature is 93% complete and can progress 1% per day, so it can be deployed after 7 days of work.
The second feature is 30% complete and can progress 30% per day, so it can be deployed after 3 days of work. However, since the first feature isn't finished yet, it gets deployed on day 7, along with the first feature.
The third feature is 55% complete and can progress 5% per day, so it can be deployed after 9 days of work.
Therefore, 2 features are deployed on day 7, and 1 feature is deployed on day 9.
Input/Output Example #2
Since every feature can progress 1% per day, the number of days remaining until completion is 5, 10, 1, 1, 20, and 1 days respectively. Even if a feature finishes earlier, it cannot be deployed unless every feature before it is also complete.
Therefore, 1 feature is deployed on day 5, 3 features on day 10, and 2 features on day 20.
- progresses represents the current progress rate.
- speeds represents the daily development speed.
By combining progresses and speeds appropriately, we can obtain the number of days remaining for development.
JAVA
int[] days = new int[progresses.length]; for (int i = 0; i < progresses.length; i++) { days[i] = (int) Math.ceil((100F - progresses[i]) / speeds[i]); }
Subtract progresses from 100 and divide by speeds to get the number of days.
If a decimal such as 2.1 or 3.5 occurs, round it up.
Traverse the days array, compare the durations, and build the deployment array to return.
Since we don't know how many elements will be in the array, we use an ArrayList.
Declare count to store the number of deployments and target to compare durations, then traverse days.
count starts at 1, and target starts at days[0].
JAVA
ArrayList<Integer> list = new ArrayList<>(); int count = 1; int target = days[0]; for (int i = 1; i < days.length; i++) { // If this task takes much longer if (target < days[i]) { target = days[i]; list.add(count); count = 1; } // If it's already finished else { count++; } } list.add(count);
Starting from days[1], compare days[i] with target.
If days[i] is larger, it means more time is needed for deployment, so store the current deployment count in the ArrayList and reset count. Also reassign target to days[i].
If target is still larger, the feature is already finished, so it can be deployed together with target. Only increment count.
Since the last data isn't reflected after the for loop finishes, save count separately at the end.
JAVA
import java.util.ArrayList; /** * Feature Development class * * @author RWB * @since 2021.12.27 Mon 12:40:08 */ class Solution { /** * Answer return method * * @param progresses: [int[]] Natural numbers * @param speeds: [int[]] Natural numbers * * @return [int[]] Answer */ public int[] solution(int[] progresses, int[] speeds) { int[] days = new int[progresses.length]; for (int i = 0; i < progresses.length; i++) { days[i] = (int) Math.ceil((100F - progresses[i]) / speeds[i]); } ArrayList<Integer> list = new ArrayList<>(); int count = 1; int target = days[0]; for (int i = 1; i < days.length; i++) { // If this task takes much longer if (target < days[i]) { target = days[i]; list.add(count); count = 1; } // If it's already finished else { count++; } } list.add(count); return list.stream().mapToInt(Integer::intValue).toArray(); } }
