-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from mercari-build/add-algorithm-and-datastru…
…cture Add algorithm and datastructure
- Loading branch information
Showing
2 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Extra1: Algorithms and Data Structures | ||
|
||
In Extra1, after learning about basic algorithms and data structures, you will practice using problems from LeetCode. It is recommended to solve the problems before looking at the explanations. | ||
|
||
## Textbooks | ||
|
||
**:book: Reference** | ||
|
||
* (JA) [Algorithms, Data Structures, and Coding Test Introduction Taught by a Silicon Valley Engineer](https://mercari.udemy.com/course/python-algo/) | ||
|
||
* (EN) [Python Data Structures & Algorithms + LEETCODE Exercises](https://mercari.udemy.com/course/data-structures-algorithms-python/) | ||
|
||
**:beginner: Point** | ||
* First, look up the following terms and be able to explain what they are. | ||
* Time complexity and space complexity | ||
* Big O notation | ||
* Associative arrays | ||
* Study the following basic algorithms on Udemy or other platforms and be able to explain them. | ||
* What is binary search? Explain why the time complexity of binary search is $O(\log n)$. | ||
* Explain the difference between LinkedList and Array. | ||
* Explain the hash table and estimate the time complexity. | ||
* Explain graph search algorithms and explain the difference between BFS (Breadth First Search) and DFS (Depth First Search). | ||
|
||
|
||
## Exercises | ||
### [Word Pattern](https://leetcode.com/problems/word-pattern/description/) | ||
Given a pattern `p` consisting of lowercase English letters and a string `s` separated by spaces, determine if `s` follows the pattern `p`. For example, if `p = "abba"` and `s = "dog cat cat dog"`, then `s` follows the pattern `p`, but if `p="abba"` and `s="dog cat cat fish"`, then `s` does not follow `p`. | ||
|
||
**:beginner: Checkpoint** | ||
#### Step1: Think about how to split the string `s` by spaces. | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* In each language, there should be standard libraries or functions provided for string manipulation. | ||
* Use web search or ChatGPT, searching for "split string by spaces" or similar queries. | ||
</details> | ||
|
||
#### Step2: Consider how to manage which part of `s` corresponds to each character of the pattern `p`. | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* For example, in Example 1, the words in `s` corresponding to each character of `p` are `a => dog`, `b => cat`. | ||
* To manage such correspondences, using a dictionary or hash table would be beneficial. | ||
* For instance, in Python, you can manage the words in `s` that correspond to each character of `p` using a `dict`. | ||
* Also use web search or ChatGPT, looking up "Python dictionary" or similar queries. | ||
</details> | ||
|
||
|
||
### [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/) | ||
Given an array of n integers where each value is in the range [1, n], return all integers in the range [1, n] that do not appear in the array. | ||
|
||
**:beginner: Checkpoint** | ||
|
||
#### Step1: Solve with O(n^2)-time and O(1)-space | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* You can solve it using a simple double loop, achieving O(n^2)-time and O(1)-space. | ||
</details> | ||
|
||
#### Step2: Solve with O(n)-time and O(n)-space | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* By preparing an array to record whether an element has appeared in the array nums, you can solve it in O(n)-time and O(n)-space. | ||
</details> | ||
|
||
#### Advanced: Solve with O(n)-time and O(1)-space (bonus) | ||
Is it possible to solve it in O(1)-space, excluding the input and return? | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* Upon deeper consideration, it is possible to solve it in O(n)-time and O(1)-space. | ||
* This will be covered in the explanation, so give it a try. | ||
</details> | ||
|
||
|
||
### [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/description) | ||
Given two singly linked lists, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return `null`. | ||
|
||
**:beginner: Checkpoint** | ||
|
||
#### Step1: Solve with O(n)-time and O(n)-space | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* By using a Hash Table to record nodes, you can solve it in O(n)-time and O(n)-space. | ||
</details> | ||
|
||
#### Step2: Solve with O(n)-time and O(1)-space | ||
Is it possible to solve it in O(1)-space, excluding the input and return? | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* By comparing the lengths of the two lists and adjusting the longer list to match the length of the shorter one, you can solve it in O(n)-time and O(1)-space. | ||
* This will be covered in the explanation. | ||
</details> | ||
|
||
#### Advanced: Solve using two pointers (bonus) | ||
<details> | ||
<summary>Hint</summary> | ||
|
||
* Start a pointer at the tail of one list and proceed to the head, reducing the problem to Floyd's Linked List Cycle Finding Algorithm. | ||
</details> | ||
|
||
|
||
### [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) (optional) | ||
|
||
### [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) (optional) | ||
|
||
### [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) (optional) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Extra1: アルゴリズムとデータ構造 | ||
Extra1では、基本的なアルゴリズムとデータ構造を学んだ後、LeetCodeの問題を用いて演習を行います。解説の前に問題を解いておくことをお勧めします。 | ||
|
||
## 教材 | ||
|
||
**:book: Reference** | ||
|
||
* (JA) [現役シリコンバレーエンジニアが教えるアルゴリズム・データ構造・コーディングテスト入門](https://mercari.udemy.com/course/python-algo/) | ||
|
||
* (EN) [Python Data Structures & Algorithms + LEETCODE Exercises](https://mercari.udemy.com/course/data-structures-algorithms-python/) | ||
|
||
**:beginner: point** | ||
* まず以下の言葉について調べ、どのようなものであるか説明できるようになりましょう | ||
* 時間計算量と空間計算量 | ||
* ビッグオー記法 | ||
* 連想配列 | ||
* 以下の基本的なアルゴリズムについて Udemy 等で勉強し、解説できるようになりましょう。 | ||
* バイナリサーチとは、どのようなアルゴリズムですか?バイナリサーチの計算量が $O(\log n)$ である理由を説明してください。 | ||
* LinkedList と Array の違いについて説明してください。 | ||
* ハッシュテーブルを説明し、計算量を見積もってください。 | ||
* グラフ探索アルゴリズムについて説明し、BFS (Breadth First Search) や DFS (Depth First Search) の使い分けについて説明してください。 | ||
|
||
## 演習 | ||
### [Word Pattern](https://leetcode.com/problems/word-pattern/description/) | ||
英小文字からなるパターン `p` と、空白区切りの文字列 `s` が与えられるので、`s` が `p` に従うかどうかを判定してください。 例えば、`p = "abba"`, `s = "dog cat cat dog"` の場合 `s` は `p` に従い、`p="abba"`, `s="dog cat cat fish"` の場合 `s` は `p` に従いません。 | ||
|
||
**:beginner: checkpoint** | ||
#### Step1: 文字列 `s` を空白で区切る方法を考えてみましょう。 | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 各言語では、文字列操作のためのライブラリや関数などが標準で提供されているはずです | ||
* Web 検索や ChatGPT を駆使して、"文字列 空白区切り" などで検索してみましょう | ||
</details> | ||
|
||
#### Step2: パターン `p` の書く文字が、`s` のどの部分に対応するかを管理する方法を考えてみましょう。 | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 例えば、Example 1 の場合、`p` の各文字に対応する `s` 内の単語は、`a => dog`, `b => cat` です | ||
* このような対応を管理するために、辞書やハッシュテーブルを使うと良いでしょう | ||
* 例えば、Python では、`dict` を使って、`p` の各文字に対応する `s` 内の単語を管理できます | ||
* こちらも、Web 検索や ChatGPT を駆使して、"Python 辞書" などで検索してみましょう | ||
</details> | ||
|
||
|
||
### [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/) | ||
n 個の整数からなる配列 nums が与えられ、nums[i] は [1, n] の範囲にあります。この配列に現れない [1, n] の範囲のすべての整数を返してください。 | ||
|
||
**:beginner: checkpoint** | ||
|
||
#### Step1: O(n^2)-time and O(1)-space で解く | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* シンプルなな 2 重ループを用いて、O(n^2)-time and O(1)-space で解けます | ||
</details> | ||
|
||
#### Step2: O(n)-time and O(n)-space で解く | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 配列 nums 内に要素が出現したかどうかを記録するための配列を用意することで、O(n)-time and O(n)-space で解けます | ||
</details> | ||
|
||
#### 発展: O(n)-time and O(1)-space で解く (おまけ) | ||
入力と返り値を除いて、O(1)-space で解くことは可能ですか? | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 深く考察をすると、O(n)-time and O(1)-space で解けることがわかります | ||
* 解説で扱う予定なので、挑戦してみてください | ||
</details> | ||
|
||
|
||
### [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/description) | ||
2 つの単方向 Linked List が与えられるので、2 つのリストが交差するノードを返してください。交差しない場合は、`null` を返してください。 | ||
|
||
**:beginner: checkpoint** | ||
|
||
#### Step1: O(n)-time and O(n)-space で解く | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* Hash Table を使ってノードを記録することで、O(n)-time and O(n)-space で解けます | ||
</details> | ||
|
||
#### Step2: O(n)-time and O(1)-space で解く | ||
入力と返り値を除いて、O(1)-space で解くことは可能ですか? | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 2つのリストの長さを比較して、長いリストを短いリストと同じ長さにすることで、O(n)-time and O(1)-space で解けます | ||
* 解説で扱う予定です | ||
</details> | ||
|
||
#### 発展: two pointers を使って解く方法 (おまけ) | ||
<details> | ||
<summary>ヒント</summary> | ||
|
||
* 片方の tail から head にポインタをはり、Floyd's Linked List Cycle Finding Algorithm に帰着する | ||
</details> | ||
|
||
|
||
### [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) (optinal) | ||
|
||
### [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) (optional) | ||
|
||
### [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) (optional) |