Finding the subarray with the Maximum sum in the given ArrayList
Here is a more concise solution
private List<Integer> solution(List<Integer> nums) {
int biggestSumSoFar = Integer.MIN_VALUE;
List<Integer> biggestSubListSoFar = new ArrayList<>();
for (int left = 0; left < nums.size(); ++left) {
for (int right = left + 1; right < nums.size(); ++right) {
List<Integer> currentSubList = subListSum(nums, left, right);
int currentSum = sum(currentSubList);
if (currentSum > biggestSumSoFar) {
biggestSumSoFar = currentSum;
biggestSubListSoFar = currentSubList;
}
}
}
return biggestSubListSoFar;
}
private List<Integer> subListSum(final List<Integer> nums, final int left, final int right)
{
final List<Integer> sublist = new ArrayList<>();
for (int i = left; i < right; i++)
{
sublist.add(nums.get(i));
}
return sublist;
}
private int sum(List<Integer> arr) {
int sum = 0;
for(int a : arr){
sum += a;
}
return sum;
}
Find maximum sum of sublist in list of positive integers under O(n^2) of specified length Python 3.5
1) Find sum current
of first w
elements, assign it to best
.
2) Starting from i = w
: current = current + lst[i]-lst[i-w]
, best = max(best, current)
.
3) Done.
Maximum sum of sublist with a specific length
Let's clarify to make sure we're on the same page.
Inputs: 1) a list li
of digits; 2) n
Output: the slice from li
of length n
that has maximal sum.
li = [4,2,1,7,1,3,8,4,7,8,1]
n = 2
slices = (li[x:x+n] for x in range(len(li)-n+1))
max(map(sum,slices))
Out[113]: 15
To find max continuous subarray sum of size M
You can edit the list and remove the largest number successively:
list = [4,6,10,8,2,1]
M = 3
result = 0
# to get unique elements
new_list = set(list)
for i in range(M):
result += max(new_list)
# removing the largest element from new_list
new_list.remove(max(new_list))
print(result)
Index of the highest sum() of sublists
Since you explicitly asked for a one-liner, here it is:
>>> list(map(sum, my_list)).index(max(map(sum, my_list)))
4
This line creates a list of sums using the map
function and finds a value in said list using index
. The value that is searched for is the max value of the list of sums mentioned earlier.
Note that this computes the sums of my_list
's elements twice, which isn't necessary. Solution without the one-line requirement:
>>> sum_list = list(map(sum, my_list))
>>> sum_list.index(max(sum_list))
4
Related Topics
How to Profile Python Code Line-By-Line
How to Change Default Anaconda Python Environment
Want to Find Contours -> Valueerror: Not Enough Values to Unpack (Expected 3, Got 2), This Appears
Accessing Mp3 Metadata with Python
How to Dynamically Compose an or Query Filter in Django
How to Activate a Virtualenv Inside Pycharm's Terminal
How to Read and Write Ini File with Python3
How to Format a String Using a Dictionary in Python-3.X
What's the Fastest Way in Python to Calculate Cosine Similarity Given Sparse Matrix Data
How to Override the [] Operator in Python
Run Command and Get Its Stdout, Stderr Separately in Near Real Time Like in a Terminal
Keyerror: 'Tcl_Library' When I Use Cx_Freeze
Numpy Random Choice to Produce a 2D-Array with All Unique Values
Unsupported Operand Type(S) for +: 'Int' and 'Str'