RainX_69

Maximize Score From Two Segments (MUST DO) ASKED IN OA

Feb 6th, 2023 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | Source Code | 0 0
  1. https://leetcode.com/problems/maximize-win-from-two-segments/
  2. DO TRY-> https://leetcode.com/problems/two-best-non-overlapping-events/
  3.  
  4. There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer k.
  5.  
  6. You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.
  7.  
  8. For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.
  9.  
  10. Return the maximum number of prizes you can win if you choose the two segments optimally.
  11.  
  12.  
  13.  
  14. Example 1:
  15. Input: prizePositions = [1,1,2,2,3,3,5], k = 2
  16. Output: 7
  17.  
  18. Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
  19.  
  20. Example 2:
  21. Input: prizePositions = [1,2,3,4], k = 0
  22. Output: 2
  23.  
  24. Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.
  25.  
  26.  
  27. Constraints:
  28.  
  29. 1 <= prizePositions.length <= 10^5
  30. 1 <= prizePositions[i] <= 10^9
  31. 0 <= k <= 10^9
  32. prizePositions is sorted in non-decreasing order.
  33. ---------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. class Solution {
  36. public:
  37.     long long dp[100500][3];
  38.    
  39.     int helper(vector<int> &arr, int curr, int k, int segments){
  40.         if(segments==0 || curr==arr.size()){
  41.             /* consider example [1,1,2,2,3,3,5] ,k=9, We can clearly see, the first segment has the potential to pick the whole array. We can just return the whole answer, because second segment can be taken as an overlap of seg1, so just return 0 if segments after recursion becomes 0 or if curr hits array size, both are valid for answer */
  42.             return 0;
  43.         }
  44.        
  45.         if(dp[curr][segments]!=-1){
  46.             return dp[curr][segments];
  47.         }
  48.        
  49.         int IGNORE=helper(arr,curr+1,k,segments);
  50.        
  51.         // if you consider arr[curr] as start, the end is arr[curr]+k, since the array is sorted find the next index using binary search
  52.         int nextInd=upper_bound(arr.begin(),arr.end(),arr[curr]+k)-arr.begin();
  53.         int TAKE=(nextInd-curr)+helper(arr,nextInd,k,segments-1);
  54.        
  55.         return dp[curr][segments]=max(IGNORE,TAKE);
  56.     }
  57.    
  58.     int maximizeWin(vector<int>& prizePositions, int k) {
  59.         memset(dp,-1,sizeof(dp));
  60.         return helper(prizePositions,0,k,2);
  61.     }
  62. };
  63.  
Advertisement
Add Comment
Please, Sign In to add comment