forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray Adjustment.py
More file actions
55 lines (49 loc) · 2.06 KB
/
Array Adjustment.py
File metadata and controls
55 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#Array Adjustment
'''
Given an integer array nums containing positive elements and an int maxValue.
Find the value of x such that the sum of the elements of the array is maximum
and is less than the given maxValue.
x is defined as: if the current value is greater than x, then x is used as the new value,
otherwise keep the original value nums[i] = min(x, nums[i]).
Example 1:
Input: nums = [10, 5, 20, 30], maxValue = 40
Output: 12
Explanation:
If x = 10, the array would be nums = [10, 5, 10, 10] and the sum of the array elements would be 35.
If x = 12, the array would be nums = [10, 5, 12, 12] and the sum of the elements would be 39 which is the maximum sum close to given maxValue which is 40.
So the answer would be 12.
'''
class Solution(object):
def __init__(self):
self.largestX = None
def getNewAdjustedArray(self, nums, x):
newArray = []
for num in nums:
newArray.append(min(x, num))
return newArray
def binarySearch(self, nums, maxValue, low, high):
if(low > high):
return
mid = (low+high)/2
newArray = self.getNewAdjustedArray(nums, mid)
if(sum(newArray) >= maxValue):
self.binarySearch(nums, maxValue, low, mid-1)
else:
# Record the mid value as highest x value
self.largestX = mid
self.binarySearch(nums, maxValue, mid+1, high)
def getX(self, nums, maxValue):
# From the problem we see that there is a range here,
# at any given point we cannot change value of x to be more than the largest element
# because of this rule nums[i] = min(x, nums[i]).
for x in range(1, max(nums)):
newArray = self.getNewAdjustedArray(nums, x)
print newArray, sum(newArray)
# from the above output we see that we can use binary search here.
self.binarySearch(nums, maxValue, 1, max(nums))
print self.largestX
# Main
nums = [10, 5, 20, 30]
maxValue = 40
obj = Solution()
obj.getX(nums, maxValue)