Skip to content

Commit 6a6c1ac

Browse files
committed
refactor: separate mdx files
1 parent d005985 commit 6a6c1ac

68 files changed

Lines changed: 1154 additions & 1260 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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]));
Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,58 @@
11
---
22
title: 1346. Check If N and Its Double Exist
33
description: Given an array arr of integers, check if there exist two indices i and j such that
4-
keywords: [LeetCode, 1346. Check If N and Its Double Exist, Easy, Array ,Hash Table, Two Pointers, Binary Search, Sorting]
4+
keywords:
5+
[
6+
LeetCode,
7+
1346. Check If N and Its Double Exist,
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/check-if-n-and-its-double-exist)
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 an array `arr` of integers, check if there exist two indices `i` and `j` such that :
20-
21-
- `i != j`
22-
- `0 <= i, j < arr.length`
23-
- `arr[i] == 2 * arr[j]`
24-
25-
#### Example 1:
26-
> **Input:**: arr = [10,2,5,3]
27-
**Output:**: true
28-
**Explanation:**: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
29-
30-
#### Example 2:
31-
>**Input:** arr = [3,1,7,11]
32-
**Output:** false
33-
**Explanation:** There is no i and j that satisfy the conditions.
34-
35-
#### Constraints:
36-
- `1 <= numBottles <= 100`
37-
- `2 <= numExchange <= 100`
38-
32+
<details open>
33+
<summary>Description</summary>
34+
<Description />
35+
<Examples />
36+
</details>
3937
</TabItem>
38+
4039
<TabItem value="solution" label="解答">
4140
## Solution
42-
```js
43-
/**
44-
* @param {number[]} arr
45-
* @return {boolean}
46-
*/
47-
var checkIfExist = function (arr) {
48-
const existSet = new Set();
49-
50-
for (let i = 0; i < arr.length; i++) {
51-
if (existSet.has(arr[i] * 2) || existSet.has(arr[i] / 2)) {
52-
return true;
53-
}
54-
55-
existSet.add(arr[i]);
56-
}
57-
58-
return false;
59-
};
60-
```
41+
<CodeBlock language="js">{Solution}</CodeBlock>
6142
</TabItem>
6243
</Tabs>
6344

64-
6545
## 解題思路
46+
6647
使用一個 Set 來不重複的記錄下已經過的值,接著遍歷一遍 `arr`,共有兩種狀況會判斷為 true
67-
1. 若當下的數字 `arr[i]` 的兩倍存在於 `Set` 之中的話就代表當下的數字符合 `j` 且 Set 中抓到的那個數字符合 `i`
68-
2. 若當下的數字 `arr[i]` 的一半存在於 `Set` 之中的話就代表當下的數字符合 `i` 且 Set 中抓到的那個數字符合 `j`
6948

70-
檢查完兩者後若都不符合就把當前的 `arr[i]` 加進 `Set`
49+
1. 若當下的數字 `arr[i]` 的兩倍存在於 `Set` 之中的話就代表當下的數字符合 `j` 且 Set 中抓到的那個數字符合 `i`
50+
2. 若當下的數字 `arr[i]` 的一半存在於 `Set` 之中的話就代表當下的數字符合 `i` 且 Set 中抓到的那個數字符合 `j`
51+
52+
檢查完兩者後若都不符合就把當前的 `arr[i]` 加進 `Set`
7153

7254
遍歷完都不符合的話就是 false 了
7355

7456
## 心得
75-
直覺想到用 Set,結果成功了好耶!
57+
58+
直覺想到用 Set,結果成功了好耶!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Given an array `arr` of integers, check if there exist two indices `i` and `j` such that :
2+
3+
- `i != j`
4+
- `0 <= i, j < arr.length`
5+
- `arr[i] == 2 * arr[j]`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#### Example 1:
2+
3+
> **Input:**: arr = [10,2,5,3]
4+
> **Output:**: true
5+
> **Explanation:**: For i = 0 and j = 2, arr[i] == 10 == 2 _ 5 == 2 _ arr[j]
6+
7+
#### Example 2:
8+
9+
> **Input:** arr = [3,1,7,11]
10+
> **Output:** false
11+
> **Explanation:** There is no i and j that satisfy the conditions.
12+
13+
#### Constraints:
14+
15+
- `1 <= numBottles <= 100`
16+
- `2 <= numExchange <= 100`

0 commit comments

Comments
 (0)