Summary of front-end algorithms

Basics – Time Complexity & Space Complexity

Complexity analysis is the essence of the entire algorithm learning. As long as you master it, you will basically master half of the content of data structures and algorithms.

What is complexity analysis?

Data structure and algorithm solving is "how to make computers faster and more space-saving to solve problems." Therefore, the performance of data structures and algorithms needs to be evaluated from the two dimensions of execution time and space occupied. The two concepts of time complexity and space complexity are used to describe performance problems, and they are collectively called complexity.
Complexity describes the growth relationship between the execution time (or space occupied) of an algorithm and the size of the data.

Why do complexity analysis?

Compared with performance testing, complexity analysis has the characteristics of not relying on the execution environment, low cost, high efficiency, easy operation, and strong guidance. Mastering complexity analysis will enable you to write code with better performance, which will help reduce system development and maintenance costs.

How to perform complexity analysis?

Big O notation

The execution time of the algorithm is proportional to the number of executions of each line of code, represented by T(n) = O(f(n)), where T(n) represents the total execution time of the algorithm, and f(n) represents the total execution time of each line of code. times, and n often represents the size of the data. This is Big O time complexity notation.

For more details, please search " " on WeChat前端爱好者 and click me to view .

double pointer

The sum of the nearest three numbers

Given an array nums containing n integers and a target value target.

Find three integers in nums such that their sum is closest to target. Returns the sum of these three numbers. It is assumed that there is only one answer for each set of inputs.

Match the longest word in the dictionary by removing letters

Given a string and a dictionary of strings, find the longest string in the dictionary that can be obtained by deleting certain characters from the given string.

If there is more than one answer, return the longest and smallest lexicographically long string. If the answer does not exist, an empty string is returned.

sliding window

Maximum value of sliding window

Given an array nums and a sliding window size k, find the maximum value in all sliding windows.

Binary tree

The most recent common ancestor of a binary tree

Given a binary tree, find the nearest common ancestor of two specified nodes in the tree.

The definition of the nearest common ancestor in Baidu Encyclopedia is: "For two nodes p and q of a rooted tree T, the nearest common ancestor is represented as a node x, such that x is the ancestor of p and q and the depth of x is as large as possible (A node can also be its own ancestor)."

For example, given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

heap

The smallest k number

Input the integer array arr and find the smallest k numbers among them. For example, if you enter 8 numbers: 4, 5, 1, 6, 2, 7, 3, and 8, the smallest 4 numbers are 1, 2, 3, and 4.

Top K high-frequency elements – bucket sort

Given a non-empty integer array, return the elements with the k highest occurrence frequencies.

Kth largest element in array – quickselect algorithm

Find the kth largest element in an unsorted array. Please note that you need to find the k-th largest element in the sorted array, not the k-th distinct element.

picture

Find the town judge

In a small town, N people are labeled from 1 to N. Rumor has it that one of these men is the town's secret judge.
If the town judge really existed, then:

  1. The town's judge doesn't believe anyone.
  2. Everyone (except the town judge) trusts the town judge.
  3. Only one person satisfies both attribute 1 and attribute 2.
    Given an array trust, the array consists of trust pairs trust[i] = [a, b], indicating that the person labeled a trusts the person labeled b.

If a secret judge exists in the town and his identity can be determined, return that judge's token. Otherwise, -1 is returned.

Curriculum issues

You must take numCourse courses this semester, numbered from 0 to numCourse-1.

Some prerequisite courses are required before taking certain courses. For example, if you want to study course 0, you need to complete course 1 first. We use a match to represent them: [0,1]
Given the total number of courses and their prerequisites, please judge whether it is possible to complete all courses?

dynamic programming

Stair climbing problem –dp[n] = dp[n−1] + dp[n−2]

Suppose you are climbing stairs. It takes n steps for you to reach the top of the building.
You can climb 1 or 2 steps at a time. How many different ways can you climb to the top of a building?

Note: Given n is a positive integer.

Climbing stairs with minimum cost –dp[i] = min(dp[i-2], dp[i-1]) + cost[i]

Each index of the array serves as a ladder, and the i-th ladder corresponds to a non-negative physical cost value cost[i] (index starts from 0).

Every time you climb a ladder, you have to spend the corresponding stamina cost, and then you can choose to continue climbing one ladder or climb two ladders.

You need to find the minimum cost to reach the top of the floor. At the beginning, you can choose the element from index 0 or 1 as the initial ladder.

Acho que você gosta

Origin blog.csdn.net/BradenHan/article/details/135418436
Recomendado
Clasificación