Merge branch 'master' into master

This commit is contained in:
极客学伟
2021-11-25 13:05:44 +08:00
committed by GitHub
9 changed files with 240 additions and 120 deletions

View File

@ -195,23 +195,26 @@ public:
## 其他语言版本
Java
> 贪心法:
```java
// 贪心思路
class Solution {
public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
// 找到一个最小的购入点
int low = Integer.MAX_VALUE;
// res不断更新直到数组循环完毕
int res = 0;
for(int i = 0; i < prices.length; i++){
low = Math.min(prices[i], low);
res = Math.max(prices[i] - low, res);
}
return maxprofit;
return res;
}
}
```
> 动态规划:版本一
```java
// 解法1
class Solution {
@ -233,30 +236,30 @@ class Solution {
}
```
> 动态规划:版本二
``` java
class Solution { // 动态规划解法
public int maxProfit(int[] prices) {
// 可交易次数
int k = 1;
// [天数][交易次数][是否持有股票]
int[][][] dp = new int[prices.length][k + 1][2];
// bad case
dp[0][0][0] = 0;
dp[0][0][1] = Integer.MIN_VALUE;
dp[0][1][0] = Integer.MIN_VALUE;
dp[0][1][1] = -prices[0];
for (int i = 1; i < prices.length; i++) {
for (int j = k; j >= 1; j--) {
// dp公式
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
}
return dp[prices.length - 1][k][0] > 0 ? dp[prices.length - 1][k][0] : 0;
class Solution {
public int maxProfit(int[] prices) {
int[] dp = new int[2];
dp[0] = -prices[0];
dp[1] = 0;
// 可以参考斐波那契问题的优化方式
// dp[0] 和 dp[1], 其实是第 0 天的数据
// 所以我们从 i=1 开始遍历数组,一共有 prices.length 天,
// 所以是 i<=prices.length
for (int i = 1; i <= prices.length; i++) {
// 前一天持有;或当天买入
dp[0] = Math.max(dp[0], -prices[i - 1]);
// 如果 dp[0] 被更新,那么 dp[1] 肯定会被更新为正数的 dp[1]
// 而不是 dp[0]+prices[i-1]==0 的0
// 所以这里使用会改变的dp[0]也是可以的
// 当然 dp[1] 初始值为 0 ,被更新成 0 也没影响
// 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行
dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
}
return dp[1];
}
}
```

View File

@ -167,6 +167,25 @@ class Solution { // 动态规划
}
```
```java
// 优化空间
class Solution {
public int maxProfit(int[] prices) {
int[] dp=new int[2];
// 0表示持有1表示卖出
dp[0]=-prices[0];
dp[1]=0;
for(int i=1; i<=prices.length; i++){
// 前一天持有; 或当天卖出然后买入
dp[0]=Math.max(dp[0], dp[1]-prices[i-1]);
// 前一天卖出; 或当天卖出,当天卖出,得先持有
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
}
return dp[1];
}
}
```
### Python

View File

@ -188,7 +188,7 @@ dp[1] = max(dp[1], dp[0] - prices[i]); 如果dp[1]取dp[1],即保持买入股
## 其他语言版本
Java
### Java
```java
// 版本一
@ -221,25 +221,30 @@ class Solution {
// 版本二: 空间优化
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int[] dp = new int[5];
dp[1] = -prices[0];
dp[3] = -prices[0];
for (int i = 1; i < len; i++) {
dp[1] = Math.max(dp[1], dp[0] - prices[i]);
dp[2] = Math.max(dp[2], dp[1] + prices[i]);
dp[3] = Math.max(dp[3], dp[2] - prices[i]);
dp[4] = Math.max(dp[4], dp[3] + prices[i]);
int[] dp=new int[4];
// 存储两天的状态就行了
// dp[0]代表第一次买入
dp[0]=-prices[0];
// dp[1]代表第一次卖出
dp[1]=0;
// dp[2]代表第二次买入
dp[2]=-prices[0];
// dp[3]代表第二次卖出
dp[3]=0;
for(int i=1; i<=prices.length; i++){
// 要么保持不变,要么没有就买,有了就卖
dp[0]=Math.max(dp[0], -prices[i-1]);
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
// 这已经是第二天了,所以得加上前一天卖出去的价格
dp[2]=Math.max(dp[2], dp[1]-prices[i-1]);
dp[3]=Math.max(dp[3], dp[2]+prices[i-1]);
}
return dp[4];
return dp[3];
}
}
```
Python
### Python
> 版本一:
```python
@ -308,7 +313,7 @@ func max(a,b int)int{
JavaScript
### JavaScript
> 版本一:
@ -347,7 +352,7 @@ const maxProfit = prices => {
};
```
Go:
### Go
> 版本一:
```go
@ -381,5 +386,7 @@ func max(a, b int) int {
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 134. 加油站
# 134. 加油站
[力扣题目链接](https://leetcode-cn.com/problems/gas-station/)
@ -23,32 +23,27 @@
示例 1:
输入:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
* gas = [1,2,3,4,5]
* cost = [3,4,5,1,2]
输出: 3
解释:
从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此3 可为起始索引。
* 从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
* 开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
* 开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
* 开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
* 开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
* 开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
* 因此3 可为起始索引。
示例 2:
输入:
gas = [2,3,4]
cost = [3,4,3]
* gas = [2,3,4]
* cost = [3,4,3]
输出: -1
解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。
* 输出: -1
* 解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。
## 暴力方法
@ -196,7 +191,7 @@ public:
## 其他语言版本
Java
### Java
```java
// 解法1
class Solution {
@ -239,8 +234,9 @@ class Solution {
return index;
}
}
```
Python
```
### Python
```python
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
@ -257,7 +253,7 @@ class Solution:
return start
```
Go
### Go
```go
func canCompleteCircuit(gas []int, cost []int) int {
curSum := 0
@ -278,7 +274,7 @@ func canCompleteCircuit(gas []int, cost []int) int {
}
```
Javascript:
### Javascript
暴力:
```js
var canCompleteCircuit = function(gas, cost) {
@ -343,7 +339,7 @@ var canCompleteCircuit = function(gas, cost) {
};
```
C:
### C
```c
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int curSum = 0;

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 135. 分发糖果
# 135. 分发糖果
[力扣题目链接](https://leetcode-cn.com/problems/candy/)
@ -19,15 +19,14 @@
那么这样下来,老师至少需要准备多少颗糖果呢?
示例 1:
输入: [1,0,2]
输出: 5
解释: 你可以分别给这三个孩子分发 2、1、2 颗糖果。
* 输入: [1,0,2]
* 输出: 5
* 解释: 你可以分别给这三个孩子分发 2、1、2 颗糖果。
示例 2:
输入: [1,2,2]
输出: 4
解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。
第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
* 输入: [1,2,2]
* 输出: 4
* 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
## 思路
@ -127,7 +126,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
/**
@ -161,7 +160,7 @@ class Solution {
}
```
Python
### Python
```python
class Solution:
def candy(self, ratings: List[int]) -> int:
@ -175,7 +174,7 @@ class Solution:
return sum(candyVec)
```
Go
### Go
```golang
func candy(ratings []int) int {
/**先确定一边,再确定另外一边
@ -213,7 +212,8 @@ func findMax(num1 int ,num2 int) int{
return num2
}
```
Javascript:
### Javascript:
```Javascript
var candy = function(ratings) {
let candys = new Array(ratings.length).fill(1)

View File

@ -222,19 +222,32 @@ class Solution {
//版本三:一维 dp数组
class Solution {
public int maxProfit(int k, int[] prices) {
//在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况
if(prices.length==0)return 0;
int[] dp=new int[2*k+1];
for (int i = 1; i <2*k ; i+=2) {
dp[i]=-prices[0];
if(prices.length == 0){
return 0;
}
for (int i = 0; i <prices.length ; i++) {
for (int j = 1; j <2*k ; j+=2) {
dp[j]=Math.max(dp[j],dp[j-1]-prices[i]);
dp[j+1]=Math.max(dp[j+1],dp[j]+prices[i]);
if(k == 0){
return 0;
}
// 其实就是123题的扩展123题只用记录2天的状态
// 这里记录k天的状态就行了
// 每天都有买入,卖出两个状态,所以要乘 2
int[] dp = new int[2 * k];
// 按123题解题格式那样做一个初始化
for(int i = 0; i < dp.length / 2; i++){
dp[i * 2] = -prices[0];
}
for(int i = 1; i <= prices.length; i++){
dp[0] = Math.max(dp[0], -prices[i - 1]);
dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
// 还是与123题一样与123题对照来看
// 就很容易啦
for(int j = 2; j < dp.length; j += 2){
dp[j] = Math.max(dp[j], dp[j - 1] - prices[i-1]);
dp[j + 1] = Math.max(dp[j + 1], dp[j] + prices[i - 1]);
}
}
return dp[2*k];
// 返回最后一天卖出状态的结果就行了
return dp[dp.length - 1];
}
}
```

View File

@ -387,5 +387,82 @@ class MyQueue {
}
```
C:
```C
/*
1.两个type为int的数组大小为100
第一个栈stackIn用来存放数据第二个栈stackOut作为辅助用来输出数据
2.两个指针stackInTop和stackOutTop分别指向栈顶
*/
typedef struct {
int stackInTop, stackOutTop;
int stackIn[100], stackOut[100];
} MyQueue;
/*
1.开辟一个队列的大小空间
2.将指针stackInTop和stackOutTop初始化为0
3.返回开辟的队列
*/
MyQueue* myQueueCreate() {
MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
queue->stackInTop = 0;
queue->stackOutTop = 0;
return queue;
}
/*
将元素存入第一个栈中,存入后栈顶指针+1
*/
void myQueuePush(MyQueue* obj, int x) {
obj->stackIn[(obj->stackInTop)++] = x;
}
/*
1.若输出栈为空且当第一个栈中有元素stackInTop>0时将第一个栈中元素复制到第二个栈中stack2[stackTop2++] = stack1[--stackTop1])
2.将栈顶元素保存
3.当stackTop2>0时将第二个栈中元素复制到第一个栈中(stack1[stackTop1++] = stack2[--stackTop2])
*/
int myQueuePop(MyQueue* obj) {
//优化:复制栈顶指针,减少对内存的访问次数
int stackInTop = obj->stackInTop;
int stackOutTop = obj->stackOutTop;
//若输出栈为空
if(stackOutTop == 0) {
//将第一个栈中元素复制到第二个栈中
while(stackInTop > 0) {
obj->stackOut[stackOutTop++] = obj->stackIn[--stackInTop];
}
}
//将第二个栈中栈顶元素(队列的第一个元素)出栈,并保存
int top = obj->stackOut[--stackOutTop];
//将输出栈中元素放回输入栈中
while(stackOutTop > 0) {
obj->stackIn[stackInTop++] = obj->stackOut[--stackOutTop];
}
//更新栈顶指针
obj->stackInTop = stackInTop;
obj->stackOutTop = stackOutTop;
//返回队列中第一个元素
return top;
}
//返回输入栈中的栈底元素
int myQueuePeek(MyQueue* obj) {
return obj->stackIn[0];
}
//若栈顶指针均为0则代表队列为空
bool myQueueEmpty(MyQueue* obj) {
return obj->stackInTop == 0 && obj->stackOutTop == 0;
}
//将栈顶指针置0
void myQueueFree(MyQueue* obj) {
obj->stackInTop = 0;
obj->stackOutTop = 0;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 860.柠檬水找零
# 860.柠檬水找零
[力扣题目链接](https://leetcode-cn.com/problems/lemonade-change/)
@ -20,30 +20,30 @@
如果你能给每位顾客正确找零返回 true 否则返回 false 
示例 1
输入:[5,5,5,10,20]
输出true
解释:
前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
由于所有客户都得到了正确的找零,所以我们输出 true。
* 输入:[5,5,5,10,20]
* 输出true
* 解释:
* 前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
* 第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
* 第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
* 由于所有客户都得到了正确的找零,所以我们输出 true。
示例 2
输入:[5,5,10]
输出true
* 输入:[5,5,10]
* 输出true
示例 3
输入:[10,10]
输出false
* 输入:[10,10]
* 输出false
示例 4
输入:[5,5,10,10,20]
输出false
解释:
前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
由于不是每位顾客都得到了正确的找零,所以答案是 false。
* 输入:[5,5,10,10,20]
* 输出false
* 解释:
* 前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
* 对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
* 对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
* 由于不是每位顾客都得到了正确的找零,所以答案是 false。
提示:
@ -124,7 +124,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
public boolean lemonadeChange(int[] bills) {
@ -153,7 +153,7 @@ class Solution {
}
```
Python
### Python
```python
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
@ -179,7 +179,7 @@ class Solution:
```
Go
### Go
```golang
func lemonadeChange(bills []int) bool {
@ -221,7 +221,7 @@ func lemonadeChange(bills []int) bool {
}
```
Javascript:
### Javascript
```Javascript
var lemonadeChange = function(bills) {
let fiveCount = 0

View File

@ -115,6 +115,11 @@
能把本篇中列举的题目都研究通透的话,你的动规水平就已经非常高了。 对付面试已经足够!
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211121223754.png)
这个图是 [代码随想录知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) 成员:[](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。
这已经是全网对动规最深刻的讲解系列了。
**其实大家去网上搜一搜也可以发现,能把动态规划讲清楚的资料挺少的,因为动规确实很难!要给别人讲清楚更难!**