6 kyu

The maximum sum value of ranges -- Simple version

1,452 of 2,747myjinxin2015

Description:

When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said

Description:

Given an array arr that contains some integers(positive, negative or 0), and a range list such as [[start1,end1],[start2,end2],...], start and end are the index of arr and start always less than end. Your task is to calculate the sum value of each range (start index and end index are both inclusive), and return the maximum sum value.

For example:

 Given arr = [1,-2,3,4,-5,-4,3,2,1], 
       range = [[1,3],[0,4],[6,8]]
 should return 6
 
 calculation process:
 range[1,3] = arr[1]+arr[2]+arr[3] = 5
 range[0,4] = arr[0]+arr[1]+arr[2]+arr[3]+arr[4] = 1
 range[6,8] = arr[6]+arr[7]+arr[8] = 6
 So the maximum sum value is 6
 arr = [1,-2,3,4,-5,-4,3,2,1] 
 ranges = [[1,3],[0,4],[6,8]]
 max_sum(arr,ranges) should return 6
 
 Process:
   range[1,3] = arr[1] + arr[2] + arr[3] = 5
   range[0,4] = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] = 1
   range[6,8] = arr[6] + arr[7] + arr[8] = 6
   
   Result: Maximum range sum is 6
Given arr = [1,-2,3,4,-5,-4,3,2,1], 
      range = [[1,3],[0,4],[6,8]]
should return 6
 
calculation process:
range[1,3] = arr[1]+arr[2]+arr[3] = 5
range[0,4] = arr[0]+arr[1]+arr[2]+arr[3]+arr[4] = 1
range[6,8] = arr[6]+arr[7]+arr[8] = 6
So the maximum sum value is 6
 Given arr = [1,-2,3,4,-5,-4,3,2,1], 
       range = [(1,3),(0,4),(6,8)]
 should return 6
 
 calculation process:
 range(1,3) = arr[1]+arr[2]+arr[3] = 5
 range(0,4) = arr[0]+arr[1]+arr[2]+arr[3]+arr[4] = 1
 range(6,8) = arr[6]+arr[7]+arr[8] = 6
 So the maximum sum value is 6

Note:

  • arr/$a always has at least 5 elements;
  • range/$range/ranges always has at least 1 element;
  • All inputs are valid;
  • This is a simple version, if you want some challenge, please try the challenge version.

Some Examples

 maxSum([1,-2,3,4,-5,-4,3,2,1],[[1,3],[0,4],[6,8]]) === 6
 maxSum([1,-2,3,4,-5,-4,3,2,1],[[1,3]]) === 5
 maxSum([1,-2,3,4,-5,-4,3,2,1],[[1,4],[2,5]]) === 0
max_sum([1, -2, 3, 4, -5, -4, 3, 2, 1], [[1, 3], [0, 4], [6, 8]]); // => 6
max_sum([1, -2, 3, 4, -5, -4, 3, 2, 1], [[1, 3]]); // => 5
max_sum([1, -2, 3, 4, -5, -4, 3, 2, 1], [[1, 4], [2, 5]]); // => 0
 maxSum [1,-2,3,4,-5,-4,3,2,1] [(1,3),(0,4),(6,8)] == 6
 maxSum [1,-2,3,4,-5,-4,3,2,1] [(1,3)] == 5
 maxSum [1,-2,3,4,-5,-4,3,2,1] [(1,4),(2,5)] == 0
 max_sum([1,-2,3,4,-5,-4,3,2,1], [[1,3]]) == 5
 max_sum([1,-2,3,4,-5,-4,3,2,1], [[1,4],[2,5]]) == 0
 max_sum([1,-2,3,4,-5,-4,3,2,1], [ [1,3], [0,4], [6,8] ]) == 6
 max_sum([11,-22,31,34,-45,-46,35,32,21], [[1,4],[0,3],[6,8],[0,8]]) == 88 
Fundamentals

Stats:

CreatedNov 29, 2016
PublishedNov 29, 2016
Warriors Trained3836
Total Skips180
Total Code Submissions7513
Total Times Completed2747
JavaScript Completions1452
Haskell Completions97
PHP Completions139
Ruby Completions107
Python Completions972
C Completions68
Total Stars48
% of votes with a positive feedback rating94% of 488
Total "Very Satisfied" Votes433
Total "Somewhat Satisfied" Votes52
Total "Not Satisfied" Votes3
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • JohanWiltink Avatar
  • Firefly2002 Avatar
  • Voile Avatar
  • rowcased Avatar
  • KenKamau Avatar
  • hobovsky Avatar
Ad