vault backup: 2022-09-03 15:41:35

This commit is contained in:
juan 2022-09-03 15:41:36 +08:00
parent 80a409b0c1
commit 2f912701ba
97 changed files with 2305 additions and 929 deletions

View file

@ -1,5 +1,5 @@
{
"theme": "obsidian",
"theme": "moonstone",
"translucency": true,
"cssTheme": "Atom",
"interfaceFontFamily": "IBM Plex Sans",

3
.obsidian/backlink.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"backlinkInDocument": false
}

View file

@ -39,6 +39,6 @@
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 0.7798887840302782,
"scale": 0.9328381822372834,
"close": true
}

View file

@ -9,17 +9,21 @@
# My notebook
___
---
## Table of contents
### Computer science notes
- [[OJ-index]]
- [[CTF-index]]
### Misc
- [[Wiki-Index]]
___
---
## Naming conventions
- When using titles, capitalize the first character

View file

@ -11,31 +11,66 @@
This is where I store notes about CS
___
---
## Lists
#### TODO
```query
#TODO
```
#### Needs more understandings
```query
#CS_list_need_understanding
```
#### Needs practicing
```query
#CS_list_need_practicing
```
## Websites
#### [leetcode.com](https://leetcode.com)
```query
#leetcode
```
#### [hackerearth.com](https://www.hackerearth.com/)
```query
#hackerearth
```
## Data Structure
#DS
#### Coding problems
#coding_problem
```query
#coding_problem and #DS
```
#### Analysis
#CS_analysis
```query
#CS_analysis and #DS
```
## Algorithm
#algorithm
#### Coding problems
#coding_problem
```query
tag:#coding_problem tag:#algorithm
```
#### Analysis
#CS_analysis
```query
#CS_analysis #algorithm
```

View file

@ -2,22 +2,33 @@
#### 2022-06-13 15:46
___
---
##### Algorithms:
#algorithm #binary_search
##### Data structures:
#array #vector #set #multiset
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### 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)
@ -33,10 +44,11 @@ or [equal_range](https://en.cppreference.com/w/cpp/container/multiset/equal_rang
> This happens if 1 is not subtracted
1. Use a while loop:
[[Leetcode Search-a-2D-Matrix#Solution]]
[[Leetcode Search-a-2D-Matrix#Solution]]
2. Use recursion:
from g4g:
from g4g:
```cpp
// C++ program to implement recursive Binary Search
#include <bits/stdc++.h>

View file

@ -3,15 +3,21 @@
#### 2022-07-22 15:11
> ##### Algorithms:
> #algorithm #bit_manipulation
> ##### Data Structure:
> #DS #bitset
> ##### Difficulty:
> #CS_analysis #difficulty-easy
> ##### Additional tags:
>
> #algorithm #bit_manipulation
>
> ##### Data Structure:
>
> #DS #bitset
>
> ##### Difficulty:
>
> #CS_analysis #difficulty-easy
>
> ##### Additional tags:
##### Related problems:
### What is Bit-Manipulation count true trick?
(n & (n - 1))

View file

@ -3,20 +3,29 @@
#### 2022-07-08 11:19
> ##### Algorithms:
> #algorithm #BFS
> ##### Data structures:
> #DS #binary_tree
> ##### Difficulty:
> #CS_analysis #difficulty-
> ##### Additional tags:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #CS_analysis #difficulty-
>
> ##### Additional tags:
##### Related problems:
##### Links:
- [cppreference]()
___
---
### What is Breadth First Search?
means BFS
### How does Breadth First Search work?

View file

@ -2,19 +2,30 @@
#### 2022-06-14 22:10
___
---
##### Algorithms:
#algorithm #Floyd_s_cycle_finding_algorithm
##### Data structures:
#linked_list
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### Links:
- [g4g](https://www.geeksforgeeks.org/floyds-cycle-finding-algorithm/)
___
---
### What is Floyd's Cycle Finding Algorithm?
[Floyds cycle finding algorithm](https://www.geeksforgeeks.org/detect-loop-in-a-linked-list/) or Hare-Tortoise algorithm is a **pointer algorithm** that uses only **two pointers**, moving through the sequence at different speeds.
It uses two pointers one moving twice as fast as the other one. The faster one is called the faster pointer and the other one is called the slow pointer.
@ -22,6 +33,7 @@ It uses two pointers one moving twice as fast as the other one. The faster one i
### How does it work?
#### Part 1. **Verify** if there is a loop
While traversing the linked list one of these things will occur-
- The Fast pointer may reach the end (NULL) this shows that there is no loop n the linked list.
@ -33,6 +45,7 @@ While traversing the linked list one of these things will occur-
- Move the slow pointer by one position.
- Move the fast pointer by **two** positions.
- If both pointers meet at some point then a loop exists and if the fast pointer meets the end position then no loop exists.
#### Part 2. **Locating the start** of the loop
Let us consider an example:
@ -49,39 +62,41 @@ Let us consider an example:
- So before both the pointer meets-
> The slow pointer has traveled **X + Y + s * C** distance, where s is any positive constant number.
> The slow pointer has traveled **X + Y + s \* C** distance, where s is any positive constant number.
>
> The fast pointer has traveled **X + Y + f * C** distance, where f is any positive constant number.
> The fast pointer has traveled **X + Y + f \* C** distance, where f is any positive constant number.
- Since the fast pointer is moving twice as fast as the slow pointer, we can say that the fast pointer covered twice the distance the slow pointer covered. Therefore-                  
- Since the fast pointer is moving twice as fast as the slow pointer, we can say that the fast pointer covered twice the distance the slow pointer covered. Therefore-
> X + Y + f * C = 2 * (X + Y + s * C)
> X + Y + f _ C = 2 _ (X + Y + s \* C)
>
> X + Y = f * C 2 * s * C
> X + Y = f _ C 2 _ s \* C
>
> We can say that,
>
> f * C 2 * s * C = (some integer) * C
> f _ C 2 _ s _ C = (some integer) _ C
>
>                          = K * C
> = K \* C
>
> Thus,
>
> X + Y = K * C       ** ( 1 )**
> X + Y = K \* C       ** ( 1 )**
>
> X = K * C Y       ** ( 2 )**
> X = K \* C Y       ** ( 2 )**
>
> Where K is some positive constant.    
> Where K is some positive constant.
- Now if ==reset the slow pointer to the head==(starting position) and move both fast and slow pointer ==by one unit at a time==, one can observe from 1st and 2nd equation that **both of them will meet** after traveling X distance at the starting of the loop because after resetting the slow pointer and moving it X distance, at the same time from loop meeting point the fast pointer will also travel K * C Y distance(because it already has traveled Y distance).
- Because X = K * C - Y, while fast pointer was at Y from start of loop, running X will place it at the start of loop, meeting the slow pointer.
- Now if ==reset the slow pointer to the head==(starting position) and move both fast and slow pointer ==by one unit at a time==, one can observe from 1st and 2nd equation that **both of them will meet** after traveling X distance at the starting of the loop because after resetting the slow pointer and moving it X distance, at the same time from loop meeting point the fast pointer will also travel K \* C Y distance(because it already has traveled Y distance).
- Because X = K \* C - Y, while fast pointer was at Y from start of loop, running X will place it at the start of loop, meeting the slow pointer.
**Pseudo-code**
- Place slow pointer at the head.
- Move one step at a time until they met.
- The start of the loop is where they met
#### Example
```cpp
// C++ program to implement
// the above approach
@ -172,6 +187,7 @@ int main() {
}
```
### When to use?
This algorithm is used ==to find a loop in a linked list==, Also it ==can locate where the loop starts.==

View file

@ -3,19 +3,31 @@
#### 2022-06-09
---
##### Data structures:
#array
##### Algorithms:
#algorithm #Kadane_s_algorithm
##### Difficulty:
#CS_analysis #difficulty-easy
##### Time complexity:
O(n)
##### Related problems:
##### Resources:
- [Explainer article](https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d)
---
### What is Kadane's Algorithm?
It's a kind of dynamic programming. You calculate A[n] by calculating A[n - 1], which makes it O(n)
@ -27,6 +39,7 @@ It's a kind of dynamic programming. You calculate A[n] by calculating A[n - 1],
### When to use it?
According my analyze [[Leetcode Best-Time-To-Buy-And-Sell-Stock#Thoughts| here]], we should use it when these conditions are met:
- You want to find the value of the highest peak or lowest valley
- The direction you search is mono-directional
- The current value can be obtained from or, is related to the value before this one.

View file

@ -3,20 +3,33 @@
#### 2022-07-17 03:15
> ##### Algorithms:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS #vector_2d
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/01-matrix/)
___
---
### Problem
Given an `m x n` binary matrix `mat`, return _the distance of the nearest_ `0` _for each cell_.
@ -61,6 +74,7 @@ The distance between two adjacent cells is `1`.
#### Why not DFS
I tried with DFS, but
1. it is not suitable for finding smallest distance
2. and is easy to go into a infinite loop.
3. Also, it is hard to determine whether to revisit (update)
@ -73,6 +87,7 @@ Start searching from 0s, because the search direction matters.
pseudo code:
- Initialization stage:
- add every 0 to queue
- make every 1 a infinite large number, (10001 this case)

View file

@ -3,16 +3,25 @@
#### 2022-07-23 15:27
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode #math
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)
___
---
### Problem
You are given an array of **unique** integers `salary` where `salary[i]` is the salary of the `ith` employee.

View file

@ -3,19 +3,30 @@
#### 2022-06-11
---
##### Data structures:
#DS #array
##### Algorithms:
#algorithm #Kadane_s_algorithm
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
- [Kadane's Algo solution](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/554875)
___
---
### Problem
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
@ -23,6 +34,7 @@ You want to maximize your profit by choosing a **single day** to buy one stock a
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
#### Examples
Example 1:
```
@ -49,16 +61,18 @@ Explanation: In this case, no transactions are done and the max profit = 0.
### Thoughts
Firstly I thought of brute forcing, which is O(n * (n-1))
Firstly I thought of brute forcing, which is O(n \* (n-1))
Then, I came up with dynamic programming, but this is still not so optimized
Lastly, from [here](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/554875) I know We can use Kadane's algo.
> [!tip]
> In Kadane's algorithm:
>
> - buyprice = min(buyprice, price[i]) // Achieve best min price
> - profit = max(profit, price[i] - buyprice) // find best profit **==so far==**
To explain that, in each iteration, there are two cases:
- if the current price is the lowest, set the buyprice to the lowest one
- if the current price minus the `buyprice` is bigger tham profit, record that to profit.
@ -67,6 +81,7 @@ So, the magic part happens **at setting the buyprice**, because the best profit
### Solution
Time O(2n) Space O(n) solution (inefficient reverse kadane's algorithm)
```cpp
class Solution {
public:
@ -94,6 +109,7 @@ public:
```
Kadane's algorithm, Time O(n) Space O(1)
```cpp
class Solution {
public:

View file

@ -3,20 +3,33 @@
#### 2022-07-09 09:34
> ##### Algorithms:
>
> #algorithm #binary_search
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_practicing
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-search/)
___
---
### Problem
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
@ -50,9 +63,11 @@ You must write an algorithm with `O(log n)` runtime complexity.
> This is a #binary_search problem.
Key takeout:
```
int r = nums.size() - 1;
```
make sure r is never OOB (l == r && r = array.size())
### Solution
@ -60,6 +75,7 @@ make sure r is never OOB (l == r && r = array.size())
==#TODO: write in recursion==
iteration
```cpp
class Solution {
public:

View file

@ -3,24 +3,39 @@
#### 2022-07-04 15:42
> ##### Algorithms:
>
> #algorithm #DFS #DFS_inorder
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-tree-inorder-traversal/)
___
---
### Problem
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
@ -39,6 +54,7 @@ Given the `root` of a binary tree, return _the inorder traversal of its nodes' v
**Output:** [1]
#### Constraints
- The number of nodes in the tree is in the range `[0, 100]`.
- `-100 <= Node.val <= 100`
@ -48,9 +64,11 @@ Given the `root` of a binary tree, return _the inorder traversal of its nodes' v
> This is a #DFS traversal problem
Many of them are same to [[Leetcode Binary-Tree-Preorder-Traversal]]
### Solution
Recursion
```cpp
/**
* Definition for a binary tree node.
@ -86,6 +104,7 @@ public:
```
Iteration
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,24 +3,39 @@
#### 2022-07-05 09:09
> ##### Algorithms:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-tree-level-order-traversal/)
___
---
### Problem
Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
@ -39,8 +54,10 @@ Given the `root` of a binary tree, return _the level order traversal of its node
**Output:** []
#### Constraints
- The number of nodes in the tree is in the range `[0, 2000]`.
- `-1000 <= Node.val <= 1000`
### Thoughts
> [!summary]
@ -54,6 +71,7 @@ vec.push_back({});
vec.back().push_back(5);
// [[ 5 ]]
```
### Solution
```cpp

View file

@ -3,23 +3,39 @@
#### 2022-07-04 21:31
> ##### Algorithms:
>
> #algorithm #DFS #DFS_postorder
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_practicing
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-tree-postorder-traversal/)
___
---
### Problem
https://leetcode.com/problems/binary-tree-postorder-traversal/
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg)
@ -47,9 +63,11 @@ https://leetcode.com/problems/binary-tree-postorder-traversal/
Same as [[Leetcode Binary-Tree-Inorder-Traversal]] and [[Leetcode Binary-Tree-Preorder-Traversal]]
== TODO: write iteration and another algo #TODO ==
### Solution
Recursion
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,24 +3,39 @@
#### 2022-07-04 14:51
> ##### Algorithms:
>
> #algorithm #DFS #DFS_preorder #Morris_traversal
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-tree-preorder-traversal/)
___
---
### Problem
Given the `root` of a binary tree, return _the preorder traversal of its nodes' values_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
@ -37,9 +52,12 @@ Given the `root` of a binary tree, return _the preorder traversal of its nodes'
**Input:** root = [1]
**Output:** [1]
#### Constraints
- The number of nodes in the tree is in the range `[0, 100]`.
- `-100 <= Node.val <= 100`
### Thoughts
> [!summary]
@ -59,7 +77,9 @@ And remember to push **right subtree** first, so that the left one will be trave
The Morris traversal needs more understandings.
### Solution
Iteration
```cpp
/**
* Definition for a binary tree node.
@ -100,7 +120,9 @@ public:
};
```
Recursion, using private funcs:
```cpp
/**
* Definition for a binary tree node.
@ -137,6 +159,7 @@ private:
```
Recursion:
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,18 +3,29 @@
#### 2022-07-27 11:38
> ##### Algorithms:
>
> #algorithm #math
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
___
---
### Problem
You are given an array `coordinates`, `coordinates[i] = [x, y]`, where `[x, y]` represents the coordinate of a point. Check if these points make a straight line in the XY plane.
@ -56,6 +67,7 @@ This is also true in this problem.
We want to calculate the k for every consecutive points, if they are the same, they are on one straight line.
By starting from the first, using them as the base k:
```
k = (dx0 - dx1) / (dy0 - dy1) = (dx - dx') / (dy - dy')
|
@ -68,6 +80,7 @@ Which is the cross product of these two points.
### Solution
==#TODO: Fill this out==
```cpp
WIP
```

View file

@ -3,18 +3,29 @@
#### 2022-07-20 14:37
> ##### Algorithms:
>
> #algorithm #dynamic_programming
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/climbing-stairs/submissions/)
___
---
### Problem
You are climbing a staircase. It takes `n` steps to reach the top.
@ -28,6 +39,7 @@ Each time you can either climb `1` or `2` steps. In how many distinct ways can y
**Input:** n = 2
**Output:** 2
**Explanation:** There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
@ -36,6 +48,7 @@ Each time you can either climb `1` or `2` steps. In how many distinct ways can y
**Input:** n = 3
**Output:** 3
**Explanation:** There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
@ -54,6 +67,7 @@ I had come up with an recursive solution, with a little bit of optimizations.
#### First iteration:
Base case:
- n is 0, 1, 2:
return n
@ -65,6 +79,7 @@ This is a brute force-y solution, because there are too much combinations, and w
#### Second iteration:
Base case:
- n = 1, 2, **3**
Return n
@ -85,6 +100,7 @@ and when n == 0, it should return 1.
### Solution
Optimized version:
```cpp
class Solution {
public:

View file

@ -3,19 +3,30 @@
#### 2022-07-19 20:09
> ##### Algorithms:
>
> #algorithm #backtrack
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/combinations/)
- [Explanation](https://leetcode.com/problems/combinations/discuss/844096/Backtracking-cheatsheet-%2B-simple-solution)
___
---
### Problem
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers out of the range_ `[1, n]`.
@ -69,6 +80,7 @@ And when it's done, it gets poped out and try another solution, and backtrack.
### Solution
Backtracking
```cpp
class Solution {
void backtrack(vector<vector<int>> &ans, vector<int> &combs, int n, int nex,

View file

@ -3,21 +3,32 @@
#### 2022-07-23 15:09
> ##### Algorithms:
>
> #algorithm #math
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
___
---
### Problem
Given two non-negative integers `low` and `high`. Return the _count of odd numbers between_ `low` _and_ `high` _(inclusive)_.
Given two non-negative integers `low` and `high`. Return the _count of odd numbers between_ `low` _and_ `high` *(inclusive)*.
#### Examples
@ -35,7 +46,7 @@ Given two non-negative integers `low` and `high`. Return the _count of odd numbe
#### Constraints
- `0 <= low <= high <= 10^9`
- `0 <= low <= high <= 10^9`
### Thoughts

View file

@ -3,18 +3,28 @@
#### 2022-09-01 14:44
> ##### Data structures:
>
> #DS #array #linked_list
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_practicing
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/design-hashmap/)
- [Explanation](https://leetcode.com/problems/design-hashmap/discuss/1097755/JS-Python-Java-C%2B%2B-or-(Updated)-Hash-and-Array-Solutions-w-Explanation)
___
- [Explanation](<https://leetcode.com/problems/design-hashmap/discuss/1097755/JS-Python-Java-C%2B%2B-or-(Updated)-Hash-and-Array-Solutions-w-Explanation>)
---
### Problem
Design a HashMap without using any built-in hash table libraries.

View file

@ -3,20 +3,33 @@
#### 2022-07-09 09:52
> ##### Algorithms:
>
> #algorithm #binary_search
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/first-bad-version/)
___
---
### Problem
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
@ -51,7 +64,7 @@ Then 4 is the first bad version.
> [!summary]
> This is a #binary_search problem
Note that [[Leetcode First-Bad-Version#Constraints]], n can be 2**31, which means there might be integer overflow.
Note that [[Leetcode First-Bad-Version#Constraints]], n can be 2\*\*31, which means there might be integer overflow.
To address that, according to [[Binary Search Algorithm#How to implement Binary search]], use `mid = l + (r - l) / 2`
@ -63,6 +76,7 @@ Later I realized that by the definition of Bi-search, left boundary will converg
### Solution
Second version, 0ms
```cpp
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
@ -94,6 +108,7 @@ public:
```
First iteration, 4ms
```cpp
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);

View file

@ -3,22 +3,35 @@
#### 2022-06-14 13:10
---
##### Algorithms:
#algorithm
##### Data structures:
#DS #string #array #hash_table
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/first-unique-character-in-a-string/)
___
---
### Problem
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`.
#### Examples
**Example 1:**
```markdown
**Input:** s = "leetcode"
**Output:** 0
@ -39,6 +52,7 @@ Given a string `s`, _find the first non-repeating character in it and return its
```
#### Constraints
- `1 <= s.length <= 105`
- `s` consists of only lowercase English letters.
@ -46,6 +60,7 @@ Given a string `s`, _find the first non-repeating character in it and return its
Really, really simple hash map problem.
Shouldn't have taken so much time.
> [!summary]
> Don't overlook simple problems! #tip

View file

@ -3,20 +3,33 @@
#### 2022-07-15 09:01
> ##### Algorithms:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/flood-fill/)
___
---
### Problem
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image.
@ -68,12 +81,14 @@ Initially, I wanted to use a hash map to record cells that are visited,
but the this takes up extra space.
Then I found out that I can use colors:
- if image[r][c] == color, this means
- the cell is no need to correct(color == origcolor),
- or this has been corrected
so I don't have to go over again.
There are checks in the loop:
- check the color is not visited, as shown above
- check the coord is not OOB
- check the cell is equal to origColor, to only fill same origColor.

View file

@ -3,18 +3,29 @@
#### 2022-07-26 09:12
> ##### Algorithms:
>
> #algorithm #Floyd_s_cycle_finding_algorithm
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/happy-number/)
___
---
### Problem
Write an algorithm to determine if a number `n` is happy.

View file

@ -3,19 +3,30 @@
#### 2022-07-20 22:21
> ##### Algorithms:
>
> #algorithm #dynamic_programming
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/house-robber/)
- [Tutorial and explanation on DP](https://leetcode.com/problems/house-robber/discuss/156523/From-good-to-great.-How-to-approach-most-of-DP-problems.)
___
---
### Problem
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
@ -59,6 +70,7 @@ According to [This tuturial](https://leetcode.com/problems/house-robber/discuss/
The robbing problem can be simplified as follows:
> Whether to rob or not?
>
> - if rob, profit = nums[n] + rob(nums, n - 2)
> - else, profit = rob(nums, n - 1)

View file

@ -3,19 +3,31 @@
#### 2022-07-03 09:52
> ##### Data structures:
>
> #DS #stack #queue
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/implement-queue-using-stacks/submissions/)
___
---
### Problem
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (`push`, `peek`, `pop`, and `empty`).
Implement the `MyQueue` class:
@ -31,11 +43,14 @@ Implement the `MyQueue` class:
- Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
#### Examples
**Input**
```
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
```
**Output**
[null, null, null, 1, 1, false]
@ -48,6 +63,7 @@ myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
#### Constraints
**Constraints:**
- `1 <= x <= 9`

View file

@ -3,20 +3,33 @@
#### 2022-07-07 08:50
> ##### Algorithms:
>
> #algorithm #recursion #DFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/insert-into-a-binary-search-tree/)
___
---
### Problem
You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
@ -46,7 +59,7 @@ Notice that there may exist multiple valid ways for the insertion, as long as th
#### Constraints
- The number of nodes in the tree will be in the range `[0, 104]`.
- The number of nodes in the tree will be in the range `[0, 104]`.
- `-108 <= Node.val <= 108`
- All the values `Node.val` are **unique**.
- `-108 <= val <= 108`
@ -68,10 +81,12 @@ DFS-like solution is a simple recursion problem, using a helper function.
root is null, simple return a new node.
##### Base cases:
- left side is empty, and val < root->val: place it in
- right side is empty, and val > root->val: place it in
##### Pseudocode:
- check for base cases
- if val < root->val, insert(root->left, val)
- vice, versa.

View file

@ -1,23 +1,35 @@
# Leetcode Intersection-of-Two-Arrays-II
#### 2022-06-11
##### First revision 2022-06-27
---
##### Data structures:
#DS #unordered_map
##### Algorithms:
#algorithm #two_pointers #sort
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/intersection-of-two-arrays-ii/)
- [Solution 2(two pointers method)](https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/846181/C%2B%2B-Solutions-or-1-%3A-Map-or-2-%3A-Two-Pointer-with-sort)
- [Solution 3 and 4](https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82243/Solution-to-3rd-follow-up-question)
___
---
### Problem
Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Follow up:**
@ -27,6 +39,7 @@ Given two integer arrays `nums1` and `nums2`, return _an array of their intersec
- What if elements of `nums2` are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
#### Examples
**Example 1:**
```
@ -59,7 +72,9 @@ For the original problem, I thought up an O(m + n) algo, that uses C++'s [[cpp_s
> [!tip] Use [[cpp_std_sort]] to sort anything. #tip
### Solution
Unordered map way O(m + n)
```cpp
class Solution {
public:

View file

@ -3,20 +3,33 @@
#### 2022-07-06 13:33
> ##### Algorithms:
>
> #algorithm #DFS #recursion
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/invert-binary-tree/)
___
---
### Problem
Given the `root` of a binary tree, invert the tree, and return _its root_.
@ -59,8 +72,9 @@ Given the `root` of a binary tree, invert the tree, and return _its root_.
> This is a #DFS like recursion problem.
Very simple, think of base cases:
- the node is void, skip.
And the flow is following
And the flow is following
- Catch base case
- Invert sub-trees first

View file

@ -3,18 +3,29 @@
#### 2022-07-26 08:39
> ##### Algorithms:
>
> #algorithm #greedy #math
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/largest-perimeter-triangle/)
___
---
### Problem
Given an integer array `nums`, return _the largest perimeter of a triangle with a non-zero area, formed from three of these lengths_. If it is impossible to form any triangle of a non-zero area, return `0`.

View file

@ -3,18 +3,29 @@
#### 2022-07-20 14:19
> ##### Algorithms:
>
> #algorithm #backtrack
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/letter-case-permutation/)
___
---
### Problem
Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.

View file

@ -3,17 +3,29 @@
#### 2022-06-14 21:54
---
##### Algorithms:
#algorithm #Floyd_s_cycle_finding_algorithm
##### Data structures:
#DS #linked_list
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/linked-list-cycle/)
___
---
### Problem
Given `head`, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to. **Note that `pos` is not passed as a parameter**.
@ -22,6 +34,7 @@ Return `true` _if there is a cycle in the linked list_. Otherwise, return `fals
**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
#### Examples
**Example 1:**
![example1](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
@ -51,21 +64,23 @@ Return `true` _if there is a cycle in the linked list_. Otherwise, return `fals
**Output:** false
**Explanation:** There is no cycle in the linked list.
```
#### Constraints
- The number of the nodes in the list is in the range `[0, 104]`.
- `-105 <= Node.val <= 105`
- `pos` is `-1` or a **valid index** in the linked-list.
### Thoughts
> [!summary]
> **Algorithm:**
> [!summary] > **Algorithm:**
> This is a #Floyd_s_cycle_finding_algorithm.
This is pretty straightforward, visit for more info
> This is pretty straightforward, visit for more info
### Solution
O(n)
```cpp
/**
* Definition for singly-linked list.

View file

@ -3,20 +3,33 @@
#### 2022-07-14 09:33
> ##### Algorithms:
>
> #algorithm #Kadane_s_algorithm #sliding_window
>
> ##### Data structures:
>
> #DS #string
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
___
---
### Problem
Given a string `s`, find the length of the **longest substring** without repeating characters.

View file

@ -3,20 +3,33 @@
#### 2022-07-08 11:53
> ##### Algorithms:
>
> #algorithm #binary_search #DFS
>
> ##### Data structures:
>
> #DS #binary_tree #binary_search_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
___
---
### Problem
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
@ -60,9 +73,11 @@ According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/
> This is a #binary_search
Because of the features of BST, the maximum val of a left sub-tree is smaller than node, so the valid LCA must meet this:
```
root->val >= small && root->val <= big
```
otherwise, search the left or right subtree.
### Solution

View file

@ -3,20 +3,33 @@
#### 2022-07-15 10:00
> ##### Algorithms:
>
> #algorithm
>
> ##### Data structures:
>
> #DS #vector_2d
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/max-area-of-island/)
___
---
### Problem
You are given an `m x n` binary matrix `grid`. An island is a group of `1`'s (representing land) connected **4-directionally** (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
@ -59,10 +72,12 @@ Return _the maximum **area** of an island in_ `grid`. If there is no island, ret
Simple O(mn) solution.
We keep track of pile we visited, using:
- an 2-d bool vector visited(which is the one I'm using)
- change the value of pile to 0
and check for:
- x, y is not OOB
- x, y is in a island -> (value == 1)
- x, y is not visited (see above)
@ -72,6 +87,7 @@ And for each unvisited pile, run DFS or BFS to check the size, and keep track of
### Solution
BFS
```cpp
class Solution {
int getSize(vector<vector<int>> &grid, int m, int n, int x, int y,
@ -137,6 +153,7 @@ public:
```
DFS iterative (simply change from queue to stack in BFS)
```cpp
class Solution {
// DFS iterative
@ -203,6 +220,7 @@ public:
```
DFS recursive
```cpp
class Solution {
// DFS recursive

View file

@ -3,21 +3,35 @@
#### 2022-07-05 09:25
> ##### Algorithms:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
___
---
### Problem
Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
@ -51,6 +65,7 @@ Simply log the level in each while iteration.
DFS way: (Popular)
Use recursion:
- Base Case:
- root == nullptr: return 0;
- maxDepth(root) = max(maxDepth(root->left), maxDepth(root->right))
@ -58,6 +73,7 @@ Use recursion:
### Solution
DFS Recursion:
```cpp
/**
* Definition for a binary tree node.
@ -85,6 +101,7 @@ public:
```
BFS:
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,24 +3,37 @@
#### 2022-06-27 11:09
> ##### Algorithms:
>
> #algorithm #Kadane_s_algorithm
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/maximum-difference-between-increasing-elements/)
___
---
### Problem
Given a **0-indexed** integer array `nums` of size `n`, find the **maximum difference** between `nums[i]` and `nums[j]` (i.e., `nums[j] - nums[i]`), such that `0 <= i < j < n` and `nums[i] < nums[j]`.
Return _the **maximum difference**._ If no such `i` and `j` exists, return `-1`.
#### Examples
Example 1:
```
@ -49,7 +62,6 @@ Explanation:
The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
```
#### Constraints
- n == nums.length
@ -59,6 +71,7 @@ The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 =
### Thoughts
Since 0 <= i < j <=n, this can be completed using kadane's algo in one pass.
> [!summary]
> This is a #Kadane_s_algorithm

View file

@ -3,22 +3,34 @@
#### 2022-06-09
---
##### Data stuctures:
#DS #array
##### Algorithms:
#algorithm #Kadane_s_algorithm
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Links:
- [Link to problem](https://leetcode.com/problems/maximum-subarray/)
- [Analysis](https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d)
##### Related topics:
### Problem
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return _its sum_.
A **subarray** is a **contiguous** part of an array.
#### Examples
Example 1:
```
@ -41,7 +53,6 @@ Input: nums = [5,4,-1,7,8]
Output: 23
```
#### Constraints
- 1 <= nums.length <= 105
@ -76,9 +87,11 @@ public:
This is a [[Kadane's Algorithm]] problem, and the philosophy behind it id divide and conquer.
local_max is the max accumulated number we've found, and global_max is the max local_max we've found.
```cpp
local_max = max(nums[i] + local_max, nums[i])
```
is the key to O(n) complexity.
> [!hint]

View file

@ -3,20 +3,32 @@
#### 2022-09-01 14:04
> ##### Algorithms:
>
> #algorithm #sort
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/merge-intervals/)
- [Solution ans Explanation](https://leetcode.com/problems/merge-intervals/solution/)
___
---
### Problem
Given an array of `intervals` where `intervals[i] = [starti, endi]`, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.

View file

@ -3,19 +3,30 @@
#### 2022-06-10
---
##### Data structures:
#DS #set #multiset #vector
##### Algorithms:
#algorithm #merge_sort #two_pointers
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/merge-sorted-array/)
- [multiset cpp reference](https://leetcode.com/problems/merge-sorted-array/)
___
---
### Problem
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.
**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.
@ -23,6 +34,7 @@ You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing
The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
#### Examples
Example 1:
```
@ -52,11 +64,13 @@ Note that because m = 0, there are no elements in nums1. The 0 is only there to
```
#### Constraints
- nums1.length == m + n
- nums2.length == n
- 0 <= m, n <= 200
- 1 <= m + n <= 200
- -10E9 <= nums1[i], nums2[j] <= 10E9
### Thoughts
I have came up with three ways.
@ -71,6 +85,7 @@ For using cpp, the most difficult thing was actually to use the vector library.
### Solution
multiset solution: O(nlogn)
```cpp
#include <iterator>
#include <set>
@ -97,6 +112,7 @@ public:
```
double pointer solution: O(m+n) (In c, it will get slower, since vectors are more efficient at inserting.)
```cpp
#include <iterator>
#include <vector>
@ -126,7 +142,9 @@ public:
}
};
```
c solution( not optimized )
```c
void insert(int *arr, int numsSize, int loc, int value) {
for (int i = numsSize - 1; i > loc; i--) {

View file

@ -3,20 +3,33 @@
#### 2022-07-16 09:11
> ##### Algorithms:
>
> #algorithm #DFS #DFS_inorder
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/merge-two-binary-trees/)
___
---
### Problem
You are given two binary trees `root1` and `root2`.
@ -52,6 +65,7 @@ Return _the merged tree_.
> This is a #DFS problem, can be solved with recursion
DFS preorder recursion:
- Base case: one leaf is empty, return another
- add value from root2 to root1

View file

@ -3,17 +3,29 @@
#### 2022-06-14 22:57
---
##### Algorithms:
#algorithm #two_pointers #recursion
##### Data structures:
#DS #linked_list
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/merge-two-sorted-lists/)
___
---
### Problem
You are given the heads of two sorted linked lists `list1` and `list2`.
Merge the two lists in a one **sorted** list. The list should be made by splicing together the nodes of the first two lists.
@ -21,6 +33,7 @@ Merge the two lists in a one **sorted** list. The list should be made by splicin
Return _the head of the merged linked list_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
@ -45,6 +58,7 @@ Return _the head of the merged linked list_.
```
#### Constraints
- The number of nodes in both lists is in the range `[0, 50]`.
- `-100 <= Node.val <= 100`
- Both `list1` and `list2` are sorted in **non-decreasing** order.
@ -61,16 +75,20 @@ The only thing to watch out for is when there is one list remaining, remember to
Very simple for recursion
##### Base Case:
- Both are nullptr -> return nullptr
- One is nullptr -> return another
##### Pseudocode:
- Check for base case:
- if list1->val > list2->val, list2's next should be the merged result
- if list2->val > list1->val, list1's next should be the merged result.
### Solution
Recursion
```cpp
class Solution {
public:
@ -96,6 +114,7 @@ public:
```
Two pointers
```cpp
/**
* Definition for singly-linked list.

View file

@ -3,20 +3,33 @@
#### 2022-07-13 09:08
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #linked_list
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/middle-of-the-linked-list/)
___
---
### Problem
Given the head of a singly linked list, return the middle node of the linked list.

View file

@ -3,20 +3,33 @@
#### 2022-07-11 09:55
> ##### Algorithms:
>
> #algorithm #two_pointers #array_in_place_operation
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/move-zeroes/)
___
---
### Problem
Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.

View file

@ -3,18 +3,29 @@
#### 2022-07-27 10:55
> ##### Data structures:
>
> #DS #stack #hash_table
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/next-greater-element-i/)
___
---
### Problem
The **next greater element** of some element `x` in an array is the **first greater** element that is **to the right** of `x` in the same array.
@ -32,6 +43,7 @@ Return _an array_ `ans` _of length_ `nums1.length` _such that_ `ans[i]` _is the
**Input:** nums1 = [4,1,2], nums2 = [1,3,4,2]
**Output:** [-1,3,-1]
**Explanation:** The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
@ -41,6 +53,7 @@ Return _an array_ `ans` _of length_ `nums1.length` _such that_ `ans[i]` _is the
**Input:** nums1 = [2,4], nums2 = [1,2,3,4]
**Output:** [3,-1]
**Explanation:** The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.

View file

@ -0,0 +1,66 @@
# Leetcode Non-Overlapping-Intervals
2022-09-03 15:34
> ##### Algorithms:
>
> #algorithm
>
> ##### Data structures:
>
> #DS
>
> ##### Difficulty:
>
> #coding_problem #difficulty-
>
> ##### Additional tags:
>
> #<platforms-like-leetcode> #<list-to-be-added>
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/non-overlapping-intervals/)
---
### Problem
Given an array of intervals `intervals` where `intervals[i] = [starti, endi]`, return _the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping_.
#### Examples
**Example 1:**
**Input:** intervals = [[1,2],[2,3],[3,4],[1,3]]
**Output:** 1
**Explanation:** [1,3] can be removed and the rest of the intervals are non-overlapping.
**Example 2:**
**Input:** intervals = [[1,2],[1,2],[1,2]]
**Output:** 2
**Explanation:** You need to remove two [1,2] to make the rest of the intervals non-overlapping.
**Example 3:**
**Input:** intervals = [[1,2],[2,3]]
**Output:** 0
**Explanation:** You don't need to remove any of the intervals since they're already non-overlapping.
#### Constraints
- `1 <= intervals.length <= 105`
- `intervals[i].length == 2`
- `-5 * 104 <= starti < endi <= 5 * 104`
### Thoughts
> [!summary]
> This is a #template_remove_me
### Solution

View file

@ -3,20 +3,33 @@
#### 2022-07-22 14:45
> ##### Algorithms:
>
> #algorithm #bit_manipulation
>
> ##### Data structures:
>
> #DS #bitset
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/number-of-1-bits/)
___
---
### Problem
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
@ -81,7 +94,7 @@ class Solution {
public:
int hammingWeight(uint32_t n) { return bitset<32>(n).count(); }
};
````
```
#### Method 2:

View file

@ -3,19 +3,31 @@
2022-09-02 14:54
> ##### Algorithms:
>
> #algorithm #recursion
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/pascals-triangle-ii/)
___
---
### Problem
Given an integer `rowIndex`, return the `rowIndexth` (**0-indexed**) row of the **Pascal's triangle**.

View file

@ -3,19 +3,30 @@
#### 2022-06-12
---
##### Data structures:
#DS #array
##### Algorithms:
#algorithm #recursion #iteration
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/pascals-triangle/)
- [Additional resources]()
___
---
### Problem
Given an integer `numRows`, return the first numRows of **Pascal's triangle**.
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
@ -23,6 +34,7 @@ In **Pascal's triangle**, each number is the sum of the two numbers directly abo
![Pascal Triangle](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
#### Examples
**Example 1:**
```markdown
@ -44,12 +56,14 @@ Output: [[1]]
### Thoughts
Very simple and straightforward problem.
> [!summary]
> answer[i][j] = answer[i - 1][j - 1] + answer[i - 1][j]
I worked around special cases using the for loop in j: j = 1 and j < i.
### Solution
```cpp
class Solution {
public:

View file

@ -3,20 +3,33 @@
#### 2022-07-06 13:45
> ##### Algorithms:
>
> #algorithm #DFS #recursion
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/path-sum/)
___
---
### Problem
Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
@ -66,12 +79,14 @@ There are one thing to consider, return false when the tree is empty.
Simple DFS-like recursion problem.
Base cases:
- node is empty, return false
- node is leaf
- if the value is sum, return true
- else return false
Pseudo-code:
- Check for base-cases
- return check(left, sum - root->val) || check(right, sum - root->val)

View file

@ -3,20 +3,33 @@
#### 2022-07-14 10:29
> ##### Algorithms:
>
> #algorithm #sliding_window
>
> ##### Data structures:
>
> #DS #string
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/permutation-in-string/)
___
---
### Problem
Given two strings `s1` and `s2`, return `true` _if_ `s2` _contains a permutation of_ `s1`_, or_ `false` _otherwise_.

View file

@ -3,18 +3,29 @@
#### 2022-07-20 14:16
> ##### Algorithms:
>
> #algorithm #backtrack
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/permutations/)
___
---
### Problem
Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.

View file

@ -3,29 +3,42 @@
#### 2022-07-16 09:43
> ##### Algorithms:
>
> #algorithm #BFS #optimization #DFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)
___
---
### Problem
You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
int val;
Node *left;
Node *right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
@ -89,9 +102,11 @@ since this is only one level, and doesn't work on two levels:
We recursively fill next pointers the level below current.
Base case:
- Root->left is empty, which means this is the leaf level, quit.
Pseudo-code:
1. Check for base case
1. link root->left and root->right
1. link root->right and root->next->left (if root->next is valid.)
@ -99,6 +114,7 @@ Pseudo-code:
### Solution
DFS:
```cpp
/*
// Definition for a Node.
@ -143,6 +159,7 @@ public:
```
Iterative, O(n)time O(1)space
```cpp
class Solution {
public:
@ -170,6 +187,7 @@ public:
```
BFS unoptimized:
```cpp
class Solution {
public:

View file

@ -3,20 +3,33 @@
#### 2022-07-22 14:30
> ##### Algorithms:
>
> #algorithm #bit_manipulation
>
> ##### Data structures:
>
> #DS #bitset
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/power-of-two/)
___
---
### Problem
Given an integer `n`, return _`true` if it is a power of two. Otherwise, return `false`_.

View file

@ -3,17 +3,29 @@
#### 2022-06-14 13:19
---
##### Algorithms:
#algorithm #hash_table
##### Data structures:
#DS #string #array
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/ransom-note/)
___
---
### Problem
Given two strings `ransomNote` and `magazine`, return `true` _if_ `ransomNote` _can be constructed by using the letters from_ `magazine` _and_ `false` _otherwise_.
Each letter in `magazine` can only be used once in `ransomNote`.
@ -40,6 +52,7 @@ Each letter in `magazine` can only be used once in `ransomNote`.
**Input:** s = "aabb"
**Output:** -1
```
#### Constraints
**Constraints:**
@ -52,7 +65,9 @@ Each letter in `magazine` can only be used once in `ransomNote`.
Super simple hash map, similar to [[Leetcode First-Unique-Character-In-a-String]]
### Solution
O(m + n)
```cpp
class Solution {
public:

View file

@ -3,23 +3,36 @@
#### 2022-06-16 14:21
> ##### Data structures:
>
> #DS #linked_list
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> Initial encounter: 2022-06-16
> 1st. revision: 2022-07-02
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
___
---
### Problem
Given the `head` of a sorted linked list, _delete all duplicates such that each element appears only once_. Return _the linked list **sorted** as well_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg)
@ -35,6 +48,7 @@ Given the `head` of a sorted linked list, _delete all duplicates such that each
**Output:** [1,2,3]
#### Constraints
- The number of nodes in the list is in the range `[0, 300]`.
- `-100 <= Node.val <= 100`
- The list is guaranteed to be **sorted** in ascending order.
@ -50,6 +64,7 @@ To understand the ptr->next->next = ptr->next, see[[Leetcode Reverse-Linked-List
### Solution
One pointer method
```cpp
/**
* Definition for singly-linked list.
@ -80,4 +95,3 @@ public:
}
};
```

View file

@ -3,19 +3,30 @@
#### 2022-06-15 21:50
---
##### Data structures:
#DS #linked_list
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/remove-linked-list-elements/)
- [Additional Solutions](https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186)
___
- [Additional Solutions](<https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186>)
---
### Problem
Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg)
@ -38,7 +49,9 @@ Given the `head` of a linked list and an integer `val`, remove all the nodes of
**Input:** head = [7,7,7,7], val = 7
**Output:** []
```
#### Constraints
- The number of nodes in the list is in the range `[0, 104]`.
- `1 <= Node.val <= 50`
- `0 <= val <= 50`
@ -46,10 +59,13 @@ Given the `head` of a linked list and an integer `val`, remove all the nodes of
### Thoughts
Simple linked list operations, but remember to check for special cases:
- The pointer is null
### Solution
Two pointers, O(n)
```cpp
/**
* Definition for singly-linked list.
@ -95,6 +111,7 @@ public:
These two are taken from discussions, and they are **not** memory safe.
Recursive solution from the same guy:
```cpp
class Solution {
public:
@ -111,7 +128,8 @@ public:
};
```
One pointer from [Discussions](https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186)
One pointer from [Discussions](<https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186>)
```cpp
class Solution {
public:
@ -134,4 +152,3 @@ public:
}
};
```

View file

@ -3,21 +3,34 @@
#### 2022-07-13 09:31
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #linked_list
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
- [Explanation](https://leetcode.com/problems/remove-nth-node-from-end-of-list/discuss/1164542/JS-Python-Java-C%2B%2B-or-Easy-Two-Pointer-Solution-w-Explanation)
___
---
### Problem
Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
@ -65,6 +78,7 @@ pointers reach the end at the same time.
We can let fast go n step first, such that **when fast comes to the end**, slow gets to the Nth node from the last.
To find the 2nd from the last:
```
slow
@ -118,7 +132,6 @@ public:
};
```
Initial iteration:
```cpp

View file

@ -3,15 +3,25 @@
#### 2022-06-12
---
##### Data structures:
#DS #array #vector
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/reshape-the-matrix/)
___
---
### Problem
In MATLAB, there is a handy function called `reshape` which can reshape an `m x n` matrix into a new one with a different size `r x c` keeping its original data.
You are given an `m x n` matrix `mat` and two integers `r` and `c` representing the number of rows and the number of columns of the wanted reshaped matrix.
@ -21,12 +31,14 @@ The reshaped matrix should be filled with all the elements of the original matri
If the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
#### Examples
```markdown
**Input:** mat = [[1,2],[3,4]], r = 1, c = 4
**Output:** [[1,2,3,4]]
```
#### Constraints
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 100
@ -46,6 +58,7 @@ Then, from the hint, I know we can transform any array to 1-D, then transform 1-
### Solution
O(nm) solution using two loops
```cpp
class Solution {
public:
@ -79,6 +92,7 @@ public:
```
O(mn) Solution using one loop
> [!tip]
> use `vector<vector<int>> ans(r, vector<int>(c));`
> to initialize a 2-d vector

View file

@ -3,22 +3,35 @@
#### 2022-07-22 15:15
> ##### Algorithms:
>
> #algorithm #bit_manipulation
>
> ##### Data structures:
>
> #DS #bitset
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/reverse-bits/)
- [Solution and detailed explanation](https://leetcode.com/problems/reverse-bits/discuss/1232842/JAVA-C%2B%2B-%3A-0ms-or-O(1)-Time-Complexity-or-in-place-or-Detailed-Explanation)
- [Shifting solution an explanation](https://leetcode.com/problems/reverse-bits/discuss/54741/O(1)-bit-operation-C++-solution-(8ms)/301342)
___
- [Solution and detailed explanation](<https://leetcode.com/problems/reverse-bits/discuss/1232842/JAVA-C%2B%2B-%3A-0ms-or-O(1)-Time-Complexity-or-in-place-or-Detailed-Explanation>)
- [Shifting solution an explanation](<https://leetcode.com/problems/reverse-bits/discuss/54741/O(1)-bit-operation-C++-solution-(8ms)/301342>)
---
### Problem
Reverse bits of a given 32 bits unsigned integer.
@ -47,6 +60,7 @@ Reverse bits of a given 32 bits unsigned integer.
> This is a #bit_manipulation problem.
There are two methods:
- swapping bits inside the bitset
- shifting

View file

@ -3,23 +3,41 @@
#### 2022-06-15 22:07
---
##### Algorithms:
#algorithm #recursion #iteration
##### Data structures:
#DS #linked_list
##### Difficulty:
#leetcode #coding_problem #difficulty-medium
##### Lists:
#CS_list_need_understanding #CS_list_need_practicing
##### Revisions:
2022-07-02
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/reverse-linked-list/)
___
---
### Problem
Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
@ -46,6 +64,7 @@ Given the `head` of a singly linked list, reverse the list, and return _the reve
```
#### Constraints
- The number of nodes in the list is the range `[0, 5000]`.
- `-5000 <= Node.val <= 5000`
@ -56,16 +75,22 @@ 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.
> **The only recursive part is returning the _tail node_.**
### Solution
I've referred to this guy: https://leetcode.com/problems/reverse-linked-list/discuss/58130/C%2B%2B-Iterative-and-Recursive
Insertion, iteration
```cpp
class Solution {
public:
@ -88,6 +113,7 @@ public:
```
Recursion:
```cpp
/**
* Definition for singly-linked list.

View file

@ -3,20 +3,33 @@
#### 2022-07-12 08:50
> ##### Algorithms:
>
> #algorithm #recursion
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/reverse-string/)
___
---
### Problem
Write a function that reverses a string. The input string is given as an array of characters `s`.
@ -46,7 +59,9 @@ You must do this by modifying the input array [in-place](https://en.wikipedia.or
> This is a #recursion problem, also can be solved using iteration.
#### Base case:
- size <= 1
#### Pseudo code:
- swap two ends
@ -55,6 +70,7 @@ You must do this by modifying the input array [in-place](https://en.wikipedia.or
### Solution
Iteration
```cpp
class Solution {
public:
@ -72,6 +88,7 @@ public:
```
Recursion
```cpp
class Solution {
void reverse(vector<char> &s, int l, int r) {

View file

@ -3,20 +3,33 @@
#### 2022-07-12 09:10
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #string
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/reverse-words-in-a-string-iii/)
___
---
### Problem
Given a string `s`, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

View file

@ -3,20 +3,33 @@
#### 2022-07-10 08:54
> ##### Algorithms:
>
> #algorithm #array_in_place_operation #reverse_array
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_practicing #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/rotate-array/)
___
---
### Problem
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
@ -57,6 +70,7 @@ This one is hard.
- Reversing a subarray changes the edge's neighbors
Suppose a array looks like:
```
[1, 2,| 3, 4, 5]
```
@ -117,6 +131,7 @@ public:
```
Method 2:
```cpp
class Solution {
void swap(vector<int> &nums, int l, int r) {
@ -148,6 +163,7 @@ public:
```
Method 3:
```cpp
class Solution {
void swap(vector<int> &nums, int l, int r) {

View file

@ -3,20 +3,32 @@
2022-09-02 15:40
> ##### Algorithms:
>
> #algorithm #math
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_understanding
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/rotate-image/)
- [Solution and explanation](https://leetcode.com/problems/rotate-image/solution/)
___
---
### Problem
You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).

View file

@ -3,20 +3,33 @@
#### 2022-07-17 15:12
> ##### Algorithms:
>
> #algorithm #BFS
>
> ##### Data structures:
>
> #DS #vector_2d
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/rotting-oranges/)
___
---
### Problem
You are given an `m x n` `grid` where each cell can have one of three values:
@ -46,6 +59,7 @@ Return _the minimum number of minutes that must elapse until no cell has a fresh
**Input:** grid = [[2,1,1],[0,1,1],[1,0,1]]
**Output:** -1
```
**Explanation:** The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
**Example 3:**
@ -54,6 +68,7 @@ Return _the minimum number of minutes that must elapse until no cell has a fresh
**Input:** grid = [[0,2]]
**Output:** 0
```
**Explanation:** Since there are already no fresh oranges at minute 0, the answer is just 0.
#### Constraints
@ -70,6 +85,7 @@ Return _the minimum number of minutes that must elapse until no cell has a fresh
> the shortest {time, distance}
Be wary of these special cases:
- everything is empty cell -> return -1, no orange => no
fresh orange
- everything is rotten -> return 0
@ -77,20 +93,24 @@ Be wary of these special cases:
> [!tip] update the value in place vs. in queue
> Update in place, such as
>
> ```cpp
> if (grid[a + 1][b] != 0) {
> queue.push({a, b});
> grid[a + 1][b] = 0;
> }
> ```
>
> Can avoid multiple enqueues, because the value is already
> updated, so another node in queue can't push that again.
>
> ```cpp
> grid[a][b] = 0;
> if (grid[a + 1][b] != 0) {
> queue.push({a, b});
> }
> ```
>
> this code might result in pushing the same node twice.
### Solution

View file

@ -3,19 +3,31 @@
2022-09-03 14:57
> ##### Algorithms:
>
> #algorithm #divide_and_conquer
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
> #leetcode
>
> #leetcode #CS_list_need_practicing
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/search-a-2d-matrix-ii/)
___
---
### Problem
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
@ -57,3 +69,26 @@ Start from the top-right, (alternatively, bottom-left),
because walking left makes the number smaller, and down makes the number bigger.
### Solution
```cpp
class Solution {
public:
bool searchMatrix(vector<vector<int>> &matrix, int target) {
// search from top-right
int c = matrix[0].size() - 1;
int r = 0;
int m = matrix.size();
while (c >= 0 && r < m) {
if (matrix[r][c] > target) {
c--;
} else if (matrix[r][c] < target) {
r++;
} else {
return true;
}
}
return false;
}
};
```

View file

@ -3,20 +3,33 @@
#### 2022-07-07 08:06
> ##### Algorithms:
>
> #algorithm #DFS #BFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/search-in-a-binary-search-tree/submissions/)
___
---
### Problem
You are given the `root` of a binary search tree (BST) and an integer `val`.
@ -35,8 +48,6 @@ Example 2:
Input: root = [4,2,7,1,3], val = 5
Output: []
#### Constraints
- The number of nodes in the tree is in the range `[1, 5000]`.
@ -56,6 +67,7 @@ In BFS, I don't have use for loop inside while loop, since we don't have to cons
### Solution
DFS
```cpp
/**
* Definition for a binary tree node.
@ -97,6 +109,7 @@ public:
```
Which can be simplified to
```cpp
/**
* Definition for a binary tree node.
@ -131,6 +144,7 @@ public:
```
BFS
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,20 +3,33 @@
#### 2022-07-09 10:25
> ##### Algorithms:
>
> #algorithm #binary_search
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-search/)
___
---
### Problem
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
@ -56,6 +69,7 @@ if the value is found, simply return it
if not found, at the end, position `l - 1` should be smaller than val.
and position `l` should be bigger, which is the position for the answer:
```
If not, return the index where it would be if it were inserted in order.
```
@ -63,6 +77,7 @@ If not, return the index where it would be if it were inserted in order.
### Solution
Iteration
```cpp
class Solution {
public:

View file

@ -3,23 +3,36 @@
#### 2022-06-13 15:33
---
##### Algorithms:
#algorithm #binary_search
##### Data structures:
#DS #vector #vector_2d
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/search-a-2d-matrix/submissions/)
___
---
### Problem
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
#### Examples
**Example 1:**
![exapmle1](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
@ -28,6 +41,7 @@ Write an efficient algorithm that searches for a value `target` in an `m x n` in
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
**Output:** true
```
**Example 2:**
![exapmle2](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
@ -36,7 +50,9 @@ Write an efficient algorithm that searches for a value `target` in an `m x n` in
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
**Output:** false
```
#### Constraints
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 100
@ -45,16 +61,20 @@ Write an efficient algorithm that searches for a value `target` in an `m x n` in
### Thoughts
Binary search algorithm, with simple 2-d to 1-d conversion
> [!tip] Binary search while loops
> In binary search, remember to check for:
>
> - l <= r
> And use:
> - l = mid + 1
> - r = mid - 1
> Init r as m * n - 1 to avoid mid being OOB.
> Init r as m \* n - 1 to avoid mid being OOB.
### Solution
==note how I calculated the mid, i, j, and how I changed r and l==
```cpp
class Solution {
public:

View file

@ -3,22 +3,35 @@
#### 2022-07-22 15:31
> ##### Algorithms:
>
> #algorithm #bit_manipulation
>
> ##### Data structures:
>
> #DS #bitset
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/single-number/)
- [Learn about XOR](https://en.wikipedia.org/wiki/Exclusive_or#Computer_science)
- [Explanation](https://leetcode.com/problems/single-number/discuss/1772139/C%2B%2Bor-Explained-Everything-w-WHY-XOR-WORKSor-BRUTE-FORCE-TO-OPTIMIZEDor-STEP-BY-STEP-DRY-RUN)
___
---
### Problem
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
@ -53,7 +66,6 @@ You must implement a solution with a linear runtime complexity and use only co
> [!summary]
> This is a #bit_manipulation problem utilizing the XOR operator
The XOR operator has some properties:
```

View file

@ -3,19 +3,31 @@
#### 2022-09-01 13:24
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem](https://leetcode.com/problems/sort-colors/)
___
---
### Problem
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

View file

@ -3,20 +3,33 @@
#### 2022-07-10 08:50
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/squares-of-a-sorted-array/)
___
---
### Problem
Given an integer array `nums` sorted in **non-decreasing** order, return _an array of **the squares of each number** sorted in non-decreasing order_.
@ -55,6 +68,7 @@ One from left, one from right end.
### Solution
cpp
```cpp
class Solution {
public:

View file

@ -3,20 +3,33 @@
#### 2022-07-22 16:16
> ##### Algorithms:
>
> #algorithm #backtrack
>
> ##### Data structures:
>
> #DS #vector
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/subsets/)
___
---
### Problem
Given an integer array `nums` of **unique** elements, return _all possible subsets (the power set)_.
@ -53,9 +66,10 @@ The solution set **must not** contain duplicate subsets. Return the solution in
It asked for combinations, so this is a backtrack problem.
go from index 0 to last one, in each iteration, you have two choices:
- Add this number
- Don't add this number
And we try different combinations at the same level, aka. backtracking.
And we try different combinations at the same level, aka. backtracking.
#### Pseudo code:

View file

@ -3,24 +3,39 @@
#### 2022-07-05 10:15
> ##### Algorithms:
>
> #algorithm #DFS #recursion #BFS
>
> ##### Data structures:
>
> #DS #binary_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/symmetric-tree/)
___
---
### Problem
Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
@ -36,16 +51,19 @@ Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e
**Output:** false
#### Constraints
**Constraints:**
- The number of nodes in the tree is in the range `[1, 1000]`.
- `-100 <= Node.val <= 100`
### Thoughts
> [!summary]
> This is a #DFS #recursion problem
Method 1, DFS-like Recursion:
- Base Cases:
- left and right are nullptr: true
- else if left or right is nullptr: false, must be asymmetric
@ -54,6 +72,7 @@ Method 1, DFS-like Recursion:
Method 2, BFS-like Iteration:
In the while loop:
- Take two nodes from queue, they should be matched.
- if both are nullptr, continue.
- if one is nullptr, return false.
@ -64,6 +83,7 @@ In the while loop:
### Solution
Recursion, 16ms
```cpp
/**
* Definition for a binary tree node.
@ -104,6 +124,7 @@ public:
```
BFS, iteration, 8ms
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,18 +3,29 @@
#### 2022-07-20 22:59
> ##### Algorithms:
>
> #algorithm #dynamic_programming
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/triangle/)
___
---
### Problem
Given a `triangle` array, return _the minimum path sum from top to bottom_.

View file

@ -3,20 +3,33 @@
#### 2022-07-11 14:54
> ##### Algorithms:
>
> #algorithm #two_pointers
>
> ##### Data structures:
>
> #DS #array
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)
___
---
### Problem
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`.

View file

@ -3,21 +3,34 @@
#### 2022-07-08 11:11
> ##### Algorithms:
>
> #algorithm #binary_search #BFS
>
> ##### Data structures:
>
> #DS #binary_tree #binary_search_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)
- [Three method to solve this](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/discuss/106059/JavaC%2B%2B-Three-simple-methods-choose-one-you-like)
___
---
### Problem
Given the `root` of a Binary Search Tree and a target number `k`, return _`true` if there exist two elements in the BST such that their sum is equal to the given target_.
@ -41,9 +54,9 @@ Given the `root` of a Binary Search Tree and a target number `k`, return _`true`
#### Constraints
- The number of nodes in the tree is in the range `[1, 104]`.
- `-104 <= Node.val <= 104`
- `-104 <= Node.val <= 104`
- `root` is guaranteed to be a **valid** binary search tree.
- `-105 <= k <= 105`
- `-105 <= k <= 105`
### Thoughts
@ -51,6 +64,7 @@ Given the `root` of a Binary Search Tree and a target number `k`, return _`true`
> This is a #BFS #hash_table problem.
Mainly two methods:
1. #BFS with hash table. Time space O(n)
This can be quicker since you are starting at the middle, which is more likely to hit the answer, theoretically taking less time.
2. #binary_search. Time O(hn), h is the height of BST, best case h == log(n), worst case h == n
@ -59,6 +73,7 @@ Mainly two methods:
### Solution
BFS with hash table
```cpp
/**
* Definition for a binary tree node.

View file

@ -3,19 +3,30 @@
#### 2022-06-10
---
##### Data structures:
#DS #array #map #unordered_map
##### Algorithms:
#algorithm
##### Difficulty:
#coding_problem #leetcode #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/two-sum)
- [O(n) Solution](https://leetcode.com/problems/two-sum/discuss/13/Accepted-C++-O(n)-Solution/263)
___
##### Data structures:
#DS #array #map #unordered_map
##### Algorithms:
#algorithm
##### Difficulty:
#coding_problem #leetcode #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/two-sum)
- [O(n) Solution](<https://leetcode.com/problems/two-sum/discuss/13/Accepted-C++-O(n)-Solution/263>)
---
### Problem
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
@ -56,7 +67,7 @@ Output: [0,1]
### Thoughts
Firstly, I think up an easy O(n^2) solution, by using a nested loop,
But the best solution utilizes an __[[cpp_std_unordered_map]]__.
But the best solution utilizes an **[[cpp_std_unordered_map]]**.
> [!tips]
> Use **[[cpp_std_unordered_map | unordered map]]** to create a hash table, it has O(1) in search, delete and insert. #tip
@ -64,6 +75,7 @@ But the best solution utilizes an __[[cpp_std_unordered_map]]__.
### Solution
O(n^2) solution
```cpp
class Solution {
public:
@ -84,6 +96,7 @@ public:
```
O(n) solution
```cpp
class Solution {
public:

View file

@ -3,29 +3,44 @@
#### 2022-06-14 13:36
---
##### Algorithms:
#algorithm #hash_table
##### Data structures:
#DS #array
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/valid-anagram/)
___
---
### Problem
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
#### Examples
**Example 1:**
```markdown
**Input:** s = "anagram", t = "nagaram"
**Output:** true
```
**Example 2:**
```markdown
@ -34,6 +49,7 @@ An **Anagram** is a word or phrase formed by rearranging the letters of a differ
```
#### Constraints
- `1 <= s.length, t.length <= 5 * 104`
- `s` and `t` consist of lowercase English letters.
@ -50,7 +66,6 @@ Refer to this [site](https://www.cprogramming.com/tutorial/unicode.html) and thi
### Solution
```cpp
class Solution {
public:

View file

@ -3,19 +3,31 @@
#### 2022-06-16 14:50
> ##### Data structures:
>
> #DS #stack
>
> ##### Difficulty:
>
> #coding_problem #difficulty-easy
>
> ##### Additional tags:
>
> #leetcode
>
> ##### Revisions:
>
> 1st revision: 2022-07-03 optimize code
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/valid-parentheses/)
___
---
### Problem
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
An input string is valid if:
@ -24,6 +36,7 @@ An input string is valid if:
2. Open brackets must be closed in the correct order.
#### Examples
**Example 1:**
```markdown
@ -44,7 +57,9 @@ An input string is valid if:
**Input:** s = "(]"
**Output:** false
```
#### Constraints
- `1 <= s.length <= 104`
- `s` consists of parentheses only `'()[]{}'`.
@ -53,8 +68,11 @@ An input string is valid if:
Basic stack usage.
Can be implemented using std_stack or stc_vector.
obviously i should use std_stack.
### Solution
1st revision: optimized code
```cpp
class Solution {
public:
@ -129,4 +147,3 @@ public:
};
```

View file

@ -3,15 +3,24 @@
#### 2022-06-13 12:56
---
##### Data structures:
#DS #vector
##### Difficulty:
#leetcode #coding_problem #difficulty-easy
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/valid-sudoku/)
- [Simple solution](https://leetcode.com/problems/valid-sudoku/discuss/15464/My-short-solution-by-C%2B%2B.-O(n2))
___
- [Simple solution](<https://leetcode.com/problems/valid-sudoku/discuss/15464/My-short-solution-by-C%2B%2B.-O(n2)>)
---
### Problem
Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
@ -26,7 +35,9 @@ Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to b
- Only the filled cells need to be validated according to the mentioned rules.
#### Examples
![[Pasted image 20220613125739.png]]
```markdown
**Input:** board =
[["5","3",".",".","7",".",".",".","."]
@ -70,14 +81,17 @@ Besides overthinking, I also spent a lot of time learning how to use sets and in
**Take TWO:**
Use a hash table to store whether an element is present:
```cpp
int usedRow[9][10] = {};
int usedCol[9][10] = {};
int usedGrid[9][10] = {};
```
### Solution
My new solution using hash map
```cpp
class Solution {
public:
@ -115,6 +129,7 @@ public:
```
Bad solution :(
```cpp
class Solution {
public:

View file

@ -3,21 +3,35 @@
#### 2022-07-08 10:36
> ##### Algorithms:
>
> #algorithm #DFS #DFS_inorder
>
> ##### Data structures:
>
> #DS #binary_tree #binary_search_tree
>
> ##### Difficulty:
>
> #coding_problem #difficulty-medium
>
> ##### Additional tags:
>
> #leetcode #CS_list_need_practicing
>
> ##### Revisions:
>
> N/A
##### Related topics:
##### Links:
- [Link to problem](https://leetcode.com/problems/validate-binary-search-tree/)
___
---
### Problem
Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
@ -44,8 +58,10 @@ A **valid BST** is defined as follows:
**Explanation:** The root node's value is 5 but its right child's value is 4.
#### Constraints
- The number of nodes in the tree is in the range `[1, 104]`.
- `-231 <= Node.val <= 231 - 1`
### Thoughts
> [!summary]

View file

@ -2,23 +2,36 @@
#### 2022-06-14 23:06
___
---
##### Algorithms:
#algorithm #two_pointers
##### Data structures:
#array #linked_list
##### Difficulty:
#CS_analysis #difficulty-
##### Related problems:
##### Links:
- [leetcode](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/)
___
---
### What is Two pointers approach?
By using two pointers, to in place modify array elements.
- One fast and one slow [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] or [[Leetcode Linked-List-Cycle]]
- The two are unordered [[Leetcode Merge-Two-Sorted-Lists]]
### Why and when to use it?
- There are two arrays, or linked lists
- They are sorted, or operation in place will not interfere elements after.

View file

@ -2,24 +2,32 @@
#### 2022-06-11
___
---
##### Data structures:
#DS #array #vector #multiset
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### Links:
- [cppreference](https://en.cppreference.com/w/cpp/language/range-for)
___
---
### What is cpp_Range_based_for_loop?
Executes a for loop over a range.
Used as a more readable equivalent to the traditional [for loop](https://en.cppreference.com/w/cpp/language/for "cpp/language/for") operating over a range of values, such as all elements in a container.
### Example
```cpp
#include <iostream>
#include <vector>
@ -78,6 +86,7 @@ int main()
```
Possible output:
```
0 1 2 3 4 5
0 1 2 3 4 5

View file

@ -2,15 +2,23 @@
#### 2022-06-12
___
---
##### Data structures:
#DS #multiset
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### Links:
- [cppreference](https://en.cppreference.com/w/cpp/container/multiset)
___
---
### What is Multiset?

View file

@ -2,22 +2,32 @@
#### 2022-06-11
___
---
##### Algorithms:
#algorithm #sort
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### Links:
- [cpp reference](https://en.cppreference.com/w/cpp/algorithm/sort)
___
---
### What is Sort?
Sorts the elements in the range `[first, last)` in non-descending order. The order of equal elements is not guaranteed to be preserved.
The default Compare function is less: `<`
### Example
```cpp
#include <algorithm>
#include <functional>

View file

@ -2,17 +2,25 @@
#### 2022-06-10
___
---
##### Data structures:
#DS #unordered_map #hash_table
##### Difficulty:
#CS_analysis #difficulty-easy
##### Related problems:
##### Links:
- [Unordered map explainer](https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/)
- [Unordered map vs ordered map](https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/)
- [cpp reference](https://en.cppreference.com/w/cpp/container/unordered_map)
___
---
### What is Unordered Map?
@ -28,6 +36,7 @@ Internally unordered_map is implemented using [Hash Table](https://www.geeksforg
### How to use
Example taken from [g4g](https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/)
```cpp
// C++ program to demonstrate functionality of unordered_map
#include <iostream>

View file

@ -12,5 +12,6 @@ Write a ncurses or slang program to display stuff and:
chroots.
Goal:
- [ ] to learn cpp
- [ ] to learn cpp based build systems

View file

@ -1,8 +1,8 @@
1. use ```int arr[][Columns]``` for 2-d function declaration
2. ```#pragma gcc optimize("Ofast, inline")``` to increase speed
1. use `int arr[][Columns]` for 2-d function declaration
2. `#pragma gcc optimize("Ofast, inline")` to increase speed
3. use parens to close bitwise operations and %
4. remember the scope in loops (int i) (int j)
5. ```matrix[i][j]``` in scanning a 2d array.
6. figure out ```matrix[i][j]``` and ```matrix[i][n-1]```
5. `matrix[i][j]` in scanning a 2d array.
6. figure out `matrix[i][j]` and `matrix[i][n-1]`
More can be found in [[OJ-index]]

View file

@ -3,157 +3,174 @@
### System-wide
#### Development
* libXres
* to use gamescope
- libXres
- to use gamescope
#### Games
* SuperTuxKart
- SuperTuxKart
#### Command Line Productivity
* ripgrep
* quickly search thru dirs
* z
* replacement of fasd
* nnn
* file manager
* entr
* watch for file change
- ripgrep
- quickly search thru dirs
- z
- replacement of fasd
- nnn
- file manager
- entr
- watch for file change
#### Mail
* Thunderbird
- Thunderbird
#### Video and audio
* xdg-desktop-portal
* for use with screencasting
* rtkit
* for better dbus support
* ofono
* for better bluetooth conection
* obs
* for test streaming
* wlrobs
* wayland plugin
- xdg-desktop-portal
- for use with screencasting
- rtkit
- for better dbus support
- ofono
- for better bluetooth conection
- obs
- for test streaming
- wlrobs
- wayland plugin
#### System
* android-tools
* to use adb fastboot ... etc
* ncurses-compta
* to make lineageOS
* v4l2loopback
* to loopback webcam into a device
* wine
* Fuck tencent, i hate chinese proprietary shitware.
* winetricks
* I hate wine and tencent.
* wf-recorder
* record screen
* tree
* to show things in a tree
* pexpect
* developing python program
- android-tools
- to use adb fastboot ... etc
- ncurses-compta
- to make lineageOS
- v4l2loopback
- to loopback webcam into a device
- wine
- Fuck tencent, i hate chinese proprietary shitware.
- winetricks
- I hate wine and tencent.
- wf-recorder
- record screen
- tree
- to show things in a tree
- pexpect
- developing python program
#### Softwares
* imv
* for image viewing
* fzf
* fuzzy searcher ytfzf
* wf-recorder
* to record things
- imv
- for image viewing
- fzf
- fuzzy searcher ytfzf
- wf-recorder
- to record things
#### Coding
* gdb
* for debugging in neovim
* ltrace
* for reverse engineering
* repo
* to sync android tree
* luaformatter
* to format lua code
* prettier
* to format web code
- gdb
- for debugging in neovim
- ltrace
- for reverse engineering
- repo
- to sync android tree
- luaformatter
- to format lua code
- prettier
- to format web code
#### Text Editing
* pandoc
* for word processing
* texlive
* for word processing
* latexmk
* for vim latex support
* ctags
* for vieving tags in vim
* libreoffice
* for vieving and editing doc files
* wl-clipboard
* to use copy and paste in cli
* fcitx5
* to use chinnnese input.
* fcitx5-gtk
* fcitx5-rime
- pandoc
- for word processing
- texlive
- for word processing
- latexmk
- for vim latex support
- ctags
- for vieving tags in vim
- libreoffice
- for vieving and editing doc files
- wl-clipboard
- to use copy and paste in cli
- fcitx5
- to use chinnnese input.
- fcitx5-gtk
- fcitx5-rime
#### Info
* powertop
* for battery stat
* radeontop
* to see gpu usage
* bat
* for better cat
* eix
* for emerge managament
* mediainfo
* for viewing media info.
* ncdu
* to view disk usage
* iotop
* to see disk io
- powertop
- for battery stat
- radeontop
- to see gpu usage
- bat
- for better cat
- eix
- for emerge managament
- mediainfo
- for viewing media info.
- ncdu
- to view disk usage
- iotop
- to see disk io
#### Cosmetic
* dejavu
* for better font
* Symbols-2048-em Nerd Font Complete.ttf (locally)
* for nerd icon
* geteltorito
* for flashing bios image
- dejavu
- for better font
- Symbols-2048-em Nerd Font Complete.ttf (locally)
- for nerd icon
- geteltorito
- for flashing bios image
#### Pentesting
* cracklib-words
* wordlist
* lynis
* to analyze vuls
* nmap
* to scan ports
* aide
* to check file integrity
* arp-scan
* to scan local network.
* openVPN
* to access THM machines.
* whois
* to query domain status
- cracklib-words
- wordlist
- lynis
- to analyze vuls
- nmap
- to scan ports
- aide
- to check file integrity
- arp-scan
- to scan local network.
- openVPN
- to access THM machines.
- whois
- to query domain status
### Local
* cxxmatrix
* cool matrix effects.
* gamescope
* RSR for steam games
- cxxmatrix
- cool matrix effects.
- gamescope
- RSR for steam games
### Pip (I hate this)
* pip-autoremove
* to remove pip software
* subilminal
* to download subs using mpv scripts
* pipx
* to properly manage apps
* gdbgui
* gui frontend of gdb
- pip-autoremove
- to remove pip software
- subilminal
- to download subs using mpv scripts
- pipx
- to properly manage apps
- gdbgui
- gui frontend of gdb
### npm
* neovim
* for npm support
- neovim
- for npm support
### go
* go buster
* for brute-force URLs
- go buster
- for brute-force URLs
### Cargo
* zeta-note
* lsp for markdowm
- zeta-note
- lsp for markdowm

View file

@ -17,6 +17,7 @@
## Projects
### [Notes for CS](Notes%20for%20CS)
### [Chroot manager](ChrootMan.md)
## Others

View file

@ -3,19 +3,31 @@
{{date}} {{time}}
> ##### Algorithms:
>
> #algorithm
>
> ##### Data structures:
>
> #DS
>
> ##### Difficulty:
>
> #coding_problem #difficulty-
>
> ##### Additional tags:
>
> #<platforms-like-leetcode> #<list-to-be-added>
>
> ##### Revisions:
>
> N/A
##### Links:
- [Link to problem]()
___
---
### Problem
#### Examples

View file

@ -3,23 +3,30 @@
{{date}} {{time}}
> ##### Algorithms:
> #algorithm
> ##### Data structures:
> #DS
> ##### Difficulty:
> #CS_analysis #difficulty-
> ##### Additional tags:
>
> #algorithm
>
> ##### Data structures:
>
> #DS
>
> ##### Difficulty:
>
> #CS_analysis #difficulty-
>
> ##### Additional tags:
##### Related problems:
```expander
tag:#coding_problem tag:#<CHANGE_ME> -tag:#template_remove_me
```
##### Links:
- [cppreference]()
___
---
### What is {{title}}?
@ -28,4 +35,5 @@ ___
#### Example code
### Why and when to use it?
#template_remove_me