Lets Code Everyday - Day5

Lets Code Everyday - Day5

Question - 5 :-

Today, I decided to go with the easy question to solve, as I always do to boost my confidence. Then, at the suggestion of a friend, I took on a difficult problem to solve just to give it a shot. I'm sure I can't do it, but I tried and came up with a solution. I admit it was a long process, but it solved the problem that I initially thought was impossible to solve.

Learned this today:

Don't underestimate yourself and pass up the opportunity to give it a shot.

Reducing Dishes

A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.

Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i] * satisfaction[i].

Return the maximum sum of like-time coefficient that the chef can obtain after dishes preparation.

Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.

Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14).
Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People do not like the dishes. No dish is prepared.

First approach:

class Solution {
    public int maxSatisfaction(int[] satisfaction) {
         List<Integer> actualList= new ArrayList<>();
         List<Integer> createdList=new ArrayList<>();
         int negCount=0;
         for(int val:satisfaction){
             actualList.add(val);
              if(val<0)
                negCount++;
         }
         if(negCount!=satisfaction.length){
         Collections.sort(actualList);
         for(int i=0;i<actualList.size();i++){
            int sum = 0;
            for (int j = 0; j < actualList.size(); j++) {
                sum += actualList.get(j).intValue() * (j+1);
            }
            createdList.add(sum);
            actualList.remove(0);
         }
         Collections.sort(createdList);
         return createdList.get(createdList.size()-1);
         }
         return 0;
    }
}

Step-by-step explanation :

  1. Create two ArrayList objects called "actualList" and "createdList" to store the satisfaction values and the created satisfaction values respectively.

  2. Initialize an integer variable "negCount" to 0 to keep track of the number of negative satisfaction values in the input array.

  3. Loop through each satisfaction value in the input array and add it to the "actualList" ArrayList. If the value is negative, increment the "negCount" variable.

  4. If the number of negative values is not equal to the length of the input array, proceed with the rest of the code. Otherwise, return 0 since there is no way to create positive satisfaction values.

  5. Sort the "actualList" ArrayList in ascending order using the Collections.sort() method.

  6. Loop through each value in the sorted "actualList" ArrayList and create a new ArrayList called "tempList" which is a copy of the "actualList" ArrayList.

  7. Loop through each value in the "tempList" ArrayList and calculate the created satisfaction value by multiplying each satisfaction value by its position in the ArrayList and summing the results. Add the resulting sum to the "createdList" ArrayList.

  8. Remove the first element from the "actualList" ArrayList to create a new ArrayList for the next iteration of the loop.

  9. Sort the "createdList" ArrayList in ascending order using the Collections.sort() method.

  10. Return the last element in the "createdList" ArrayList, which represents the maximum created satisfaction value that can be achieved.

The time complexity of the given code is O(n^2 log n), where n is the length of the input array satisfaction. This is because the outer loop iterates n times, and each inner loop iterates n times as well. Additionally, sorting the list takes O(n log n) time.

The space complexity of the code is O(n), as it creates two new lists with n elements each. The actualList stores the input array values, and the createdList stores the sums computed from each iteration of the outer loop.

Second approach:

class Solution {
  public int maxSatisfaction(int[] satisfaction) {
        Arrays.sort(satisfaction);
        int n = satisfaction.length;
        int max = 0;
        for(int i = 0; i < n; i++) {
            int index = 1;
            int sum = 0;
            for(int j = i; j < n; j++) {
                sum += satisfaction[j] * (index++);
            }
            max = Math.max(max, sum);
        }
        return max;
    }
}

Step-by-step explanation:

  1. The method maxSatisfaction takes an integer array satisfaction as input and returns an integer as output.

  2. The first line of the method sorts the satisfaction array in ascending order using Arrays.sort(). This is done so that we can calculate the maximum satisfaction by starting with the smallest element in the array and gradually adding the larger elements.

  3. The variable n is assigned the length of the satisfaction array.

  4. The variable max is initialized to 0. This variable will be used to keep track of the maximum satisfaction that can be achieved.

  5. The outer loop starts from index 0 and goes up to n-1. This loop iterates over all possible starting points in the satisfaction array.

  6. The variable index is initialized to 1. This variable will be used to keep track of the cooking time for each dish. We start with 1 as the cooking time for the first dish and increase it by 1 for each subsequent dish.

  7. The variable sum is initialized to 0. This variable will be used to calculate the total satisfaction that can be achieved starting from the current index.

  8. The inner loop starts from the current index i and goes up to n-1. This loop iterates over all remaining elements in the satisfaction array.

  9. For each element in the satisfaction array, we calculate the satisfaction that can be achieved by multiplying the cooking time index with the satisfaction value of the current element. This gives us the total satisfaction for the current dish.

  10. We add the total satisfaction for the current dish to the variable sum and increment index by 1 for the next dish.

  11. After the inner loop is finished, we compare the current value of max with sum and update max to be the maximum of the two.

  12. The outer loop continues to iterate over all possible starting points in the satisfaction array, calculating the maximum satisfaction that can be achieved from each starting point.

  13. After the outer loop is finished, we return the final value of max as the maximum satisfaction that can be achieved from the satisfaction array.

Therefore, the overall time complexity of the method is O(n^2 log n), where n is the length of the input array and the space complexity of the method is O(1).

In terms of time complexity, both the first and second approaches are giving the same where as the space complexity for the second approach is less comparatively.

Did you find this article valuable?

Support Learn-->Share-->Learn by becoming a sponsor. Any amount is appreciated!