Skip to content

Commit

Permalink
fix: remove useless comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer committed Jan 4, 2020
1 parent 23654b8 commit 3ab443a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 212 deletions.
30 changes: 3 additions & 27 deletions problems/209.minimum-size-subarray-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If you have figured out the O(n) solution, try coding another solution of which

## 代码

* 语言支持:JS,C++
- 语言支持:JS,C++

JavaScript Code:

Expand All @@ -41,31 +41,6 @@ JavaScript Code:
*
* [209] Minimum Size Subarray Sum
*
* https://leetcode.com/problems/minimum-size-subarray-sum/description/
*
* algorithms
* Medium (34.31%)
* Total Accepted: 166.9K
* Total Submissions: 484.9K
* Testcase Example: '7\n[2,3,1,2,4,3]'
*
* Given an array of n positive integers and a positive integer s, find the
* minimal length of a contiguous subarray of which the sum ≥ s. If there isn't
* one, return 0 instead.
*
* Example:
*
*
* Input: s = 7, nums = [2,3,1,2,4,3]
* Output: 2
* Explanation: the subarray [4,3] has the minimal length under the problem
* constraint.
*
* Follow up:
*
* If you have figured out the O(n) solution, try coding another solution of
* which the time complexity is O(n log n).
*
*/
/**
* @param {number} s
Expand Down Expand Up @@ -98,6 +73,7 @@ var minSubArrayLen = function(s, nums) {
```

C++ Code:

```C++
class Solution {
public:
Expand All @@ -109,7 +85,7 @@ public:
total += nums[right++];
} while (right < num_len && total < s);
while (left < right && total - nums[left] >= s) total -= nums[left++];
if (total >=s && min_len > right - left)
if (total >=s && min_len > right - left)
min_len = right- left;
}
return min_len <= num_len ? min_len: 0;
Expand Down
71 changes: 20 additions & 51 deletions problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## 题目地址

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/

## 题目描述
Expand All @@ -14,25 +14,26 @@ After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day
Example:
Input: [1,2,3,0,2]
Output: 3
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
```

## 思路
这是一道典型的DP问题, DP 问题的核心是找到状态和状态转移方程。

这道题目的状态似乎比我们常见的那种DP问题要多,这里的状态有buy sell cooldown三种,
这是一道典型的 DP 问题, DP 问题的核心是找到状态和状态转移方程。

这道题目的状态似乎比我们常见的那种 DP 问题要多,这里的状态有 buy sell cooldown 三种,
我们可以用三个数组来表示这这三个状态,buy,sell, cooldown.

- buy[i]表示第i天,且以buy结尾的最大利润
- sell[i]表示第i天,且以sell结尾的最大利润
- cooldown[i]表示第i天,且以sell结尾的最大利润
- buy[i]表示第 i 天,且以 buy 结尾的最大利润
- sell[i]表示第 i 天,且以 sell 结尾的最大利润
- cooldown[i]表示第 i 天,且以 sell 结尾的最大利润

我们思考一下,其实cooldown这个状态数组似乎没有什么用,因此cooldown不会对`profit`产生
任何影响。 我们可以进一步缩小为两种状态。
我们思考一下,其实 cooldown 这个状态数组似乎没有什么用,因此 cooldown 不会对`profit`产生
任何影响。 我们可以进一步缩小为两种状态。

- buy[i] 表示第i天,且以buy或者coolwown结尾的最大利润
- sell[i] 表示第i天,且以sell或者cooldown结尾的最大利润
- buy[i] 表示第 i 天,且以 buy 或者 coolwown 结尾的最大利润
- sell[i] 表示第 i 天,且以 sell 或者 cooldown 结尾的最大利润

对应的状态转移方程如下:

Expand All @@ -43,17 +44,17 @@ Explanation: transactions = [buy, sell, cooldown, buy, sell]
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
```

我们来分析一下,buy[i]对应第i的action只能是buy或者cooldown
我们来分析一下,buy[i]对应第 i 的 action 只能是 buy 或者 cooldown

- 如果是cooldown,那么profit就是 buy[i - 1]
- 如果是buy,那么就是`前一个卖的profit减去今天买股票花的钱`,即 sell[i -2] - prices[i]
- 如果是 cooldown,那么 profit 就是 buy[i - 1]
- 如果是 buy,那么就是`前一个卖的profit减去今天买股票花的钱`,即 sell[i -2] - prices[i]

> 注意这里是i - 2,不是 i-1 ,因为有cooldown的限制
> 注意这里是 i - 2,不是 i-1 ,因为有 cooldown 的限制
sell[i]对应第i的action只能是sell或者cooldown
sell[i]对应第 i 的 action 只能是 sell 或者 cooldown

- 如果是cooldown,那么profit就是 sell[i - 1]
- 如果是sell,那么就是`前一次买的时候获取的利润加上这次卖的钱`,即 buy[i - 1] + prices[i]
- 如果是 cooldown,那么 profit 就是 sell[i - 1]
- 如果是 sell,那么就是`前一次买的时候获取的利润加上这次卖的钱`,即 buy[i - 1] + prices[i]

## 关键点解析

Expand All @@ -62,42 +63,11 @@ sell[i]对应第i的action只能是sell或者cooldown。
## 代码

```js


/*
* @lc app=leetcode id=309 lang=javascript
*
* [309] Best Time to Buy and Sell Stock with Cooldown
*
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
*
* algorithms
* Medium (43.52%)
* Total Accepted: 88.3K
* Total Submissions: 201.4K
* Testcase Example: '[1,2,3,0,2]'
*
* Say you have an array for which the i^th element is the price of a given
* stock on day i.
*
* Design an algorithm to find the maximum profit. You may complete as many
* transactions as you like (ie, buy one and sell one share of the stock
* multiple times) with the following restrictions:
*
*
* You may not engage in multiple transactions at the same time (ie, you must
* sell the stock before you buy again).
* After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1
* day)
*
*
* Example:
*
*
* Input: [1,2,3,0,2]
* Output: 3
* Explanation: transactions = [buy, sell, cooldown, buy, sell]
*
*/
/**
* @param {number[]} prices
Expand Down Expand Up @@ -131,7 +101,6 @@ var maxProfit = function(prices) {
```

## 相关题目

- [121.best-time-to-buy-and-sell-stock](./121.best-time-to-buy-and-sell-stock.md)
- [122.best-time-to-buy-and-sell-stock-ii](./122.best-time-to-buy-and-sell-stock-ii.md)


91 changes: 29 additions & 62 deletions problems/328.odd-even-linked-list.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## 题目地址

https://leetcode.com/problems/odd-even-linked-list/description/

## 题目描述
Expand All @@ -24,11 +24,13 @@ The first node is considered odd, the second node even and so on ...
```

## 思路

符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。

但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。
但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是 N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。

因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。

因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。
## 关键点解析

- 用虚拟节点来简化操作
Expand All @@ -37,52 +39,17 @@ The first node is considered odd, the second node even and so on ...

## 代码

* 语言支持:JS,C++
- 语言支持:JS,C++

JavaScript Code:

```js
/*
* @lc app=leetcode id=328 lang=javascript
*
* [328] Odd Even Linked List
*
* https://leetcode.com/problems/odd-even-linked-list/description/
*
* algorithms
* Medium (48.22%)
* Total Accepted: 137.6K
* Total Submissions: 284.2K
* Testcase Example: '[1,2,3,4,5]'
*
* Given a singly linked list, group all odd nodes together followed by the
* even nodes. Please note here we are talking about the node number and not
* the value in the nodes.
*
* You should try to do it in place. The program should run in O(1) space
* complexity and O(nodes) time complexity.
*
* Example 1:
*
*
* Input: 1->2->3->4->5->NULL
* Output: 1->3->5->2->4->NULL
*
*
* Example 2:
*
*
* Input: 2->1->3->5->6->4->7->NULL
* Output: 2->3->6->7->1->5->4->NULL
*
*
* Note:
*
*
* The relative order inside both the even and odd groups should remain as it
* was in the input.
* The first node is considered odd, the second node even and so on ...
*
*
*/
/**
* Definition for singly-linked list.
Expand All @@ -96,37 +63,37 @@ JavaScript Code:
* @return {ListNode}
*/
var oddEvenList = function(head) {
if (!head || !head.next) return head;
if (!head || !head.next) return head;

const dummyHead1 = {
next: head
}
const dummyHead2 = {
next: head.next
}
const dummyHead1 = {
next: head
};
const dummyHead2 = {
next: head.next
};

let odd = dummyHead1.next;
let even = dummyHead2.next;

while(odd && odd.next && even && even.next) {
const oddNext = odd.next.next;
const evenNext = even.next.next;

odd.next = oddNext;
even.next = evenNext;

odd = oddNext;
even = evenNext;
}
let odd = dummyHead1.next;
let even = dummyHead2.next;

odd.next = dummyHead2.next;
while (odd && odd.next && even && even.next) {
const oddNext = odd.next.next;
const evenNext = even.next.next;

return dummyHead1.next;
odd.next = oddNext;
even.next = evenNext;

odd = oddNext;
even = evenNext;
}

odd.next = dummyHead2.next;

return dummyHead1.next;
};
```

C++ Code:

```C++
/**
* Definition for singly-linked list.
Expand Down
Loading

0 comments on commit 3ab443a

Please sign in to comment.