Skip to main content

Command Palette

Search for a command to run...

Maximum Number of Weeks for Which You Can Work

Published
2 min read
Maximum Number of Weeks for Which You Can Work
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

There are n projects numbered from 0 to n - 1. You are given an integer array milestones where each milestones[i] denotes the number of milestones the i<sup>th</sup> project has.

You can work on the projects following these two rules:

  • Every week, you will finish exactly one milestone of one project. You must work every week.

  • You cannot work on two milestones from the same project for two consecutive weeks.

Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will stop working. Note that you may not be able to finish every project's milestones due to these constraints.

Return the maximum number of weeks you would be able to work on the projects without violating the rules mentioned above.

LeetCode Problem - 1953

class Solution {
    // Method to calculate the maximum number of weeks one can work on projects given the milestones array
    public long numberOfWeeks(int[] milestones) {
        // Variable to keep track of the maximum element in the milestones array
        long maxElement = 0;
        // Variable to store the sum of all elements in the milestones array
        long sumOfElement = 0;

        // Loop through the milestones to calculate the sum and find the maximum element
        for (int e : milestones) {
            sumOfElement += e;  // Add the current milestone to the total sum
            maxElement = Math.max(maxElement, e);  // Update the maxElement if current element is larger
        }

        // Calculate the sum of elements excluding the maximum one
        long sumExceptMaxElement = sumOfElement - maxElement;

        // If the maxElement is greater than the sum of the other elements plus 1,
        // it means we can't alternate projects equally, so return the possible number of weeks
        if (maxElement > sumExceptMaxElement + 1) {
            return sumExceptMaxElement * 2 + 1;  // Maximum weeks we can work is twice the sum of other elements plus 1
        }

        // If we can alternate evenly, return the total sum of elements as the number of weeks
        return sumOfElement;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.