Interview(1)Algorithm Book
Chapter 1 Algorithm
Basic Sorting
A[0.. n-1], 0<i<n, system should have A[i-1] <= A[i]
Solution:
5, 2, 7, 4, 6, 3, 1
Bubblesort(S[], n)
exchange the position of 2 items which are next to each other
2, 5, 4, 6, 3, 1, 7
2, 4, 5, 3, 1, 6, 7
2, 4, 5, 1, 3, 6, 7
2, 4, 1, 3, 5, 6, 7
2, 1, 3, 4, 5, 6, 7
1, 2, 3, 4, 5, 6, 7
Max N round will make it working.
Algorithm Performance
Time Performance
Execution Time - depend on inputs
n will be the inputs, Time will be T(n). So if we enlarge T, who the T(n) will increase?
2 * (n - 1) * (n -1)
T(n) < 2 * n * n
T(n) = O(n * n)
Space Complexity
Memory Space system uses.
Non-extremeElement(S[], n)
for example, x, y, z belongs to S, min(x, y, z), max(x, y, z), find the non mix, non max items.
System will only have 1 min, 1 max, so no matter how many items in S, we can pick first 3 items, system should be able to find the non-min non-max one.
O(3), bubble sort to 1, 2, 3. Then choose the middle one. O(3) + O(3) + O(1) = O(7) = O(1)
BaseConversion(n)
N - 10 will be convert to N - 3
for till n = 0 {
print n mod 3;
n = n /3
}
101(10) = 10202(3)
1 + log3n = O(2 * (1 + |log3n|)) = O(log3n) = O(log n)
Sum(A[], n)
s = 0, for every A[i], i = 0, 1, …, n-1, s = s + A[i]
O(1) + O(1) x n = O(n+1) = O(n)
PowerBruteForce
{
power = 1;
while (0 < r——){
power = power * 2;
}
return power;
}
O(2-n)
Recursive
LinearSum(A, n)
{
if(n=1){
return A[0];
}else{
return LinearSum(A, n-1) + A[n -1]
}
}
ReverseArray(A, lo, hi)
{
if(lo < hi){
Swap(A[lo], A[hi]);
ReverseArray(A, lo+1, hi -1);
}
}
power(2, r) = 2 * 2 * 2…R
recursive solution
power(2, r) = {
1 //if r = 0
2 * power(2, r-1)
}
Enhancement
power(2, r) = {
1 // if r = 0
2 * power(2, (r-1)/2) ‘2 //if r > 0 and odd
power(2, r/2)’2 //if r > 0 and even
}
Power(r)
inputs: r >=0
outputs: 2 * 2 * 2 …r
{
if(r = 0) return 1;
if(r is odd) return 2 * sqr(Power((r-1)/2));
else return sqr(Power(r/2));
}
O(logR)
Recursion Trace Performance
LinearSum(A, 5) - A[0] + A[1] + A[2] + A[3] + A[4]
LinearSum(A, 4) - A[0] + A[1] + A[2] + A[3]
LinearSum(A, 3) - A[0] + A[1] + A[2]
LinearSum(A, 2) - A[0] + A[1]
LinearSum(A, 1) - A[0]
N * O(1) = O(n)
Binary Recursion
BinarySum(A, i, n)
inputs: A, i >= 0, n >0
outputs: sum of start position i, length n
{
if(n <=1) return A[i];
else return BinarySum(A, i, [n/2]) + BinarySum(A, i+[n/2], [n/2]);
}
Fibonacci
Fib(n) = {
0 // if n = 0
1 // if n = 1
Fib(n -1) + Fib(n-2) //if n>=2
}
BinaryFib(k)
inputs: k >=0
outputs: Fib(k)
{
if (k<=1) return k;
else return BinaryFib(k-1) + BinaryFib(k-2);
}
Performance O(2 * 2 * 2 ..n)
LinearFibonacci(k)
inputs: k >=0
outputs: Fibonacci (Fib(k), Fib(k-1))
{
if( k < = 1){
return (k, 0);
}
else {
(i , j ) = LinearFibonacci(k-1);
return (i+j, i);
}
}
O(n)
Multiple Recursion
References:
https://codility.com/programmers/lessons/1-iterations/
https://www.codecademy.com/learn/all
https://leetcode.com/
https://github.com/kamyu104/LeetCode
https://github.com/leetcoders/LeetCode-Java
http://www.programcreek.com/2012/12/leetcode-solution-of-two-sum-in-java/
https://www.gitbook.com/book/lidinghao/leetcode-java/details
分享到:
相关推荐
This is a deeply technical book and focuses on the software engineering skills to ace your interview. The book is over 500 pages and includes 150 programming interview questions and answers, as well ...
This is a deeply technical book and focuses on the software engineering skills to ace your interview. The book is over 500 pages and includes 150 programming interview questions and answers, as well ...
Cracking the Coding Interview(6th).pdf 程序员面试经典书籍 <br/>Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling ...
CHAPTER 1: INTRODUCTION - PROGRAMMING OVERVIEW CHAPTER 2: ALGORITHMS ANALYSIS CHAPTER 3: APPROACH TO SOLVE ALGORITHM DESIGN PROBLEMS CHAPTER 4: ABSTRACT DATA TYPE & C++ COLLECTIONS CHAPTER 5: ...
CHAPTER 1: INTRODUCTION - PROGRAMMING OVERVIEW CHAPTER 2: ALGORITHMS ANALYSIS CHAPTER 3: APPROACH TO SOLVE ALGORITHM DESIGN PROBLEMS CHAPTER 4: ABSTRACT DATA TYPE & JAVA COLLECTIONS CHAPTER 5: ...
Title: Problem Solving in Data Structures & Algorithms Using C++: Programming Interview Guide Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2017-01-08 ISBN...
This book is about the usage of data structures and algorithms in computer programming. Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This ...
The book is easy to follow and is written for interview preparation point of view. In these books, the examples are solved in various languages like Go, C, C++, Java, C#, Python, VB, JavaScript and ...
book is about learning data structure and algorithm. Introduction Part I is some brief introduction of basic data structures and algorithm, such as linked lists, stack, queues, trees, sorting. Part II...
Designing an efficient algorithm is a very important skill that all software companies, e.g. Microsoft, Google, Facebook etc. pursues. Most of the interviews for these companies are focused on ...
Interview book, including algorithm, database, shell, concurrency problems and other online contests. Category LeetCode Algorithms # TitleDifficulty Category Solution Notes 1 Easy MathArray 枚举哈希表...
interview questions => Probability and Statistics => Advanced Machine Learning Topics 本培训材料仅包含Algorithm and Data Structure 。 几年前我在准备算法竞赛ACM-ICPC时使用它。 虽然我忘记了大部分内容。 ...
There are multiple solutions for each problem and the book is coded in C/C++, it comes handy as an interview and exam guide for computer scientists. A handy guide of sorts for any computer science ...
cook_book # python cookbook练习 ├── documentation ├── interview # 面试题练习 ├── leetcode # leetcode散装练习 ├── leetcodeDay # leetcode按月打卡练习2020 │ ├── April │ ├── July ...
Up to date (2016-08-22), there are 289 problems on . The number of problems is increasing recently. Here is the classification of all 289 problems. For more problems and...O(1) O(1) Medium 82 O(n) O(1) E
식식식식전전전사전 :open_book: 自:2019.03.01 合作者 提交约定规则:날짜-[주제]-내용-상태 ex) 2019-10-14 [Algorithm] Sort Add/Update/Delete 내 용 잘 못 된 은 와 로 알 려 주 세 요 :light_bulb: :...
:blue_book:电子书 这是我将我的所有公开的算法资料整理的一个电子书,全部译文信息中文化,以前会有一些英语描述,感谢@CYL的中文整理。 限时免费下载!后期随时可能收费 有些动图,在少量电子书(只有pdf)的时候...