Commonly used algorithms and tools
using Array: An array is the most commonly used tools
Sort: selection sort, insertion sort, merge sort; quicksort
find: binary search
data structures: stack, queue, stack
How to write correct programs - for example in a binary search
Premise: an ordered array before you can use binary search
1. Identify the meaning of the variables:
2. loop invariant : change the value, but does not change the meaning.
3. Note the meaning of the border represents, to ensure effective when border.
Algorithms on thinking and memory may be a little deeper understanding
tip :
mid = ( left + right ) / 2; // 容易出现 left+right 的整型溢出问题
mid = ( left + right ) / 2; // 尽量使用减法
Small data sets: the clever design of small data sets bug testing, debugging programs.
Large data sets: the performance of the test program.
An array of commonly used techniques
- Ordered : binary search
- Double indexing techniques:
- Collider pointer
- Sliding window
- Limited number of elements: Technology Sort
- Combined with sliding window and look-up table
Find related issues
- LOOK FOR:
set
; // collection - Find a Map:
map
; // dictionaries, maps - Shortcoming hash table: the loss of the order of the data, insert and look for:
O(1)
- Binary search tree (balanced): insert and look for:
O(logn)
- String two points to consider:
- Null string
- character set
Related list
- Set list of virtual head node
- Go-between: setup work pointer, changing only the address pointer to complete the algorithm.
- not just go-between , and sometimes change the value of the node, it is more efficient.
- Double pointer (sliding window): General procedure on a linear table.
Stacks, queues, recursion and binary tree
- Classic recursive algorithms: traversal algorithm on binary tree.
- Design a recursive algorithm: (recursive termination condition, recursive procedure)
- A binary tree is empty, but there are no children around.
- The three-stage design: recursive termination condition, body recursion, recursive process.
- Before writing code, to define clearly what is the meaning of recursive recursive function yes.
- Note: Note recursive termination condition.
Recursion and backtracking
- Tree problem: the problem is not defined in the structure of the binary tree, but the idea is essentially to solve the problem is a binary tree.
- An important feature of the recursive call is: return , that is backtracking.
- Backtracking is violent solution of a major means to achieve, but efficiency is relatively low.
- Improved backtracking algorithm:
- Dynamic Programming
- Pruning
- Backtracking applications: permutation problem, combinatorial problems.
- Backtracking, recursion an important question: How to pass value problem.
- Definition of variables in the class.
- Pruning operations: the original operation will try many may not need to try. These possibilities need to attempt to remove, is the pruning.
- Backtracking on a two-dimensional plane: (EG, Word Search )
- Looking order provisions, up and down, clockwise.
- Find a tree, recursive structure.
- Tip: on a two-dimensional array of offsets . Movement in two dimensions is very easy to use.
- Another classic algorithm on a two-dimensional plane:
floodfill
Algorithms- e.g., Number of Islands
- eg, N-Queen
Dynamic programming foundation
What is dynamic programming
- Fibonacci number: Natural recursive structure
// F(0) = 1; F(1) = 1; F(n) = F( n - 1 ) + F( n - 2 )
int fib( int n ) {
if(n==0) return 0;
if(n==1) return 1;
return fib(n-1)+fib(n-2);
}
- Memory search: top-down approach
// F(0) = 1; F(1) = 1; F(n) = F( n - 1 ) + F( n - 2 )
vector<int> memo(n+1, -1);
memo[0] = 0;
memo[1] = 1;
int fib( int n ) {
if(n==0) return 0;
if(n==1) return 1;
if(memo[n] != -1)
return memo[n];
else
return fib(n-1)+fib(n-2);
}
- Dynamic Programming: bottom-up approach
// F(0) = 1; F(1) = 1; F(n) = F( n - 1 ) + F( n - 2 )
int fib( int n ) {
vector<int> memo(n+1, -1);
memo[0] = 0;
memo[1] = 1;
for(int i =2; i <=n; i++) {
memo[i] = memo[i-1] + memo[i-2];
}
}
Problem-solving ideas of dynamic programming
Dismantling the original problem into several sub-problems, while preserving the child answer questions. So that each sub-problem solving only once and, ultimately, the answer to the original problem. ps. Most problems are recursive dynamic programming problem.
Optimal substructure: by sub-optimal solutions to solve the problem, you can obtain the optimal solution to the original problem.
Ideas: first top-down thinking the problem, find the sub-problem; the bottom-up problem-solving in.
- First with a recursive solution: Recursive body and recursive end condition.
- 1. into the memory search.
- The 2, rewrite dynamic programming.
Dynamic Programming - House Robber
Problem-solving ideas:
- The definition of the status of
consideration steal[x ... n-1]
the scope of the house, namelyf(x)
the definition of - State transition equation
f(0) = max ( v(0) + f(2), v(1)+f(3), v(2)+f(4), ... , v(n-3) + f(n-1), v(n-2), v(n-1) )
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if(n == 0 ) return 0;
int[] memo = new int[n+1];
//初始化
memo[0] = 0;
memo[1] = nums[0];
for(int i = 2; i <= n; i++) {
memo[i] = Math.max(memo[i-2] + nums[i-1], memo[i-1]);
}
return memo[n];
}
}