Pergunta de entrevista da empresa Microsoft

Given an int array, find the sequence of elements that has the largest sum

Respostas da entrevista

Sigiloso

25 de ago. de 2010

seems like simple sorting and then taking the last elements !! if not, or MaxA; MaxB; march thru elements and capture MaxA and MaxB. the sum of MaxA and MaxB will be the largest sum.

Sigiloso

18 de set. de 2010

Wow what a really sick question.. 1. Find the subarray of maximum sum starting from index 0. 2. Find the subarray of minimum sum starting from index 0 ending at the last index of maximum sum. 3. Max sum-Min sum= MAX

Sigiloso

17 de out. de 2010

bool allNegatives = true; int largest = int.MinValue; for (int i = 0; i array[i] ? largest : array[i]; curMax = curMax + array[i]; if (curMax > maxSoFar) { maxSoFar = curMax; maxEnd = i; maxStart = curMaxStart; allNegatives = false; } else if (curMax <= 0) { curMax = 0; curMaxStart = i + 1; } } if (allNegatives) { return largest; } return maxSoFar; } Taken from http://www.technicalinterviewquestions.net/2009/01/largest-sum-sub-sequence-integer-array.html

Sigiloso

1 de out. de 2011

Kadane's algorithm is the best one I think. Linear.