Skip to content

Commit 34df2c1

Browse files
committed
refactor: separate mdx files
1 parent d005985 commit 34df2c1

23 files changed

Lines changed: 342 additions & 383 deletions

File tree

docs/LeetCode/11. Container With Most Water/README.mdx

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,76 @@
22
title: 11. Container With Most Water
33
description: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).Find two lines that together with the x-axis form a container, such that the container contains the most water.Return the maximum amount of water a container can store.Notice that you may not slant the container.
44
keywords:
5-
[
6-
LeetCode,
7-
11. Container With Most Water,
8-
Medium,
9-
Array,
10-
Two Pointers,
11-
Greedy,
12-
]
5+
[LeetCode, 11. Container With Most Water, Medium, Array, Two Pointers, Greedy]
6+
tags: [LeetCode, Medium, Array, Two Pointers, Greedy]
137
---
148

159
import Tabs from "@theme/Tabs";
1610
import TabItem from "@theme/TabItem";
17-
import CodeBlock from '@theme/CodeBlock';
11+
import CodeBlock from "@theme/CodeBlock";
1812
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
19-
import Solution from '!!raw-loader!./solution.js';
13+
import Solution from "!!raw-loader!./solution.js";
14+
import Description from "./_Description.md";
15+
import Examples from "./_Examples.md";
2016

2117
# [{frontMatter.title}](https://leetcode.com/problems/container-with-most-water)
2218

2319
<DifficultyBadge difficulty="Medium" />
2420

2521
<Tabs>
2622
<TabItem value="description" label="題目描述" default>
27-
## Description
28-
You are given an integer array height of length `n`. There are n vertical lines drawn such that the two endpoints of the `ith` line are `1(i, 0)` and `(i, height[i])`.
29-
30-
Find two lines that together with the x-axis form a container, such that the container contains the most water.
31-
32-
Return the maximum amount of water a container can store.
33-
34-
**Notice** that you may not slant the container.
35-
36-
37-
#### Example 1:
38-
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
39-
> **Input:** height = [1,8,6,2,5,4,8,3,7]
40-
**Output:** 49
41-
**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
42-
43-
#### Example 2:
44-
> **Input:** height = [1,1]
45-
**Output:** 1
46-
23+
<details open>
24+
<summary>Description</summary>
25+
<Description />
26+
<Examples />
27+
</details>
4728
</TabItem>
29+
4830
<TabItem value="solution" label="解答">
4931
## Solution
5032
<CodeBlock language="js">{Solution}</CodeBlock>
5133
</TabItem>
5234
</Tabs>
5335

54-
5536
## 解題思路
56-
37+
5738
1. 先定義好 `left pointer``right pointer` 分別指向 height 的頭與尾,以及 mostWater 紀錄目前的最大水量值。
5839
```js
5940
let left = 0;
6041
let right = height.length - 1;
6142
let mostWater = 0;
6243
```
6344
2. 重複執行程式直到 right 和 left 重合。
64-
```js
65-
while (right > left) {
6645

67-
}
68-
```
69-
3. 使用 waterCurrently 算出目前水量,因為最大水兩高度是由兩邊中較短的決定的(假設一邊是 8 一邊是 5,水最高只能到 5,再多就會從水槽流出,而 right - left 是寬度,`長 * 寬`就是目前水量了。
46+
```js
47+
while (right > left) {}
48+
```
49+
50+
3. 使用 waterCurrently 算出目前水量,因為最大水兩高度是由兩邊中較短的決定的(假設一邊是 8 一邊是 5,水最高只能到 5,再多就會從水槽流出,而 right - left 是寬度,`長 * 寬`就是目前水量了。
7051
若 waterCurrently > mostWater 則更新。
7152

72-
```js
73-
const waterCurrently = Math.min(height[left], height[right]) * (right - left);
53+
```js
54+
const waterCurrently = Math.min(height[left], height[right]) * (right - left);
7455

75-
mostWater = Math.max(mostWater, waterCurrently);
76-
```
56+
mostWater = Math.max(mostWater, waterCurrently);
57+
```
7758

7859
4. 如果左邊較短的話就把 `left pointer` 向右移動,反之則把 `right point` 向左移。
79-
```js
80-
if (height[left] < height[right]) {
81-
left++;
82-
} else {
83-
right--;
84-
}
85-
```
86-
5. 最後回傳 mostWater 就是答案了。
87-
```js
88-
return mostWater;
89-
```
9060

61+
```js
62+
if (height[left] < height[right]) {
63+
left++;
64+
} else {
65+
right--;
66+
}
67+
```
9168

69+
5. 最後回傳 mostWater 就是答案了。
70+
71+
```js
72+
return mostWater;
73+
```
9274

9375
## 心得
76+
9477
練習 Two Pointers,題目光看描述覺得很抽象,但有圖片輔助說明有好懂了不少!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#### Example 1:
2+
3+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
4+
5+
> **Input:** height = [1,8,6,2,5,4,8,3,7]
6+
> **Output:** 49
7+
> **Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
8+
9+
#### Example 2:
10+
11+
> **Input:** height = [1,1]
12+
> **Output:** 1

docs/LeetCode/11. Container With Most Water/solution.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ var maxArea = function (height) {
2222

2323
return mostWater;
2424
};
25-
26-
console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));

docs/LeetCode/209. Minimum Size Subarray Sum/README.mdx

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,91 +11,42 @@ keywords:
1111
Sliding Window,
1212
Prefix Sum,
1313
]
14+
tags: [LeetCode, Medium, Array, Binary Search, Sliding Window, Prefix Sum]
1415
---
1516

1617
# [{frontMatter.title}](https://leetcode.com/problems/minimum-size-subarray-sum/)
1718

1819
import Tabs from "@theme/Tabs";
1920
import TabItem from "@theme/TabItem";
21+
import CodeBlock from "@theme/CodeBlock";
2022
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
23+
import Solution from "!!raw-loader!./solution.js";
24+
import Description from "./_Description.md";
25+
import Examples from "./_Examples.md";
2126

2227
<DifficultyBadge difficulty="Medium" />
2328

2429
<Tabs>
2530
<TabItem value="description" label="題目描述" default>
26-
## Description
27-
28-
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
29-
30-
#### Example 1:
31-
32-
Input: target = 7, nums = [2,3,1,2,4,3]
33-
Output: 2
34-
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
35-
36-
#### Example 2:
37-
38-
Input: target = 4, nums = [1,4,4]
39-
Output: 1
40-
41-
#### Example 3:
42-
43-
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
44-
Output: 0
45-
46-
### Constraints:
47-
48-
```
49-
1 <= target <= 109
50-
1 <= nums.length <= 105
51-
1 <= nums[i] <= 104
52-
```
53-
54-
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)).
55-
31+
<details open>
32+
<summary>Description</summary>
33+
<Description />
34+
<Examples />
35+
</details>
5636
</TabItem>
37+
5738
<TabItem value="solution" label="解答">
5839
## Solution
59-
```js
60-
/**
61-
* @param {number} target
62-
* @param {number[]} nums
63-
* @return {number}
64-
*/
65-
var minSubArrayLen = function (target, nums) {
66-
let minLength = nums.length + 1;
67-
let left = 0;
68-
let right = 0;
69-
let currentSum = 0;
70-
71-
while (right < nums.length) {
72-
currentSum += nums[right];
73-
74-
while (currentSum >= target) {
75-
if (minLength > right - left + 1) {
76-
minLength = right - left + 1;
77-
}
78-
currentSum -= nums[left];
79-
left++;
80-
}
81-
82-
right++;
83-
84-
}
85-
if (minLength === nums.length + 1) {
86-
return 0;
87-
} else {
88-
return minLength;
89-
}
90-
};
91-
```
40+
<CodeBlock language="js">{Solution}</CodeBlock>
9241
</TabItem>
9342
</Tabs>
9443

9544
### 解題思路
9645

97-
練習 slide window,首先由 right 開始慢慢往右移動,並加總 left 和 right 範圍的這個 window 中的總和,直到大於 target。當大於 target 時候 left 就往右移動來減少 minLength 的可能值,重複移動 left 和 right 直到找出正確答案。
46+
練習 `Sliding Window`
47+
首先由 `right` 開始慢慢往右移動,並加總 `left``right` 範圍的這個 window 中的總和,直到大於 `target`
48+
當大於 `target` 時候 `left` 就往右移動來減少 `minLength` 的可能值,重複移動 `left``right` 直到找出正確答案。
9849

9950
### 心得
10051

101-
也是看了 [Wilson Ren 的 資料結構與演算法 (JavaScript)](https://www.udemy.com/course/algorithm-data-structure) 介紹這題經典題,要把這些題型弄熟打好演算法基礎!
52+
也是看了 [Wilson Ren 的 資料結構與演算法 (JavaScript)](https://www.udemy.com/course/algorithm-data-structure) 介紹這題經典題,要把這些題型弄熟打好演算法基礎!
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You are given an integer array height of length `n`. There are n vertical lines drawn such that the two endpoints of the `ith` line are `1(i, 0)` and `(i, height[i])`.
2+
3+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
4+
5+
Return the maximum amount of water a container can store.
6+
7+
**Notice** that you may not slant the container.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#### Example 1:
2+
3+
> **Input:** target = 7, nums = [2,3,1,2,4,3]
4+
> **Output:** 2
5+
> **Explanation:** The subarray [4,3] has the minimal length under the problem constraint.
6+
7+
#### Example 2:
8+
9+
> **Input:** target = 4, nums = [1,4,4]
10+
> **Output:** 1
11+
12+
#### Example 3:
13+
14+
> **Input:** target = 11, nums = [1,1,1,1,1,1,1,1]
15+
> **Output:** 0
16+
17+
### Constraints:
18+
19+
```
20+
1 <= target <= 109
21+
1 <= nums.length <= 105
22+
1 <= nums[i] <= 104
23+
```
24+
25+
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)).
Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,55 @@
11
---
22
title: 349. Intersection of Two Arrays
33
description: Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
4-
keywords: [LeetCode, 349. Intersection of Two Arrays, Easy,Array,Hash Table,Two Pointers,Binary Search,Sorting]
4+
keywords:
5+
[
6+
LeetCode,
7+
349. Intersection of Two Arrays,
8+
Easy,
9+
Array,
10+
Hash Table,
11+
Two Pointers,
12+
Binary Search,
13+
Sorting,
14+
]
15+
tags: [LeetCode, Easy, Array, Hash Table, Two Pointers, Binary Search, Sorting]
516
---
617

718
# [{frontMatter.title}](https://leetcode.com/problems/intersection-of-two-arrays/)
819

920
import Tabs from "@theme/Tabs";
1021
import TabItem from "@theme/TabItem";
22+
import CodeBlock from "@theme/CodeBlock";
1123
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
24+
import Solution from "!!raw-loader!./solution.js";
25+
import Description from "./_Description.md";
26+
import Examples from "./_Examples.md";
1227

1328
<DifficultyBadge difficulty="Easy" />
1429

1530
<Tabs>
1631
<TabItem value="description" label="題目描述" default>
17-
## Description
18-
19-
Given two integer arrays nums1 and nums2, return an array of their
20-
intersection. Each element in the result must be unique and you may return the result in any order.
21-
22-
#### Example 1:
23-
Input: nums1 = [1,2,2,1], nums2 = [2,2]
24-
Output: [2]
25-
26-
#### Example 2:
27-
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
28-
Output: [9,4]
29-
Explanation: [4,9] is also accepted.
30-
31-
#### Constraints:
32-
```
33-
1 <= nums1.length, nums2.length <= 1000
34-
0 <= nums1[i], nums2[i] <= 1000
35-
```
36-
32+
<details open>
33+
<summary>Description</summary>
34+
<Description />
35+
<Examples />
36+
</details>
3737
</TabItem>
38+
3839
<TabItem value="solution" label="解答">
3940
## Solution
40-
```js
41-
/**
42-
* @param {number[]} nums1
43-
* @param {number[]} nums2
44-
* @return {number[]}
45-
*/
46-
var intersection = function (nums1, nums2) {
47-
const set1 = new Set(nums1);
48-
const set2 = new Set(nums2);
49-
50-
return [...set1].filter((num) => set2.has(num));
51-
};
52-
```
41+
<CodeBlock language="js">{Solution}</CodeBlock>
5342
</TabItem>
5443
</Tabs>
5544

56-
5745
## 解題思路
58-
原本想用 Counter 之類的做法,但看到 `Set 的特色是有 has() 這個方法,可以快速判斷該 Set 中是否包含某個元素` 就直接用這個特性來大幅簡化了。
46+
47+
原本想用 Counter 之類的做法,但看到:
48+
49+
> `Set 的特色是有 has() 這個方法,可以快速判斷該 Set 中是否包含某個元素`
50+
51+
就直接用這個特性來大幅簡化了。
5952

6053
## 心得
61-
謝謝PJ大佬的筆記 [[JS] JavaScript 集合(Set)](https://pjchender.dev/javascript/js-set/), 只要熟練語法基本就能秒解開一堆 easy 了。
54+
55+
謝謝 PJ 大佬的筆記 [[JS] JavaScript 集合(Set)](https://pjchender.dev/javascript/js-set/), 只要熟練語法基本就能秒解開一堆 easy 了。
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Given two integer arrays nums1 and nums2, return an array of their
2+
intersection. Each element in the result must be unique and you may return the result in any order.

0 commit comments

Comments
 (0)