Leetcode-Transform your problem to some other problems to get the solution.
There are some problems which are not easy to get the solution quickly. But we can transform these problems to other problems to get the solution. This article will introduce some problems which are easy to get the solution. Try to transform your problem to these problems.
Find the target sum/Find two equal sums
For this kind of problem, the number of elements for the answer is not determined, so it’s not 2Sum problem. Two equal sums means the half of the sum of all elements. If you need to find the target sum from the input array, you need to use 0/1 knapsack to solve this problem.
Leetcode 416,494.
Find the first or the last value which is smaller or larger or equal to the target/ Find the closest k(k=1,2,3…) elements/
Binary Search. See this article Leetcode-Binary Search.
Kth largest element/ Top k elements.
HeapSort. See this article Leetcode-HeapSort & Quick Select.
Find the path in a binary tree
If we are asked to find a path in a binary tree, we can use recursion to solve this problem. For example, we need to find the path which sum is target value. We can call our solution this way: sumUp(root,sum,0)+tranverse(root.left,sum)+tranverse(root.right,sum). In our sumUp functions, we will calculate all sums which start from the root and end in the leaf nodes. If we find target sum, plus 1. tranverse(root.left,sum)+tranverse(root.right,sum) will help us cover all paths where the start is not root.
Leetcode 437.