Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/deploy-cheatsheet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# # .github/workflows/deploy-cheatsheet.yml

# name: Deploy Cheatsheet to GitHub Pages

# on:
# push:
# branches: [ main ] # Or your default branch
# paths:
# - 'doc/cheatsheet/**'

# permissions:
# contents: read
# pages: write
# id-token: write

# concurrency:
# group: 'pages'
# cancel-in-progress: true

# jobs:
# deploy:
# runs-on: ubuntu-latest

# steps:
# - name: Checkout Repository
# uses: actions/checkout@v3

# - name: Setup Pages
# uses: actions/configure-pages@v3

# - name: Upload Cheatsheet files
# uses: actions/upload-pages-artifact@v2
# with:
# path: doc/cheatsheet

# - name: Deploy to GitHub Pages
# uses: actions/deploy-pages@v2
51 changes: 51 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Build and Deploy GitHub Pages

on:
push:
branches:
- main
- master
paths:
- 'doc/cheatsheet/**' # Only trigger when files in doc/cheatsheet change

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
bundler-cache: true

- name: Create Jekyll site structure
run: |
mkdir -p _layouts _includes _sass assets
# Copy only cheatsheet files to a temporary directory
mkdir -p temp_site/doc/cheatsheet
cp -r doc/cheatsheet/* temp_site/doc/cheatsheet/
# Create a minimal config file
echo "source: temp_site" > _config.yml
echo "baseurl: /CS_basics" >> _config.yml
echo "title: CS Basics Cheatsheets" >> _config.yml
echo "markdown: kramdown" >> _config.yml

- name: Install Jekyll and Dependencies
run: |
gem install bundler
bundle init
bundle add jekyll -v "~> 3.9.0"
bundle add github-pages
bundle install

- name: Build Site
run: bundle exec jekyll build --source temp_site --destination _site

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_site
23 changes: 11 additions & 12 deletions doc/cheatsheet/backtrack.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ These are often **permutation problems**, where:
- You **should revisit** earlier choices (sometimes)


-> Dont use `start_idx` when:
-> Don't use `start_idx` when:
- You're generating **permutations**
- You need **all orderings**
- Choices are not sequential (e.g., trying all positions)
Expand Down Expand Up @@ -789,10 +789,10 @@ if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])

// dfsFind(board, word, x+1, y, visited, start_idx + 1)

// 1) Youre passing a copy of start_idx + 1 to the recursive function. So, inside the recursive call, start_idx is a new variable, and changes to it wont affect the start_idx in the calling function.
// 1) You're passing a copy of start_idx + 1 to the recursive function. So, inside the recursive call, start_idx is a new variable, and changes to it won't affect the start_idx in the calling function.


// 2) We dont need start_idx -= 1; because start_idx is passed by value, not by reference. So modifying it in the recursive call doesnt affect the callers start_idx. Were already handling the correct index in each recursive call by passing start_idx + 1.
// 2) We don't need start_idx -= 1; because start_idx is passed by value, not by reference. So modifying it in the recursive call doesn't affect the caller's start_idx. We're already handling the correct index in each recursive call by passing start_idx + 1.

```

Expand Down Expand Up @@ -1092,7 +1092,7 @@ private boolean dfs_(char[][] board, int y, int x, int idx, String word, boolean
/** NOTE !!! we update visited on x, y here */
visited[y][x] = true;

int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int[][] dirs = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
/**
* NOTE !!!
*
Expand Down Expand Up @@ -1198,13 +1198,13 @@ class Solution(object):
// V0'
// IDEA : Backtracking
// https://leetcode.com/problems/subsets/editorial/
List<List<Integer>> output = new ArrayList();
List<List<Integer>> output = new ArrayList<>();
int n, k;

public void backtrack(int first, ArrayList<Integer> curr, int[] nums) {
// if the combination is done
if (curr.size() == k) {
output.add(new ArrayList(curr));
output.add(new ArrayList<>(curr));
return;
}
/** NOTE HERE !!!
Expand Down Expand Up @@ -1701,9 +1701,9 @@ private void backtrack(String s, int start, List<String> currentList) {
*
* • This is the base case of the recursion.
*
* • It means: If weve reached the end of the string,
* • It means: "If we've reached the end of the string,
* then the current list of substrings (currentList)
* forms a valid full partition of s into palindromes.
* forms a valid full partition of s into palindromes."
*
* • -> So we add a copy of currentList into
* the final result list partitionRes.
Expand All @@ -1712,9 +1712,9 @@ private void backtrack(String s, int start, List<String> currentList) {
* - Why start == s.length()?
*
* • Because start is the index from which
* were currently trying to partition.
* we're currently trying to partition.
*
* • If start == s.length(), it means weve
* • If start == s.length(), it means we've
* used up all characters in s, and currentList is now a full,
* valid partition.
*/
Expand Down Expand Up @@ -2039,5 +2039,4 @@ private boolean dfs(int crs, Map<Integer, List<Integer>> preMap, Set<Integer> vi
visiting.remove(crs);
preMap.get(crs).clear(); // Clear prerequisites as the course is confirmed to be processed
return true;
}
```
}
12 changes: 6 additions & 6 deletions doc/cheatsheet/bfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ class Solution:
for x, y in q:
# get the distance from a gate
distance = rooms[x][y]+1
directions = [(-1,0), (1,0), (0,-1), (0,1)]
for dx, dy in directions:
dirs = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }
for dx, dy in dirs:
# find the INF around the gate
new_x, new_y = x+dx, y+dy
if 0 <= new_x < row and 0 <= new_y < col and rooms[new_x][new_y] == 2147483647:
Expand Down Expand Up @@ -484,7 +484,7 @@ class Solution:

int space_cnt = 0;
int gete_cnt = 0;
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int[][] dirs = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

int len = rooms.length;
int width = rooms[0].length;
Expand Down Expand Up @@ -1033,7 +1033,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
*
* * You perform a **BFS for every 1** in the matrix.
* * In worst case, you scan the whole matrix **once per 1**.
* * Thats **O(N × M × (N + M))** — very slow for large inputs.
* * That's **O(N × M × (N + M))** — very slow for large inputs.
*
* ---
*
Expand All @@ -1043,7 +1043,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
*
* #### Why this works:
*
* * You flip the problem: instead of asking *how far is this 1 from a 0?*, you ask *how far can each 0 reach a 1?*
* * You flip the problem: instead of asking *"how far is this 1 from a 0?"*, you ask *"how far can each 0 reach a 1?"*
* * When you expand from all 0s **at the same time**, you ensure that **each 1 gets the shortest path to a 0**, because BFS guarantees minimum-distance traversal.
* * Time complexity is **O(N × M)** — each cell is visited only once.
*
Expand All @@ -1057,7 +1057,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
}
}

int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int[][] dirs = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

// 2️⃣ BFS from all 0s
while (!queue.isEmpty()) {
Expand Down
8 changes: 4 additions & 4 deletions doc/cheatsheet/dfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ _is_island(grid, x, y-1, seen);
// V2
// private boolean _is_island_2(char[][] grid, int x, int y, boolean[][] seen) {}

int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int[][] directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

for (int[] dir : directions) {
int newX = x + dir[0];
Expand Down Expand Up @@ -1681,7 +1681,7 @@ private void dfs(int[][] heights, boolean[][] reachable, int y, int x) {

reachable[y][x] = true;

int[][] directions = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int[][] directions = new int[][]{ {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
for (int[] dir : directions) {
int newY = y + dir[0];
int newX = x + dir[1];
Expand Down Expand Up @@ -1771,7 +1771,7 @@ private void reveal_1(char[][] board, int x, int y) {
* and avoid unnecessary recursion.
*
* - 3) board[x][y] != 'E'
* • Avoids re-processing non-‘E’ cells
* • Avoids re-processing non-'E' cells
* • The board can have:
* • 'M' → Mine (already handled separately)
* • 'X' → Clicked mine (game over case)
Expand All @@ -1795,7 +1795,7 @@ private void reveal_1(char[][] board, int x, int y) {
* • Counts 1 mine nearby → Updates board[0][0] = '1'
* • Does NOT recurse further, avoiding unnecessary work.
*
* What If We Didnt Check board[x][y] != 'E'?
* What If We Didn't Check board[x][y] != 'E'?
* • It might try to expand into already processed cells, leading to redundant computations or infinite recursion.
*
*/
Expand Down
6 changes: 3 additions & 3 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public List<List<Integer>> levelOrder(TreeNode root) {
*
* • res is a List<List<Integer>>, where each inner list represents a level of the tree.
* • res.get(depth) retrieves the list at the given depth.
* • .add(curRoot.val) adds the current nodes value to the corresponding depth level.
* • .add(curRoot.val) adds the current node's value to the corresponding depth level.
*
*/

Expand Down Expand Up @@ -1254,7 +1254,7 @@ return true;
```java
// java
// LC 417
public int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
public int[][] DIRECTIONS = new int[][]{ {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
```

### 1-18) Arrays.fill (1 D)
Expand Down Expand Up @@ -1640,7 +1640,7 @@ orderMap[order.charAt(i) - 'a'] = i;

### 🔍 What's happening?

Lets say:
Let's say:
```java
order = "hlabcdefgijkmnopqrstuvwxyz";
```
Expand Down
6 changes: 3 additions & 3 deletions doc/cheatsheet/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,15 @@ class Solution(object):
* -> so we have 3 layer loop as below:
* - i : Iterates over the rows of A (outer loop).
* - j : Iterates over the columns of B (second loop).
* - k : Iterates over the shared dimension (columns of A or rows of B ) to compute the dot product (inner loop).
* - k : Iterates over the "shared dimension" (columns of A or rows of B ) to compute the dot product (inner loop).
*
*
* ->
*
* The Role of the Loops
* 1. Outer loop ( i ): Iterates over the rows of mat1 to calculate each row of the result matrix.
* 2. Middle loop ( j ): Iterates over the columns of mat2 to compute each element in a row of the result matrix.
* 3. Inner loop ( k ): Iterates over the shared dimension to compute the dot product of the i^{th} row of mat1 and the j^{th} column of mat2.
* 3. Inner loop ( k ): Iterates over the "shared dimension" to compute the dot product of the i^{th} row of mat1 and the j^{th} column of mat2.
*
*
* -> Why the Inner Loop ( k ) Exists ?
Expand All @@ -488,7 +488,7 @@ class Solution(object):
public static int[][] multiply(int[][] mat1, int[][] mat2) {
// Edge case: Single element matrices
if (mat1.length == 1 && mat1[0].length == 1 && mat2.length == 1 && mat2[0].length == 1) {
return new int[][]{{mat1[0][0] * mat2[0][0]}};
return new int[][]{ {mat1[0][0] * mat2[0][0]} };
}

int l_1 = mat1.length; // Number of rows in mat1
Expand Down
11 changes: 11 additions & 0 deletions ref_code/CtCI-6th-Edition-master/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1751452257593</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1751452257582</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions ref_code/crack_code/CtCI-6th-Edition-master/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1751452257597</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Loading