摘要:原文作者:Yx.Ac 文章来源:勇幸|Thinking(http://www.ahathinking.com) DP方案考虑DP求解。这个问题可以继续像LCS、LIS一样,“从后向前”分析(《编程之美》对此又是从前向后分析的,个人不太习惯)。我们考虑最后一个元素arr[n-1]与最大子数组的关系,有如下三种情况:arr[n-1]单独构成最大子数组最大子数组以arr[n-1]结尾最大子数组跟arr[n-1]没关系,最大子数组在arr[0-n-2]范围内,转为考虑元素arr[n-2]从上面我们可以看出,问题分解成了三个子问题,最大子数组就是这三个子问题的最大值,现假设:以arr[n-1]为结尾的最
阅读全文
摘要:3-Sum : Given N distinct integers, how many triples sum to exactly zero?---------------------------------------------------------------------------------------3-Sum : brute-force algorithm O(N3)---------------------------------------------------------------------------------------3-Sum : sorting-bas
阅读全文
摘要:Connected components : Maximal set of objects that are mutually connected.Find query : Check if two objects are in the same component.Union command : Replace components containing two objects with their union.-------------------------------------------------------------------------------------------
阅读全文
摘要:1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 typedef map > graphType; 9 10 void BFS(graphType graph, int starter, map & visitTime) {11 queue Q;12 Q.push(starter);13 map visited;14 for (auto i : graph) {15 visited[i.first] = false;16 }17 ...
阅读全文
摘要:the idea of quick sort is:choose a pivot(randomly or simply)partition the array into a[0,1,...,pivot-1],a[pivot],a[pivot+1,...,end] that all elements ina[0,1,...,pivot-1] = a[pivot]quick sorta[0,1,...,pivot-1]quick sorta[pivot+1,...,end] 1 void swap(int a[], int i, int j) { 2 int temp = a[i]; 3 ...
阅读全文
摘要:merge sort is a typical divide & conquer algorithm,merge is this algorithm's core subroutine,merge:merge two sorted subarrays into one sorted arraymergeSort:divide input array to two subarrays leftArray and rightArraymergeSort(leftArray);mergeSort(rightArray);merge these two subarrays 1 // m
阅读全文