1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-20 22:17:09 +08:00
Java-Interview-Guide/docs/cs-basics/algorithms/classical-algorithm-problems-recommended.md
2023-10-28 18:18:37 +08:00

110 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 经典算法思想介绍
## 贪心算法
### 算法思想
贪心的本质是选择每一阶段的局部最优,从而达到全局最优。
### 一般解题步骤
- 将问题分解为若干个子问题
- 找出适合的贪心策略
- 求解每一个子问题的最优解
- 将局部最优解堆叠成全局最优解
### LeetCode
455.分发饼干https://leetcode.cn/problems/assign-cookies/
121.买卖股票的最佳时机https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
122.买卖股票的最佳时机IIhttps://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
55.跳跃游戏https://leetcode.cn/problems/jump-game/
45.跳跃游戏IIhttps://leetcode.cn/problems/jump-game-ii/
## 动态规划
### 算法思想
动态规划中每一个状态一定是由上一个状态推导出来的,这一点就区分于贪心,贪心没有状态推导,而是从局部直接选最优的。
经典题目01背包、完全背包
### 一般解题步骤
- 确定dp数组dp table以及下标的含义
- 确定递推公式
- dp数组如何初始化
- 确定遍历顺序
- 举例推导dp数组
### LeetCode
509.斐波那契数https://leetcode.cn/problems/fibonacci-number/
746.使用最小花费爬楼梯https://leetcode.cn/problems/min-cost-climbing-stairs/
416.分割等和子集https://leetcode.cn/problems/partition-equal-subset-sum/
518.零钱兑换https://leetcode.cn/problems/coin-change-ii/
647.回文子串https://leetcode.cn/problems/palindromic-substrings/
516.最长回文子序列https://leetcode.cn/problems/longest-palindromic-subsequence/
## 回溯算法
### 算法思想
回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条
件时,就“回溯”返回,尝试别的路径。其本质就是穷举。
经典题目8皇后
### 一般解题步骤
- 针对所给问题,定义问题的解空间,它至少包含问题的一个(最优)解。
- 确定易于搜索的解空间结构,使得能用回溯法方便地搜索整个解空间 。
- 以深度优先的方式搜索解空间,并且在搜索过程中用剪枝函数避免无效搜索。
### leetcode
77.组合https://leetcode.cn/problems/combinations/
39.组合总和https://leetcode.cn/problems/combination-sum/
40.组合总和IIhttps://leetcode.cn/problems/combination-sum-ii/
78.子集https://leetcode.cn/problems/subsets/
90.子集IIhttps://leetcode.cn/problems/subsets-ii/
51.N皇后https://leetcode.cn/problems/n-queens/
## 分治算法
### 算法思想
将一个规模为N的问题分解为K个规模较小的子问题这些子问题相互独立且与原问题性质相同。求出子问题的解就可得到原问题的解。
经典题目:二分查找、汉诺塔问题
### 一般解题步骤
- 将原问题分解为若干个规模较小,相互独立,与原问题形式相同的子问题;
- 若子问题规模较小而容易被解决则直接解,否则递归地解各个子问题
- 将各个子问题的解合并为原问题的解。
### LeetCode
108.将有序数组转换成二叉搜索数https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
148.排序列表https://leetcode.cn/problems/sort-list/
23.合并k个升序链表https://leetcode.cn/problems/merge-k-sorted-lists/