What to do/Continue after the loop
leetcode 228: Summary Ranges
LeetCode - Decode Ways
Base cases/conditions
http://massivealgorithms.blogspot.com/2016/01/leetcode-325-maximum-size-subarray-sum.html
Initialization:
http://massivealgorithms.blogspot.com/2015/12/leetcode-322-coin-change.html
-- dp[n + 1][amount + 1], where dp[i][j] means the minimum number of coins in the first i coins for which the sum of amount equal to j.
-- Initialization:
dp[0][0] = 0;
dp[0][j] = Integer.MAX_VALUE, means there is no way to fulfill the amount j with no coins
dp[i][0] = 0, meaning the number of coins is 0 to meet the total amount 0.
For others, the initial state is Integer.MAX_VALUE.
Top 10 Useful, Yet Paranoid Java Programming Techniques
9. Always throw on switch default
10. Switch with curly braces
Big Picture:
List all problems that u need solve.
Don't do simulation.
Always think multiple/different/better solution before start to write code.
Don't start to write code unless asked to.
Don't start to write code unless find best solution.
leetcode 228: Summary Ranges
| public List<String> summaryRanges(int[] nums) { | |
| List<String> res = new ArrayList<>(); | |
| if (nums == null || nums.length == 0) { | |
| return res; | |
| } | |
| StringBuilder sb = new StringBuilder(); | |
| int min = nums[0]; | |
| int max = nums[0]; | |
| for (int i = 1; i < nums.length; i++) { | |
| if (nums[i - 1] + 1 == nums[i]) { | |
| continue; | |
| } | |
| max = nums[i - 1]; | |
| if (max != min) { | |
| sb.append(min).append("->").append(max); | |
| }else { | |
| sb.append(max); | |
| } | |
| res.add(sb.toString()); | |
| min = nums[i]; | |
| sb.setLength(0); | |
| } | |
| if (min != nums[nums.length - 1]) { | |
| sb.append(min).append("->").append(nums[nums.length - 1]); | |
| }else { | |
| sb.append(nums[nums.length - 1]); | |
| } | |
| res.add(sb.toString()); | |
| return res; | |
| } |
Base cases/conditions
http://massivealgorithms.blogspot.com/2016/01/leetcode-325-maximum-size-subarray-sum.html
map.put(0, -1); // IMPOARTANT
when current sum is K
Initialization:
http://massivealgorithms.blogspot.com/2015/12/leetcode-322-coin-change.html
-- dp[n + 1][amount + 1], where dp[i][j] means the minimum number of coins in the first i coins for which the sum of amount equal to j.
-- Initialization:
dp[0][0] = 0;
dp[0][j] = Integer.MAX_VALUE, means there is no way to fulfill the amount j with no coins
dp[i][0] = 0, meaning the number of coins is 0 to meet the total amount 0.
For others, the initial state is Integer.MAX_VALUE.
Top 10 Useful, Yet Paranoid Java Programming Techniques
9. Always throw on switch default
10. Switch with curly braces
List all problems that u need solve.
Don't do simulation.
Always think multiple/different/better solution before start to write code.
Don't start to write code unless asked to.
Don't start to write code unless find best solution.