Introduction
It is my turn to congratulate my two classmates to become computer science department head this year. I cannot believe how hard people are working and then make such great career success.
From January 2015, she started to practice leetcode questions; she trains herself to stay focus, develops "muscle" memory when she practices those questions one by one. 2015年初, Julia开始参与做Leetcode, 开通自己第一个博客. 刷Leet code的题目, 她看了很多的代码, 每个人那学一点, 也开通Github, 发表自己的代码, 尝试写自己的一些体会. She learns from her favorite sports – tennis, 10,000 serves practice builds up good memory for a great serve. Just keep going. Hard work beats talent when talent fails to work hard.
/// <summary> | |
/// time complexity analysis: | |
/// combination starting from 2, target 7, minimum is 2, at most 4 elements in one combination | |
/// starting from 2, look for at most another 3 elements -> recursive function, depth first search | |
/// at most 3 time, exhaust all options. Each step at most 4 options. So in total will be 4^3 = 64 recursive | |
/// calls. | |
/// next step, minimum value in combination starts from 3, each step at most 3 options, so in total | |
/// will be 3^3 = 27 calls | |
/// so the recursive calls should be 4^3 + 3^3 + 2^3 + 1 ^3 | |
/// the idea is to sort the candidates first in ascending order. | |
/// </summary> | |
public static void RunTestcase() | |
{ | |
var result = CombinationSum(new int[]{2,3,6,7}, 7); | |
} | |
public static IList<IList<int>> CombinationSum(int[] candidates, int target) | |
{ | |
var result = new List<IList<int>>(); | |
var combination = new List<int>(); | |
Array.Sort(candidates); | |
CombinationSum(result, candidates, combination, target, 0); | |
return result; | |
} | |
/// <summary> | |
/// I like to talk about the design: | |
/// this algorithm is like brute force solution, using back tracking. | |
/// combination can be bruted force to find solution. Like minimum value in the combination. | |
/// starting from each one. | |
/// </summary> | |
/// <param name="result"></param> | |
/// <param name="candidates"></param> | |
/// <param name="combination"></param> | |
/// <param name="target"></param> | |
/// <param name="start"></param> | |
private static void CombinationSum( | |
IList<IList<int>> result, | |
int[] candidates, | |
IList<int> combination, | |
int target, | |
int start) | |
{ | |
if (target == 0) | |
{ | |
result.Add(new List<int>(combination)); | |
return; | |
} | |
// enforce the combination numbers in the list will be non-descending order | |
// brute force the start value from all possible values starting from start variable | |
for (int i = start; i != candidates.Length && target >= candidates[i]; ++i) | |
{ | |
combination.Add(candidates[i]); | |
CombinationSum(result, candidates, combination, target - candidates[i], i); | |
combination.Remove(combination.Last()); | |
} | |
} |
665 | 19.9% | Easy | |||
189 | 26.3% | Easy | |||
414 | 28.2% | Easy | |||
532 | 28.4% | Easy | |||
581 | 29.4% | Easy | |||
605 | 30.0% | Easy | |||
88 | 32.8% | Easy | |||
219 | 33.3% | Easy | |||
840 | 34.3% | Easy | |||
624 | 35.8% | Easy | |||
26 | 37.3% | Easy | |||
643 | 37.8% | Easy | |||
849 | 38.1% | Easy | |||
1 | 38.6% | Easy | |||
724 | 39.2% | Easy | |||
119 | 39.3% | Easy | |||
66 | 39.9% | Easy | |||
35 | 40.0% | Easy | |||
747 | 40.4% | Easy | |||
53 | 40.8% | Easy | |||
118 | 41.3% | Easy | |||
27 | 41.5% | Easy | |||
674 | 42.7% | Easy | |||
746 | 43.8% | Easy | |||
121 | 43.8% | Easy | |||
628 | 44.8% | Easy | |||
268 | 45.7% | Easy | |||
830 | 46.7% | Easy | |||
661 | 46.8% | Easy | |||
697 | 47.0% | Easy | |||
167 | 47.4% | Easy | |||
217 | 48.1% | Easy | |||
122 | 48.6% | Easy | |||
169 | 49.0% | Easy | |||
717 | 49.0% | Easy | |||
448 | 51.3% | Easy | |||
283 | 52.1% | Easy | |||
695 | 52.2% | Easy | |||
485 | 53.8% | Easy | |||
243 | 54.2% | Easy | |||
566 | 57.7% | Easy | |||
766 | 58.2% | Easy | |||
561 | 66.8% | Easy | |||
867 | 66.8% | Easy | |||
832 | 69.2% | Easy |