Graduating half a year, usually work is always focused on business, architecture, but less attention to performance, and has never done any work involved in the algorithm
Hope to have the time to pick up these things, after all, no matter what language to use, in what industry, as long as the programmer, the algorithm is the real foundation.
The topic comes from Leetcode, the code language is usually C + +, later may be the individual topic will use Golang
Everything starts hard, but it's harder to stick to it.
(2018.2.3)
Topic 1: Given and, get Addend
Description: given an array of integers, and an integer, it is known that this integer is the number of two elements in the array and is now needed to find and return the index of these two elements, for example:
Array of integers: {11, 7, 12, 2}
Integers: 9
Results returned: {1, 3}
(assuming that the result must be present in the given array, and that there is no need to consider multiple sets of results)
Title Link: https://leetcode.com/problems/two-sum/description/
Answer:
1, the most traditional method is to find each one, judge the results, the specific steps are:
Each trip starts with the first element (11), fixed the first element unchanged, calculates the sum of the current element and the value of each subsequent element, and determines whether it is equal to the target integer
This way, in the worst case, each pair of elements in the array is evaluated once, so the time complexity is O (n * N)
Code:
Vector<int> twosum (Vector<int>& nums, int target) { vector<int> ret; int sz = nums.size (); for (size_t i = 0; i < sz - 1; ++i) { for (size_t j = i + 1; j < sz; ++ j) { if ( Nums[i] + nums[j] == target) { ret.push_back (i); ret.push_back ( j); return ret; } } } return ret; }
2, the above greedy method can be decomposed into two parts, the outer loop and the inner loop, whenever the outer loop to perform a step, the inner loop will need to traverse the remaining elements, the execution of a trip time complexity of O (N) process, the entire process, the outer loop is not optimized, and the inner layer of the loop as optimization, Consider the idea of exchanging space for time: to index the entire array beforehand, so that the inner loop of each trip is no longer a traversal, but an exact lookup, making the algorithm's time complexity changed from O (n*n) to O (n*1)
The specific steps are:
3, the process of indexing can be decomposed into each step of the outer loop:
No index at first, each step finds the target element in the index, and if not, the current element is indexed and iterated to the next step
Compare 1, 2, 3 methods,
1 Method time complexity is O (n*n), minimum efficiency,
2, 3 Methods The overall time complexity is O (N), the difference is that the 2 method is to set up a complete index at the beginning, and the 3 method is the iterative process of gradually indexing
In the pessimistic case, the 2, 3 method efficiency is the same
In optimistic situations, the 3 method only needs to store an element in the index, so it is relatively more efficient
Algorithm problem thinking and code (not regularly updated)