diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json index 81383b5..09ff637 100644 --- a/.obsidian/appearance.json +++ b/.obsidian/appearance.json @@ -5,7 +5,7 @@ "interfaceFontFamily": "IBM Plex Sans", "textFontFamily": "IBM Plex Mono,monospace", "monospaceFontFamily": "IBM Plex Mono", - "baseFontSize": 18, + "baseFontSize": 19, "enabledCssSnippets": [ "expander" ] diff --git a/CS notes/CS-index.md b/CS notes/CS-index.md index 2da143d..d726816 100644 --- a/CS notes/CS-index.md +++ b/CS notes/CS-index.md @@ -14,7 +14,7 @@ This is where I store notes about CS ___ ## Lists -- Needs more understandings #CS_list_need_understanding +#### Needs more understandings #CS_list_need_understanding ```expander tag:#CS_list_need_understanding - [ ] [[$filename]] @@ -24,7 +24,7 @@ tag:#CS_list_need_understanding -- Needs practicing #CS_list_need_practicing +#### Needs practicing #CS_list_need_practicing ```expander tag:#CS_list_need_practicing - [ ] [[$filename]] @@ -72,9 +72,10 @@ tag:#hackerearch -___ -### Data Structure + +___ +## Data Structure #### Coding problems ```expander tag:#DS tag:#coding_problem -tag:#template_remove_me @@ -92,6 +93,7 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me - [[Leetcode Ransom-Note]] - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] +- [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Two-Sum]] - [[Leetcode Valid-Anagram]] @@ -105,7 +107,6 @@ tag:#DS tag:#CS_analysis -tag:#template_remove_me - [[$filename]] ``` -- [[Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O]] - [[cpp_Range_based_for_loop]] - [[cpp_std_multiset]] - [[cpp_std_unordered_map]] @@ -113,7 +114,7 @@ tag:#DS tag:#CS_analysis -tag:#template_remove_me ___ -### Algorithm +## Algorithm #### Coding problems ```expander tag:#algorithm tag:#coding_problem -tag:#template_remove_me @@ -129,6 +130,7 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] - [[Leetcode Ransom-Note]] +- [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Two-Sum]] - [[Leetcode Valid-Anagram]] @@ -145,7 +147,6 @@ tag:#algorithm tag:#CS_analysis -tag:#template_remove_me ``` - [[Binary Search Algorithm]] -- [[Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O]] - [[cpp_std_sort]] - [[Floyd's Cycle Finding Algorithm]] - [[Kadane's Algorithm]] diff --git a/CS notes/pages/Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O.md b/CS notes/pages/Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O.md deleted file mode 100644 index b3286bb..0000000 --- a/CS notes/pages/Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O.md +++ /dev/null @@ -1,79 +0,0 @@ -# Binary Search Algorithm - -#### 2022-06-13 15:46 - -___ -##### Algorithms: -#algorithm #binary_search -##### Data structures: -#DS #array #vector #set #multiset -##### Difficulty: -#CS_analysis #difficulty-easy -##### Related problems: -```expander -tag:#coding_problem tag:#binary_search -tag:#template_remove_me -``` - -- [[Leetcode Search-a-2D-Matrix]] - - -##### Links: -- [g4g for manual implementation](https://www.geeksforgeeks.org/binary-search/) -- [cppreference, find](https://en.cppreference.com/w/cpp/container/set/find) -___ - -### How to implement Binary search? - -#### a: Use cpp's library -Use cpp's set's [find](https://en.cppreference.com/w/cpp/container/set/find) -or [equal_range](https://en.cppreference.com/w/cpp/container/multiset/equal_range) - -#### b: Manual -1. Use a while loop: -[[Leetcode Search-a-2D-Matrix#Solution]] - -2. Use recursion: -from g4g: -```cpp -// C++ program to implement recursive Binary Search -#include -using namespace std; - -// A recursive binary search function. It returns -// location of x in given array arr[l..r] is present, -// otherwise -1 -int binarySearch(int arr[], int l, int r, int x) { - if (r >= l) { - int mid = l + (r - l) / 2; - - // If the element is present at the middle - // itself - if (arr[mid] == x) - return mid; - - // If element is smaller than mid, then - // it can only be present in left subarray - if (arr[mid] > x) - return binarySearch(arr, l, mid - 1, x); - - // Else the element can only be present - // in right subarray - return binarySearch(arr, mid + 1, r, x); - } - - // We reach here when element is not - // present in array - return -1; -} - -int main(void) { - int arr[] = {2, 3, 4, 10, 40}; - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? cout << "Element is not present in array" - : cout << "Element is present at index " << result; - return 0; -} - -``` \ No newline at end of file diff --git a/CS notes/pages/Leetcode Reverse-Linked-List.md b/CS notes/pages/Leetcode Reverse-Linked-List.md index e6fc48c..f76640b 100644 --- a/CS notes/pages/Leetcode Reverse-Linked-List.md +++ b/CS notes/pages/Leetcode Reverse-Linked-List.md @@ -15,6 +15,13 @@ ```expander tag:#linked_list ``` + +- [[Floyd's Cycle Finding Algorithm]] +- [[Leetcode Linked-List-Cycle]] +- [[Leetcode Merge-Two-Sorted-Lists]] +- [[Leetcode Remove-Linked-List-Elements]] +- [[Two pointers approach]] + @@ -59,6 +66,12 @@ I thought a slow O(n ^ 2) hybrid solution, while there are better algorithms, us The in place insert is easier to understand, and simple to implement, using a very clever trick. +> [!summary] My thoughts on the recursion algorithm +> #### How was that implemented? +> The recursion algorithm takes the advantage of the fact that when you change the node's next properties or nodes after that, the node before still points to the same node, so when every node after **tmp** is reversed, simply move **tmp** after **tmp->next**, which now points to the tail of reversed list +> #### Why return the last element of the original list? +> It returns the last element in the original list to make sure you are always returning the head of the reversed array, since for any reversed list, the last one comes first. + ### Solution I've referred to this guy: https://leetcode.com/problems/reverse-linked-list/discuss/58130/C%2B%2B-Iterative-and-Recursive @@ -86,6 +99,7 @@ public: ``` Recursion: +==TODO== Needs revision! ```cpp class Solution { public: @@ -95,11 +109,16 @@ public: return head; } - // node is the end of linked list, which stays the same and untouched + // node is the end of the initial linked list, which stays the same ListNode *node = reverseList(head->next); - // change head->next + + // move head after head->next + // notice that the node before head still points to head! head->next->next = head; + + // since the order is reversed, head->next now points to the tail of the linked list. This is the genius part of this algorithm head->next = NULL; + return node; } }; diff --git a/_templates/OJ Problem Notes.md b/_templates/OJ Problem Notes.md index ebad482..f4d513c 100644 --- a/_templates/OJ Problem Notes.md +++ b/_templates/OJ Problem Notes.md @@ -7,7 +7,7 @@ > ##### Data structures: > #DS > ##### Difficulty: -> #coding_problem #difficulty-easy +> #coding_problem #difficulty- > ##### Additional tags: > # # > ##### Revisions: @@ -27,8 +27,6 @@ ___ ### Problem #### Examples -```markdown -``` #### Constraints