Storage Optimization Amazon OA 2023 Solution

Storage Optimization Amazon OA 2023 Solution

You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and verticalCuts where:

  • horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, and
  • verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.

Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a large number, return this modulo 109 + 7.

Example 1:

Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]

Output: 4

Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. 

Storage Optimization Amazon OA 2023 Solution

Example 2:

Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]

Output: 6

Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. 

Storage Optimization Amazon OA 2023 Solution

Example 3:

Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]

Output: 9

Constraints:

  • 2 <= h, w <= 109
  • 1 <= horizontalCuts.length <= min(h – 1, 105)
  • 1 <= verticalCuts.length <= min(w – 1, 105)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • All the elements in horizontalCuts are distinct.
  • All the elements in verticalCuts are distinct.

SOLUTION

Program: Storage Optimization Amazon OA Solution in C++

class Solution {
public:
    int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
        int mod = pow(10, 9) + 7;
        sort(hc.begin(), hc.end());
        sort(vc.begin(), vc.end());
        int maxl = INT_MIN, maxw = INT_MIN;
        long long res;
        for(int i = 0; i < hc.size(); i++) {
            if(i == 0) maxl = max(maxl, hc[i] - 0);
            if(i == hc.size() - 1) maxl = max(maxl,h - hc[i]);
            if(i > 0)  maxl = max(maxl, hc[i] - hc[i - 1]);
        }
        for(int i = 0; i < vc.size(); i++) {
            if(i == 0) maxw = max(maxw, vc[i] - 0);
            if(i == vc.size() - 1) maxw = max(maxw, w - vc[i]);
            if(i > 0)  maxw = max(maxw, vc[i] - vc[i - 1]);
        }
        res = (long(maxl)* long(maxw)) % mod;
        return res;
    }
};

Program: Storage Optimization Amazon OA Solution in Java

class Solution {
    public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
        Arrays.sort(horizontalCuts);
        Arrays.sort(verticalCuts);
        
        long maxHorizontalGap = horizontalCuts[0];
        int index = 0;
        for(index = 1; index < horizontalCuts.length; index++){
            maxHorizontalGap = (long)Math.max(maxHorizontalGap, horizontalCuts[index] - horizontalCuts[index - 1]);
        }
        maxHorizontalGap = (long)Math.max(maxHorizontalGap, h - horizontalCuts[index - 1]);
        
        long maxVerticalGap = verticalCuts[0];
        index = 0;
        for(index = 1; index < verticalCuts.length; index++){
            maxVerticalGap = (long)Math.max(maxVerticalGap, verticalCuts[index] - verticalCuts[index - 1]);
        }
        maxVerticalGap = (long)Math.max(maxVerticalGap, w - verticalCuts[index - 1]);
        
        return (int)((maxHorizontalGap * maxVerticalGap) % 1000000007 );
    }
}

Program: Storage Optimization Amazon OA Solution in Python

class Solution:
    def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
        #SORT BOTH INPUT LISTS
		horizontalCuts.sort()
		verticalCuts.sort()
		
		#INSERT '0' AT BEGINNING AND 'h' AT END OF 'horizontalCuts' LIST
        horizontalCuts.insert(0,0)
        horizontalCuts.append(h)
		
		#CREATE A NEW LIST OF DIFFERENCES BETWEEN  ALL 'horizontalCuts' ELEMENTS
		horDifs = []
        for x in range(1,len(horizontalCuts)):
            horDifs.append(horizontalCuts[x] - horizontalCuts[x-1])
		
        
        #DO THE SAME WITH 'verticalCuts' LIST
        verticalCuts.insert(0,0)
        verticalCuts.append(w)
        verDifs = []
        for x in range(1,len(verticalCuts)):
            verDifs.append(verticalCuts[x] - verticalCuts[x-1])
        
		#TAKE THE MAX VALUES FROM BOTH LISTS OF DIFFERENCES AND MULTIPLY
		#TAKE MODULO AT THE END
        return max(horDifs) * max(verDifs) % (10**9 + 7)

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Valid Discount Coupons Amazon OA 2023 Solution
  13. Count Maximum Teams Amazon OA 2023
  14. Minimum Coin Flips Amazon OA 2023
  15. Max Average Stock Price Amazon OA 2023 Solution
  16. Robot Bounded In Circle Amazon OA 2023
  17. Shopping Options Amazon OA 2023 Solution
  18. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  19. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  20. Slowest Key Amazon OA 2023 Solution
  21. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  22. Split String Into Unique Primes Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution

1 thought on “Storage Optimization Amazon OA 2023 Solution”

  1. I was looking for this question for so long and I found this site which already had solutions and a list of amazon oa 2021 questions, thank you so much it’s so valuable to me.

    Reply

Leave a Comment

seventeen − ten =