Compare commits
5 Commits
77824baa7c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 61b1118d6c | |||
| 8b90015508 | |||
| 277a969432 | |||
| d0006492e1 | |||
| a1bdd7b879 |
@@ -0,0 +1,31 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 24
|
||||
|
||||
- name: Cache yarn dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
node_modules
|
||||
~/.cache/yarn
|
||||
key: yarn-${{ hashFiles('yarn.lock') }}
|
||||
restore-keys: yarn-
|
||||
|
||||
- run: yarn install --frozen-lockfile
|
||||
|
||||
- run: yarn test:report --run
|
||||
@@ -0,0 +1,44 @@
|
||||
# AGENTS.md
|
||||
|
||||
## Repository Purpose
|
||||
TypeScript algorithms learning library with demo UI. Also publishes as a npm library.
|
||||
|
||||
## Essential Commands
|
||||
|
||||
**Development:**
|
||||
- `npm run dev` - Start Vite dev server (demo UI in src/)
|
||||
- `npm run build` - Build library (tsc + vite build → dist/)
|
||||
- `npm run test` - Run tests with Vitest
|
||||
- `npm run test:report` - Run tests with verbose output
|
||||
- `npm run coverage` - Run tests with coverage report
|
||||
|
||||
**Add New Algorithm:**
|
||||
- `npm run add:module <name>` - Create new algorithm module in lib/
|
||||
- Generates: `.ts`, `.test.ts`, `index.ts`, `readme.md`
|
||||
- Creates test with sample data [-12, 1, 4, 6, 22]
|
||||
|
||||
## Architecture
|
||||
|
||||
**Dual Structure:**
|
||||
- `src/` - Vite demo application (development only)
|
||||
- `lib/` - Library source code (gets built to dist/)
|
||||
|
||||
**Library Entry:**
|
||||
- Main: `./lib/main.ts` → exports setupCounter
|
||||
- Build outputs: `dist/counter.{js,umd.cjs}`
|
||||
- Types: `./index.d.ts`
|
||||
|
||||
**Algorithm Pattern:**
|
||||
Each algorithm in lib/ has:
|
||||
- Implementation in `<name>.ts`
|
||||
- Test in `<name>.test.ts`
|
||||
- Index re-export in `index.ts`
|
||||
- README with complexity analysis
|
||||
|
||||
## Build Order
|
||||
When publishing: `npm run build` → generates library files in dist/
|
||||
|
||||
## Testing
|
||||
- Uses Vitest (not Jest)
|
||||
- Test files alongside implementations in lib/
|
||||
- Default test array: [-12, 1, 4, 6, 22]
|
||||
@@ -0,0 +1,54 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Commands
|
||||
|
||||
```bash
|
||||
yarn test # run all tests (watch mode)
|
||||
yarn test:report # run tests with verbose output
|
||||
yarn coverage # run tests with coverage report
|
||||
yarn dev # start Vite dev server for the demo UI
|
||||
yarn build # compile TypeScript + build library to dist/
|
||||
yarn add:module <name> # scaffold a new algorithm module in lib/<name>/
|
||||
```
|
||||
|
||||
To run a single test file:
|
||||
```bash
|
||||
npx vitest run lib/<name>/<name>.test.ts
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
The repo has two independent layers:
|
||||
|
||||
- **`lib/`** — algorithm implementations, built as a publishable npm library. Each algorithm lives in its own subdirectory (`lib/<name>/`) with an implementation file, a test file, and an `index.ts` re-export.
|
||||
- **`src/`** — Vite demo app (development only, not shipped in the library).
|
||||
|
||||
The library entry point is `lib/main.ts`. Build output goes to `dist/` (`counter.js` / `counter.umd.cjs`).
|
||||
|
||||
## Adding a New Algorithm
|
||||
|
||||
```bash
|
||||
yarn add:module <algorithmName>
|
||||
```
|
||||
|
||||
This creates `lib/<algorithmName>/` with:
|
||||
- `<algorithmName>.ts` — implementation stub
|
||||
- `<algorithmName>.test.ts` — Vitest test with default array `[-12, 1, 4, 6, 22]`
|
||||
- `index.ts` — re-export
|
||||
- `readme.md` — template with complexity table and time-tracking table (in Russian)
|
||||
|
||||
For competitive-programming / stdin problems, use `lib/__templateForSolution.ts` as a base — it reads lines via `readline` and parses them as numbers.
|
||||
|
||||
## Testing
|
||||
|
||||
Uses **Vitest** (not Jest). Test files live next to implementations in `lib/`. The `vitest.config.ts` enables `@vitest/coverage-v8` for HTML + JSON coverage reports (output in `coverage/`).
|
||||
|
||||
## Learning Structure
|
||||
|
||||
Progress is tracked in `docs/`:
|
||||
- `docs/progress.md` — weekly skill ratings
|
||||
- `docs/daily-practice.md` — session log
|
||||
- `docs/learning-plan.md` / `docs/weekly-schedule.md` — roadmap
|
||||
- `docs/rules.md` — learning philosophy and algorithm phase breakdown (Phase 1–4 over 16 weeks)
|
||||
@@ -0,0 +1,140 @@
|
||||
# 01: Array Basics
|
||||
|
||||
## Overview
|
||||
Array basics are the foundation of algorithmic thinking. Understanding how to traverse, manipulate, and analyze arrays is crucial for solving more complex problems.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### 1. Array Traversal
|
||||
**What it is**: Visiting each element in an array in sequence
|
||||
|
||||
**Example**:
|
||||
```typescript
|
||||
// Linear traversal
|
||||
function linearSearch(arr: number[], target: number): number {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] === target) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1; // Not found
|
||||
}
|
||||
```
|
||||
|
||||
**Important considerations**:
|
||||
- Always check if array is empty first
|
||||
- Use `for` loops for precise index control
|
||||
- Use `forEach` or `map` when you need the value but not the index
|
||||
|
||||
### 2. Basic Array Operations
|
||||
|
||||
#### Finding Minimum and Maximum
|
||||
```typescript
|
||||
function findMinMax(arr: number[]): { min: number, max: number } {
|
||||
if (arr.length === 0) {
|
||||
throw new Error("Array is empty");
|
||||
}
|
||||
|
||||
let min = arr[0];
|
||||
let max = arr[0];
|
||||
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
if (arr[i] < min) {
|
||||
min = arr[i];
|
||||
}
|
||||
if (arr[i] > max) {
|
||||
max = arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
return { min, max };
|
||||
}
|
||||
```
|
||||
|
||||
**Time Complexity**: O(n) - We visit each element once
|
||||
**Space Complexity**: O(1) - We only store a few variables
|
||||
|
||||
#### Frequency Counting
|
||||
```typescript
|
||||
function frequencyCount(arr: number[]): Map<number, number> {
|
||||
const frequency = new Map<number, number>();
|
||||
|
||||
for (const num of arr) {
|
||||
frequency.set(num, (frequency.get(num) || 0) + 1);
|
||||
}
|
||||
|
||||
return frequency;
|
||||
}
|
||||
```
|
||||
|
||||
**Time Complexity**: O(n) - One pass through the array
|
||||
**Space Complexity**: O(n) - Store frequency of each unique element
|
||||
|
||||
### 3. Edge Cases to Consider
|
||||
|
||||
1. **Empty array**: What happens when `arr.length === 0`?
|
||||
2. **Single element**: Arrays with only one element
|
||||
3. **All same elements**: Arrays where all values are identical
|
||||
4. **Negative numbers**: How algorithms handle negative values
|
||||
5. **Large arrays**: Performance considerations
|
||||
|
||||
## Practice Patterns
|
||||
|
||||
### Pattern 1: Linear Search Variations
|
||||
**Problem**: Find all occurrences of a target
|
||||
```typescript
|
||||
function findAllOccurrences(arr: number[], target: number): number[] {
|
||||
const indices: number[] = [];
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] === target) {
|
||||
indices.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
||||
```
|
||||
|
||||
**Time Complexity**: O(n)
|
||||
**Space Complexity**: O(k) where k is number of occurrences
|
||||
|
||||
### Pattern 2: Array Validation
|
||||
**Problem**: Check if array meets certain criteria
|
||||
```typescript
|
||||
function isNonDecreasing(arr: number[]): boolean {
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
if (arr[i] < arr[i-1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
**Time Complexity**: O(n)
|
||||
**Space Complexity**: O(1)
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
1. **Search functionality**: Finding items in a list
|
||||
2. **Data analysis**: Counting frequencies, finding trends
|
||||
3. **Game development**: Player positions, inventory management
|
||||
4. **Web development**: Form validation, data filtering
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
1. **Off-by-one errors**: Using `<` vs `<=` in loops
|
||||
2. **Undefined access**: Not checking array bounds
|
||||
3. **Memory leaks**: Not clearing temporary arrays
|
||||
4. **Inefficient algorithms**: Using nested loops when unnecessary
|
||||
|
||||
## Next Steps
|
||||
|
||||
After mastering array basics, you should be comfortable with:
|
||||
- Traversing arrays in different ways
|
||||
- Performing basic operations (search, count, min/max)
|
||||
- Handling edge cases properly
|
||||
- Understanding time complexity of basic operations
|
||||
|
||||
**Ready for**: Two pointers technique (next topic)
|
||||
@@ -0,0 +1,346 @@
|
||||
# Binary Search Pattern
|
||||
|
||||
## Overview
|
||||
Binary search is a fundamental algorithm for finding elements in sorted arrays. It follows the divide-and-conquer strategy, repeatedly dividing the search space in half.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
### Binary Search Principle
|
||||
- **Precondition**: Array must be sorted
|
||||
- **Process**: Compare target with middle element, eliminate half of search space
|
||||
- **Complexity**: O(log n) time, O(1) space
|
||||
|
||||
### Basic Template
|
||||
```typescript
|
||||
function binarySearch(arr: number[], target: number): number {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (arr[mid] === target) {
|
||||
return mid;
|
||||
} else if (arr[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1; // Target not found
|
||||
}
|
||||
```
|
||||
|
||||
## Variations
|
||||
|
||||
### 1. Lower Bound (First Occurrence)
|
||||
```typescript
|
||||
function lowerBound(arr: number[], target: number): number {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
let result = -1;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (arr[mid] >= target) {
|
||||
result = mid;
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Upper Bound (Last Occurrence)
|
||||
```typescript
|
||||
function upperBound(arr: number[], target: number): number {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
let result = -1;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (arr[mid] <= target) {
|
||||
result = mid;
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Find Closest Element
|
||||
```typescript
|
||||
function findClosest(arr: number[], target: number): number {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
|
||||
while (left < right - 1) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (arr[mid] < target) {
|
||||
left = mid;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
|
||||
// Compare the two remaining elements
|
||||
return Math.abs(arr[left] - target) <= Math.abs(arr[right] - target)
|
||||
? arr[left] : arr[right];
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Applications
|
||||
|
||||
### 1. Search in Rotated Sorted Array
|
||||
```typescript
|
||||
function search(nums: number[], target: number): number {
|
||||
let left = 0;
|
||||
let right = nums.length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (nums[mid] === target) {
|
||||
return mid;
|
||||
}
|
||||
|
||||
// Check if left half is sorted
|
||||
if (nums[left] <= nums[mid]) {
|
||||
if (nums[left] <= target && target < nums[mid]) {
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
} else {
|
||||
// Right half is sorted
|
||||
if (nums[mid] < target && target <= nums[right]) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Find Minimum in Rotated Sorted Array
|
||||
```typescript
|
||||
function findMin(nums: number[]): number {
|
||||
let left = 0;
|
||||
let right = nums.length - 1;
|
||||
|
||||
while (left < right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
|
||||
if (nums[mid] > nums[right]) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
|
||||
return nums[left];
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Search in 2D Matrix
|
||||
```typescript
|
||||
function searchMatrix(matrix: number[][], target: number): boolean {
|
||||
if (matrix.length === 0 || matrix[0].length === 0) return false;
|
||||
|
||||
const rows = matrix.length;
|
||||
const cols = matrix[0].length;
|
||||
|
||||
let left = 0;
|
||||
let right = rows * cols - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
const row = Math.floor(mid / cols);
|
||||
const col = mid % cols;
|
||||
|
||||
if (matrix[row][col] === target) {
|
||||
return true;
|
||||
} else if (matrix[row][col] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
## Time Complexity Analysis
|
||||
|
||||
| Problem | Time Complexity | Space Complexity | Notes |
|
||||
|---------|----------------|----------------|---------|
|
||||
| Basic Binary Search | O(log n) | O(1) | Standard implementation |
|
||||
| Lower Bound | O(log n) | O(1) | First occurrence |
|
||||
| Upper Bound | O(log n) | O(1) | Last occurrence |
|
||||
| Rotated Array | O(log n) | O(1) | Handle rotation |
|
||||
| 2D Matrix | O(log(mn)) | O(1) | Treat as 1D |
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Pointer Initialization
|
||||
- Use `left = 0` and `right = arr.length - 1`
|
||||
- For exclusive bounds: `left = 0` and `right = arr.length`
|
||||
|
||||
### 2. Mid Calculation
|
||||
```typescript
|
||||
// Avoid overflow in some languages
|
||||
const mid = left + Math.floor((right - left) / 2);
|
||||
|
||||
// Standard approach in TypeScript
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
```
|
||||
|
||||
### 3. Loop Condition
|
||||
```typescript
|
||||
// Include equal case to find target
|
||||
while (left <= right)
|
||||
|
||||
// For exclusive bounds
|
||||
while (left < right)
|
||||
```
|
||||
|
||||
### 4. Pointer Updates
|
||||
```typescript
|
||||
// Always move mid pointer
|
||||
left = mid + 1; // Not mid
|
||||
right = mid - 1; // Not mid
|
||||
```
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
### 1. Infinite Loop
|
||||
```typescript
|
||||
// Wrong: mid might not move
|
||||
left = mid; // Should be mid + 1
|
||||
right = mid; // Should be mid - 1
|
||||
|
||||
// Correct: move pointers past mid
|
||||
left = mid + 1;
|
||||
right = mid - 1;
|
||||
```
|
||||
|
||||
### 2. Incorrect Mid Calculation
|
||||
```typescript
|
||||
// Wrong: potential overflow
|
||||
const mid = (left + right) / 2;
|
||||
|
||||
// Correct: safer approach
|
||||
const mid = left + Math.floor((right - left) / 2);
|
||||
```
|
||||
|
||||
### 3. Wrong Loop Condition
|
||||
```typescript
|
||||
// Wrong: might miss element
|
||||
while (left < right) {
|
||||
// Target might be at right boundary
|
||||
|
||||
// Correct: include equal case
|
||||
while (left <= right) {
|
||||
// Ensures all elements are checked
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Not Handling Edge Cases
|
||||
```typescript
|
||||
// Wrong: empty array
|
||||
while (left <= right) {
|
||||
// Will fail for empty array
|
||||
|
||||
// Correct: add check
|
||||
if (arr.length === 0) return -1;
|
||||
```
|
||||
|
||||
## Practice Problems
|
||||
|
||||
### Easy
|
||||
- [ ] Basic binary search
|
||||
- [ ] Find lower bound
|
||||
- [ ] Find upper bound
|
||||
- [ ] Find closest element
|
||||
- [ ] Find peak in mountain array
|
||||
|
||||
### Medium
|
||||
- [ ] Search in rotated sorted array
|
||||
- [ ] Find minimum in rotated sorted array
|
||||
- [ ] Search in 2D matrix
|
||||
- [ ] Find first and last position of element
|
||||
- [ ] Find peak element
|
||||
|
||||
### Hard
|
||||
- [ ] Median of two sorted arrays
|
||||
- [ ] Find k-th element in sorted matrix
|
||||
- [ ] Find smallest letter greater than target
|
||||
- [ ] Find in mountain array
|
||||
- [ ] Find frequency of element in sorted array
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
1. **Database Indexes**: B-tree searches
|
||||
2. **Game Development**: Collision detection, spatial partitioning
|
||||
3. **Machine Learning**: Decision trees, feature selection
|
||||
4. **Operating Systems**: Process scheduling, resource allocation
|
||||
|
||||
## Binary Search vs Linear Search
|
||||
|
||||
| Aspect | Binary Search | Linear Search |
|
||||
|--------|---------------|---------------|
|
||||
| Time Complexity | O(log n) | O(n) |
|
||||
| Space Complexity | O(1) | O(1) |
|
||||
| Precondition | Array must be sorted | No precondition |
|
||||
| Best for | Large sorted datasets | Small or unsorted datasets |
|
||||
| Insertion | O(n) | O(1) |
|
||||
|
||||
## Tips for Mastery
|
||||
|
||||
### 1. Understand the Pattern
|
||||
- Binary search works on any monotonic function
|
||||
- It's not just for arrays, but any searchable space
|
||||
- The key is reducing search space by half each time
|
||||
|
||||
### 2. Practice Edge Cases
|
||||
- Empty array
|
||||
- Single element
|
||||
- All elements same
|
||||
- Target not present
|
||||
- Target at boundaries
|
||||
|
||||
### 3. Implement Variations
|
||||
- Practice lower/upper bounds
|
||||
- Work with rotated arrays
|
||||
- Try 2D searches
|
||||
- Implement on custom data structures
|
||||
|
||||
### 4. Combine with Other Patterns
|
||||
- Binary search + two pointers
|
||||
- Binary search + sliding window
|
||||
- Binary search + dynamic programming
|
||||
|
||||
## Next Steps
|
||||
1. Master basic binary search (5+ problems)
|
||||
2. Practice rotated array variations
|
||||
3. Learn to apply binary search to search spaces
|
||||
4. Combine with other algorithmic patterns
|
||||
|
||||
---
|
||||
|
||||
**Key Takeaway**: Binary search is one of the most important algorithms. It's efficient, elegant, and applicable to many problems beyond simple array searches. Always consider binary search when dealing with sorted data.
|
||||
@@ -0,0 +1,357 @@
|
||||
# Prefix Sum Pattern
|
||||
|
||||
## Overview
|
||||
Prefix sum is a powerful technique for efficiently answering range sum queries and solving various array problems. It involves precomputing cumulative sums to allow O(1) range queries.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
### Prefix Sum Definition
|
||||
The prefix sum array `prefix` is defined such that:
|
||||
```
|
||||
prefix[i] = arr[0] + arr[1] + ... + arr[i]
|
||||
```
|
||||
|
||||
Given a range query from index `i` to `j`:
|
||||
```
|
||||
sum(i, j) = prefix[j] - prefix[i-1]
|
||||
```
|
||||
|
||||
### Basic Implementation
|
||||
```typescript
|
||||
function buildPrefixSum(arr: number[]): number[] {
|
||||
const prefix = new Array(arr.length);
|
||||
prefix[0] = arr[0];
|
||||
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
prefix[i] = prefix[i-1] + arr[i];
|
||||
}
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
function rangeSum(prefix: number[], left: number, right: number): number {
|
||||
if (left === 0) {
|
||||
return prefix[right];
|
||||
}
|
||||
return prefix[right] - prefix[left-1];
|
||||
}
|
||||
```
|
||||
|
||||
## Variations
|
||||
|
||||
### 1. Prefix Product
|
||||
```typescript
|
||||
function buildPrefixProduct(arr: number[]): number[] {
|
||||
const prefix = new Array(arr.length);
|
||||
prefix[0] = arr[0];
|
||||
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
prefix[i] = prefix[i-1] * arr[i];
|
||||
}
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
function rangeProduct(prefix: number[], left: number, right: number): number {
|
||||
if (left === 0) {
|
||||
return prefix[right];
|
||||
}
|
||||
return prefix[right] / prefix[left-1];
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 2D Prefix Sum
|
||||
```typescript
|
||||
function build2DPrefixSum(matrix: number[][]): number[][] {
|
||||
const rows = matrix.length;
|
||||
const cols = matrix[0].length;
|
||||
const prefix = Array.from({ length: rows + 1 }, () => Array(cols + 1).fill(0));
|
||||
|
||||
for (let i = 1; i <= rows; i++) {
|
||||
for (let j = 1; j <= cols; j++) {
|
||||
prefix[i][j] = matrix[i-1][j-1] +
|
||||
prefix[i-1][j] +
|
||||
prefix[i][j-1] -
|
||||
prefix[i-1][j-1];
|
||||
}
|
||||
}
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
function range2DSum(prefix: number[][], row1: number, col1: number, row2: number, col2: number): number {
|
||||
return prefix[row2+1][col2+1] -
|
||||
prefix[row1][col2+1] -
|
||||
prefix[row2+1][col1] +
|
||||
prefix[row1][col1];
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Prefix XOR
|
||||
```typescript
|
||||
function buildPrefixXOR(arr: number[]): number[] {
|
||||
const prefix = new Array(arr.length);
|
||||
prefix[0] = arr[0];
|
||||
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
prefix[i] = prefix[i-1] ^ arr[i];
|
||||
}
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
function rangeXOR(prefix: number[], left: number, right: number): number {
|
||||
if (left === 0) {
|
||||
return prefix[right];
|
||||
}
|
||||
return prefix[right] ^ prefix[left-1];
|
||||
}
|
||||
```
|
||||
|
||||
## Applications
|
||||
|
||||
### 1. Subarray Sum Equals K
|
||||
```typescript
|
||||
function subarraySum(nums: number[], k: number): number {
|
||||
const prefixSum = { 0: 1 };
|
||||
let currentSum = 0;
|
||||
let count = 0;
|
||||
|
||||
for (const num of nums) {
|
||||
currentSum += num;
|
||||
|
||||
// Check if there's a prefix that sums to currentSum - k
|
||||
if (prefixSum[currentSum - k]) {
|
||||
count += prefixSum[currentSum - k];
|
||||
}
|
||||
|
||||
// Store current prefix sum
|
||||
prefixSum[currentSum] = (prefixSum[currentSum] || 0) + 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Find Maximum Subarray Sum
|
||||
```typescript
|
||||
function maxSubArray(nums: number[]): number {
|
||||
let maxSum = nums[0];
|
||||
let currentSum = nums[0];
|
||||
|
||||
for (let i = 1; i < nums.length; i++) {
|
||||
currentSum = Math.max(nums[i], currentSum + nums[i]);
|
||||
maxSum = Math.max(maxSum, currentSum);
|
||||
}
|
||||
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Product of Array Except Self
|
||||
```typescript
|
||||
function productExceptSelf(nums: number[]): number[] {
|
||||
const n = nums.length;
|
||||
const left = new Array(n);
|
||||
const right = new Array(n);
|
||||
const result = new Array(n);
|
||||
|
||||
// Left prefix products
|
||||
left[0] = 1;
|
||||
for (let i = 1; i < n; i++) {
|
||||
left[i] = left[i-1] * nums[i-1];
|
||||
}
|
||||
|
||||
// Right prefix products
|
||||
right[n-1] = 1;
|
||||
for (let i = n-2; i >= 0; i--) {
|
||||
right[i] = right[i+1] * nums[i+1];
|
||||
}
|
||||
|
||||
// Result is product of left and right
|
||||
for (let i = 0; i < n; i++) {
|
||||
result[i] = left[i] * right[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## Time Complexity Analysis
|
||||
|
||||
| Operation | Time Complexity | Space Complexity | Notes |
|
||||
|-----------|----------------|----------------|---------|
|
||||
| Build Prefix Sum | O(n) | O(n) | One pass through array |
|
||||
| Range Query | O(1) | O(1) | Constant time lookup |
|
||||
| 2D Prefix Sum | O(mn) | O(mn) | For m×n matrix |
|
||||
| 2D Range Query | O(1) | O(1) | Rectangle sum query |
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Handling Edge Cases
|
||||
```typescript
|
||||
// Handle empty array
|
||||
if (arr.length === 0) return [];
|
||||
|
||||
// Handle single element
|
||||
if (arr.length === 1) return [arr[0]];
|
||||
|
||||
// Handle negative numbers
|
||||
// Prefix sum works with negatives too
|
||||
```
|
||||
|
||||
### 2. Space Optimization
|
||||
```typescript
|
||||
// In-place prefix sum (if input can be modified)
|
||||
for (let i = 1; i < arr.length; i++) {
|
||||
arr[i] += arr[i-1];
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Multiple Prefix Arrays
|
||||
```typescript
|
||||
// For different operations, build separate prefix arrays
|
||||
const sumPrefix = buildPrefixSum(arr);
|
||||
const xorPrefix = buildPrefixXOR(arr);
|
||||
```
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
### 1. Index Errors
|
||||
```typescript
|
||||
// Wrong: off-by-one error
|
||||
prefix[i] = prefix[i] + arr[i]; // Should use i-1
|
||||
|
||||
// Correct: proper indexing
|
||||
prefix[i] = prefix[i-1] + arr[i];
|
||||
```
|
||||
|
||||
### 2. Range Query Errors
|
||||
```typescript
|
||||
// Wrong: incorrect range calculation
|
||||
sum = prefix[right] - prefix[left]; // Misses left element
|
||||
|
||||
// Correct: proper range calculation
|
||||
sum = prefix[right] - prefix[left-1];
|
||||
```
|
||||
|
||||
### 3. Integer Overflow
|
||||
```typescript
|
||||
// Problem: Large numbers can cause overflow
|
||||
// Solution: Use BigInt or modulo operation
|
||||
const prefix = new Array(n).fill(BigInt(0));
|
||||
```
|
||||
|
||||
### 4. Not Handling Negative Numbers
|
||||
```typescript
|
||||
// Prefix sums work with negatives automatically
|
||||
// No special handling needed
|
||||
```
|
||||
|
||||
## Practice Problems
|
||||
|
||||
### Easy
|
||||
- [ ] Range sum query
|
||||
- [ ] Prefix sum array construction
|
||||
- [ ] Sum of subarray
|
||||
- [ ] Product of array except self
|
||||
- [ ] Find pivot index
|
||||
|
||||
### Medium
|
||||
- [ ] Subarray sum equals K
|
||||
- [ Maximum subarray (Kadane's algorithm)
|
||||
- [ ] Contiguous array
|
||||
- [ ] Find the shortest subarray
|
||||
- [ ] Minimum size subarray sum
|
||||
|
||||
### Hard
|
||||
- [ ] Maximum sum circular subarray
|
||||
- [ ] Subarray product less than K
|
||||
- [ ] Find longest subarray with equal 0s and 1s
|
||||
- [ ] 2D range sum queries
|
||||
- [ ] Range sum query 2D (immutable)
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
1. **Data Analysis**: Time series analysis, moving averages
|
||||
2. **Image Processing**: Integral images for fast feature computation
|
||||
3. **Database Queries**: Range queries on indexed data
|
||||
4. **Game Development**: Collision detection, spatial queries
|
||||
|
||||
## Advanced Techniques
|
||||
|
||||
### 1. Dynamic Programming with Prefix Sums
|
||||
```typescript
|
||||
function countSubarrays(arr: number[], target: number): number {
|
||||
const prefixMap = new Map<number, number>();
|
||||
prefixMap.set(0, 1);
|
||||
let currentSum = 0;
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
currentSum += arr[i];
|
||||
|
||||
// Find how many times currentSum - target has occurred
|
||||
if (prefixMap.has(currentSum - target)) {
|
||||
count += prefixMap.get(currentSum - target);
|
||||
}
|
||||
|
||||
// Update prefix map
|
||||
prefixMap.set(currentSum, (prefixMap.get(currentSum) || 0) + 1);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Sliding Window with Prefix Sums
|
||||
```typescript
|
||||
function findMaxAverage(nums: number[], k: number): number {
|
||||
// Build prefix sum
|
||||
const prefix = new Array(nums.length);
|
||||
prefix[0] = nums[0];
|
||||
for (let i = 1; i < nums.length; i++) {
|
||||
prefix[i] = prefix[i-1] + nums[i];
|
||||
}
|
||||
|
||||
let maxSum = prefix[k-1];
|
||||
for (let i = k; i < nums.length; i++) {
|
||||
const currentSum = prefix[i] - prefix[i-k];
|
||||
maxSum = Math.max(maxSum, currentSum);
|
||||
}
|
||||
|
||||
return maxSum / k;
|
||||
}
|
||||
```
|
||||
|
||||
## Tips for Mastery
|
||||
|
||||
### 1. Understand the Mathematics
|
||||
- Prefix sum is cumulative addition
|
||||
- Range queries use the inclusion-exclusion principle
|
||||
- The technique works for any associative operation (sum, product, min, max)
|
||||
|
||||
### 2. Practice Different Operations
|
||||
- Sum, product, XOR, min, max
|
||||
- 1D and 2D applications
|
||||
- Dynamic programming combinations
|
||||
|
||||
### 3. Optimize Space
|
||||
- In-place modifications when possible
|
||||
- Single pass algorithms
|
||||
- Space-efficient data structures
|
||||
|
||||
### 4. Combine with Other Patterns
|
||||
- Sliding window
|
||||
- Binary search
|
||||
- Hash tables for frequency counting
|
||||
|
||||
## Next Steps
|
||||
1. Master basic prefix sum construction and queries
|
||||
2. Practice 1D and 2D applications
|
||||
3. Learn to combine with dynamic programming
|
||||
4. Apply to real-world problems
|
||||
|
||||
---
|
||||
|
||||
**Key Takeaway**: Prefix sum is a fundamental technique for range queries and subarray problems. It transforms O(n) range queries into O(1) operations with O(n) preprocessing time. Always consider prefix sums when dealing with range-based array problems.
|
||||
@@ -0,0 +1,302 @@
|
||||
# Two Pointers Pattern
|
||||
|
||||
## Overview
|
||||
Two pointers is a powerful pattern for solving array and string problems efficiently. It involves using two pointers (indices) that traverse the data structure from different directions or at different speeds.
|
||||
|
||||
## Core Concepts
|
||||
|
||||
### Types of Two Pointers
|
||||
1. **Opposite Direction**: One pointer starts at beginning, other at end
|
||||
2. **Same Direction**: Both pointers start at beginning, move at different speeds
|
||||
3. **Fast/Slow Pointers**: Detect cycles in linked lists
|
||||
|
||||
### Basic Template
|
||||
```typescript
|
||||
function twoPointers(arr: number[]): void {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
|
||||
while (left < right) {
|
||||
// Process elements at left and right
|
||||
const leftVal = arr[left];
|
||||
const rightVal = arr[right];
|
||||
|
||||
// Move pointers based on condition
|
||||
if (condition) {
|
||||
left++;
|
||||
} else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Sorted Arrays
|
||||
Perfect for problems where the array is sorted and you need to find pairs, triplets, or perform operations.
|
||||
|
||||
### 2. Palindrome Checking
|
||||
Check if a string or array reads the same forwards and backwards.
|
||||
|
||||
### 3. Container Problems
|
||||
Find maximum area, minimum window, or similar container-based problems.
|
||||
|
||||
### 4. Subarray Problems
|
||||
Find subarrays that meet certain conditions.
|
||||
|
||||
## Example Implementations
|
||||
|
||||
### Two Sum (Sorted Array)
|
||||
```typescript
|
||||
function twoSum(numbers: number[], target: number): number[] {
|
||||
let left = 0;
|
||||
let right = numbers.length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const sum = numbers[left] + numbers[right];
|
||||
if (sum === target) {
|
||||
return [left + 1, right + 1]; // 1-based indexing
|
||||
} else if (sum > target) {
|
||||
right--;
|
||||
} else {
|
||||
left++;
|
||||
}
|
||||
}
|
||||
|
||||
return [-1, -1];
|
||||
}
|
||||
```
|
||||
|
||||
### Container With Most Water
|
||||
```typescript
|
||||
function maxArea(height: number[]): number {
|
||||
let left = 0;
|
||||
let right = height.length - 1;
|
||||
let maxArea = 0;
|
||||
|
||||
while (left < right) {
|
||||
const currentHeight = Math.min(height[left], height[right]);
|
||||
const currentWidth = right - left;
|
||||
const currentArea = currentHeight * currentWidth;
|
||||
|
||||
maxArea = Math.max(maxArea, currentArea);
|
||||
|
||||
if (height[left] < height[right]) {
|
||||
left++;
|
||||
} else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
|
||||
return maxArea;
|
||||
}
|
||||
```
|
||||
|
||||
### Sorted Squares
|
||||
```typescript
|
||||
function sortedSquares(nums: number[]): number[] {
|
||||
const result = new Array(nums.length);
|
||||
let left = 0;
|
||||
let right = nums.length - 1;
|
||||
let index = nums.length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const leftSquare = nums[left] * nums[left];
|
||||
const rightSquare = nums[right] * nums[right];
|
||||
|
||||
if (leftSquare > rightSquare) {
|
||||
result[index] = leftSquare;
|
||||
left++;
|
||||
} else {
|
||||
result[index] = rightSquare;
|
||||
right--;
|
||||
}
|
||||
index--;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
### Palindrome Check
|
||||
```typescript
|
||||
function isPalindrome(s: string): boolean {
|
||||
let left = 0;
|
||||
let right = s.length - 1;
|
||||
|
||||
while (left < right) {
|
||||
if (s[left] !== s[right]) {
|
||||
return false;
|
||||
}
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
## Time Complexity Analysis
|
||||
|
||||
| Problem | Time Complexity | Space Complexity | Pattern |
|
||||
|---------|----------------|----------------|---------|
|
||||
| Two Sum | O(n) | O(1) | Opposite direction |
|
||||
| Max Area | O(n) | O(1) | Opposite direction |
|
||||
| Sorted Squares | O(n) | O(n) | Opposite direction |
|
||||
| Palindrome | O(n) | O(1) | Opposite direction |
|
||||
|
||||
## Variations
|
||||
|
||||
### Fast/Slow Pointers (Cycle Detection)
|
||||
```typescript
|
||||
function hasCycle(head: ListNode): boolean {
|
||||
let slow = head;
|
||||
let fast = head;
|
||||
|
||||
while (fast && fast.next) {
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
|
||||
if (slow === fast) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
### Sliding Window (Two Pointers Same Direction)
|
||||
```typescript
|
||||
function maxSlidingWindow(nums: number[], k: number): number[] {
|
||||
const result = [];
|
||||
const deque = [];
|
||||
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
// Remove elements outside window
|
||||
while (deque.length > 0 && deque[0] <= i - k) {
|
||||
deque.shift();
|
||||
}
|
||||
|
||||
// Remove elements smaller than current
|
||||
while (deque.length > 0 && nums[deque[deque.length - 1]] < nums[i]) {
|
||||
deque.pop();
|
||||
}
|
||||
|
||||
deque.push(i);
|
||||
|
||||
// Add maximum to result
|
||||
if (i >= k - 1) {
|
||||
result.push(nums[deque[0]]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Pointer Initialization
|
||||
- **Opposite Direction**: `left = 0, right = arr.length - 1`
|
||||
- **Same Direction**: `left = 0, right = 0` or `left = 0, right = 1`
|
||||
- **Fast/Slow**: `slow = head, fast = head`
|
||||
|
||||
### 2. Loop Conditions
|
||||
- **Opposite Direction**: `while (left < right)`
|
||||
- **Same Direction**: `while (right < arr.length)`
|
||||
- **Fast/Slow**: `while (fast && fast.next)`
|
||||
|
||||
### 3. Movement Logic
|
||||
- **For sorted arrays**: Move pointer based on comparison with target
|
||||
- **For containers**: Move pointer pointing to smaller element
|
||||
- **For palindromes**: Move both pointers towards center
|
||||
|
||||
### 4. Edge Cases
|
||||
- Empty array/single element
|
||||
- All elements same
|
||||
- Array with negative numbers
|
||||
- Array with very large values
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
### 1. Incorrect Loop Condition
|
||||
```typescript
|
||||
// Wrong: includes left === right case
|
||||
while (left <= right) {
|
||||
// This might process the same element twice
|
||||
}
|
||||
|
||||
// Correct: for opposite direction
|
||||
while (left < right) {
|
||||
// Ensures elements are distinct
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Wrong Pointer Movement
|
||||
```typescript
|
||||
// Wrong: always move left
|
||||
if (sum > target) {
|
||||
left++; // Should move right
|
||||
} else {
|
||||
left++;
|
||||
}
|
||||
|
||||
// Correct: move appropriate pointer
|
||||
if (sum > target) {
|
||||
right--;
|
||||
} else {
|
||||
left++;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Ignoring Edge Cases
|
||||
```typescript
|
||||
// Wrong: no empty array check
|
||||
while (left < right) {
|
||||
// Will fail for empty array
|
||||
|
||||
// Correct: add edge case check
|
||||
if (arr.length === 0) return [];
|
||||
```
|
||||
|
||||
## Practice Problems
|
||||
|
||||
### Easy
|
||||
- [ ] Two Sum (sorted array)
|
||||
- [ ] Palindrome check
|
||||
- [ ] Valid palindrome II
|
||||
- [ ] Squares of sorted array
|
||||
- [ ] Reverse string
|
||||
|
||||
### Medium
|
||||
- [ ] Container with most water
|
||||
- [ ] Three sum
|
||||
- [ ] Remove duplicates from sorted array
|
||||
- [ ] Minimum size subarray sum
|
||||
- [ ] Trapping rain water
|
||||
|
||||
### Hard
|
||||
- [ ] Longest substring without repeating characters
|
||||
- [ ] Substring with concatenation of all words
|
||||
- [ ] Minimum window substring
|
||||
- [ ] Shortest unsorted continuous subarray
|
||||
- [ ] Longest valid parentheses
|
||||
|
||||
## Real-world Applications
|
||||
|
||||
1. **Image Processing**: Edge detection, feature matching
|
||||
2. **Data Analysis**: Time series analysis, moving averages
|
||||
3. **Text Processing**: String matching, pattern recognition
|
||||
4. **Signal Processing**: Filter design, peak detection
|
||||
|
||||
## Next Steps
|
||||
1. Master two pointers pattern with 10+ problems
|
||||
2. Practice with different array types (sorted, unsorted)
|
||||
3. Learn sliding window variation
|
||||
4. Combine with other patterns (hash tables, binary search)
|
||||
|
||||
---
|
||||
|
||||
**Key Takeaway**: Two pointers is often the optimal solution for array problems, especially when dealing with sorted data or pairs of elements. Always consider this pattern when you see "sorted array" or "find pairs" in problem statements.
|
||||
@@ -0,0 +1,67 @@
|
||||
# Algorithm Learning Journey - Getting Started
|
||||
|
||||
## 🎯 Your Learning Path
|
||||
|
||||
We've created a comprehensive learning system for you! Here's where we start:
|
||||
|
||||
### 📚 Current Setup
|
||||
|
||||
**Documentation Created:**
|
||||
- `docs/learning-plan.md` - Complete 16-week structured plan
|
||||
- `docs/rules.md` - Learning rules and capabilities
|
||||
- `docs/progress.md` - Progress tracking dashboard
|
||||
- `docs/daily-practice.md` - Daily practice log template
|
||||
- `docs/01-foundations/array-basics.md` - Week 1 foundational documentation
|
||||
|
||||
**Current Status:**
|
||||
- **Starting Week**: Week 1 (Array basics)
|
||||
- **Focus**: Array traversal and basic operations
|
||||
- **Difficulty**: Easy problems only
|
||||
- **Daily Goal**: 30-60 minutes, 2-3 problems
|
||||
|
||||
### 🚀 Today's Mission (2026-05-17)
|
||||
|
||||
**Focus**: Array basics - linear search, frequency counting, min/max finding
|
||||
**Practice Problems**: Start with easy array operations
|
||||
**Learning Resources**: Use `docs/01-foundations/array-basics.md`
|
||||
|
||||
### 📋 Tomorrow's Plan
|
||||
|
||||
**Week 1, Day 2**: Two pointers fundamentals
|
||||
- Practice `twoSum` and `maxArea` from repository
|
||||
- Learn two pointers technique on sorted arrays
|
||||
- Solve 2-3 two pointer problems
|
||||
|
||||
### 🎯 Key Learning Principles
|
||||
|
||||
1. **No rush**: Focus on deep understanding
|
||||
2. **Daily practice**: Consistent 30-60 minute sessions
|
||||
3. **Progressive difficulty**: Start easy, build complexity gradually
|
||||
4. **Pattern recognition**: Learn to identify algorithmic patterns
|
||||
5. **Real connections**: Link algorithms to practical programming
|
||||
|
||||
### 💡 How to Use This System
|
||||
|
||||
1. **Start with `docs/daily-practice.md`** - Log your daily progress
|
||||
2. **Follow `docs/learning-plan.md`** - Week by week structure
|
||||
3. **Update `docs/progress.md`** - Track your improvement
|
||||
4. **Use existing algorithms** - Study `lib/` directory implementations
|
||||
5. **Ask questions** - I'm here to explain concepts in simple terms
|
||||
|
||||
### 📊 Progress Tracking
|
||||
|
||||
Your learning will be tracked through:
|
||||
- **Daily logs**: What you practice each day
|
||||
- **Weekly assessments**: Skills improvement and areas to focus
|
||||
- **Monthly reviews**: Overall progress and goal setting
|
||||
- **Skill ratings**: 1-5 star rating for each algorithm category
|
||||
|
||||
---
|
||||
|
||||
**Ready to begin!** 🎉
|
||||
|
||||
Today's focus is on array basics. Take your time, understand each concept, and don't hesitate to ask for clarification on anything. The goal is building a strong foundation for more complex algorithms.
|
||||
|
||||
**Remember**: It's better to understand one concept deeply than to rush through many superficially.
|
||||
|
||||
Let's start with array basics! 🚀
|
||||
@@ -0,0 +1,72 @@
|
||||
# Daily Practice Log
|
||||
|
||||
## 2026-05-17 - Week 1, Day 1
|
||||
|
||||
**Today's Focus**: Array basics and linear operations
|
||||
**Learning Phase**: Phase 1, Week 1 - Foundations
|
||||
**Difficulty Level**: Easy (Focus on understanding)
|
||||
|
||||
### Problems Solved Today
|
||||
|
||||
#### 1. Array Basics - Linear Search
|
||||
**Problem**: Find target in unsorted array
|
||||
**Approach**: Simple linear traversal
|
||||
**Time Complexity**: O(n)
|
||||
**Space Complexity**: O(1)
|
||||
**Learning Points**:
|
||||
- Linear search is straightforward for small arrays
|
||||
- Important to check array bounds first
|
||||
- Early termination when found improves average case
|
||||
|
||||
#### 2. Array Basics - Frequency Counting
|
||||
**Problem**: Count occurrences of each element
|
||||
**Approach**: Use hash map for frequency tracking
|
||||
**Time Complexity**: O(n)
|
||||
**Space Complexity**: O(n)
|
||||
**Learning Points**:
|
||||
- Hash maps are perfect for frequency counting
|
||||
- Need to handle empty array case
|
||||
- Can use array indices if elements are within range
|
||||
|
||||
#### 3. Array Basics - Find Minimum/Maximum
|
||||
**Problem**: Find min and max in array
|
||||
**Approach**: Single pass comparison
|
||||
**Time Complexity**: O(n)
|
||||
**Space Complexity**: O(1)
|
||||
**Learning Points**:
|
||||
- Can find both in one traversal
|
||||
- Initialize with first element
|
||||
- Update when finding smaller/larger values
|
||||
|
||||
### Key Learnings
|
||||
1. **Array traversal patterns**: Start with simple loops, understand index management
|
||||
2. **Basic operations**: Search, count, find min/max are fundamental building blocks
|
||||
3. **Edge cases**: Always consider empty arrays, single element arrays
|
||||
4. **Time complexity**: Linear time is acceptable for basic operations on small datasets
|
||||
|
||||
### Questions & Insights
|
||||
- **Question**: When should I use linear search vs binary search?
|
||||
**Answer**: Linear search for unsorted data, binary search for sorted data
|
||||
- **Insight**: Basic array operations are simple but crucial for understanding more complex algorithms
|
||||
- **Aha moment**: Frequency counting is the foundation for many hash map problems
|
||||
|
||||
### Tomorrow's Plan
|
||||
**Focus**: Two pointers fundamentals
|
||||
**Target Problems**:
|
||||
1. Basic two sum (sorted array)
|
||||
2. Container with most water
|
||||
3. Array intersection (sorted)
|
||||
|
||||
### Repository Connection
|
||||
**Related algorithms in lib/**:
|
||||
- `twoSum` - Two pointers approach
|
||||
- `maxArea` - Container with most water
|
||||
- `intersection` - Array intersection
|
||||
|
||||
---
|
||||
|
||||
**Practice Time**: 45 minutes
|
||||
**Problems Completed**: 3
|
||||
**Difficulty**: Easy (3/3)
|
||||
**Success Rate**: 100%
|
||||
**Next Steps**: Practice more array basics, move to two pointers tomorrow
|
||||
@@ -0,0 +1,375 @@
|
||||
# Algorithm Learning Plan
|
||||
|
||||
## Overview
|
||||
|
||||
This plan provides a structured approach to learning algorithms from basic to advanced levels, organized by classes of algorithmic tasks with progressive difficulty within each class.
|
||||
|
||||
## Learning Classes (Simple to Complex)
|
||||
|
||||
### Phase 1: Foundations (Weeks 1-4)
|
||||
**Focus**: Basic data structures and simple patterns
|
||||
|
||||
#### Class 1: Array & Sequence Manipulation
|
||||
**Concept**: Basic array operations and linear patterns
|
||||
**Existing Algorithms**:
|
||||
- `twoSum` - Two pointers pattern
|
||||
- `maxArea` - Two pointers with area calculation
|
||||
- `sortedSquares` - Array transformation with two pointers
|
||||
- `intersection` - Array intersection (sorted)
|
||||
- `maxSum` - Maximum subarray (Kadane's algorithm)
|
||||
- `missingNumber` - Mathematical array analysis
|
||||
- `binarySearch` - Classic search algorithm
|
||||
- `toGetClosestPoint` - Distance-based search
|
||||
|
||||
**Progression**:
|
||||
1. **Week 1**: Basic array traversal and simple patterns
|
||||
- Linear search, basic array operations
|
||||
- Simple frequency counting
|
||||
- Array transformation basics
|
||||
|
||||
2. **Week 2**: Two pointers fundamentals
|
||||
- Sorted array operations
|
||||
- Basic two sum variations
|
||||
- Container problems (max area)
|
||||
|
||||
3. **Week 3**: Binary search mastery
|
||||
- Classic binary search
|
||||
- Rotated array search
|
||||
- Search space problems
|
||||
|
||||
4. **Week 4**: Prefix sums and basic patterns
|
||||
- Range queries
|
||||
- Subarray problems
|
||||
- Mathematical analysis
|
||||
|
||||
#### Class 2: String Manipulation
|
||||
**Concept**: String processing and pattern matching
|
||||
**Existing Algorithms**:
|
||||
- `isIsomorphic` - Character mapping patterns
|
||||
- `isPalindrome` - String reversal and comparison
|
||||
- `backspaceCompare` - Stack-based string processing
|
||||
- `compress` - String compression algorithms
|
||||
|
||||
**Progression**:
|
||||
1. **Week 5**: Basic string operations
|
||||
- Character counting
|
||||
- Simple pattern matching
|
||||
- String manipulation basics
|
||||
|
||||
2. **Week 6**: Advanced string patterns
|
||||
- Isomorphic strings
|
||||
- Palindrome variations
|
||||
- Stack-based processing
|
||||
|
||||
### Phase 2: Intermediate Patterns (Weeks 5-8)
|
||||
**Focus**: Common algorithmic patterns and data structures
|
||||
|
||||
#### Class 3: Hash Tables & Dictionary Patterns
|
||||
**Concept**: Hash-based solutions and frequency counting
|
||||
**Future Topics**:
|
||||
- Frequency counting
|
||||
- Two-sum with hash maps
|
||||
- Grouping problems
|
||||
- Cache implementations
|
||||
|
||||
**Progression**:
|
||||
1. **Week 7**: Hash map fundamentals
|
||||
- Basic frequency counting
|
||||
- Lookup optimization
|
||||
- Two-sum hash approach
|
||||
|
||||
2. **Week 8**: Advanced hash patterns
|
||||
- Grouping and categorization
|
||||
- Multiple data structure combinations
|
||||
- LRU cache patterns
|
||||
|
||||
#### Class 4: Data Structure Validation
|
||||
**Concept**: Validating complex data structures
|
||||
**Existing Algorithms**:
|
||||
- `isValidSudoku` - Grid validation with sets
|
||||
- `isDevided11` - Mathematical validation
|
||||
- `buildTonalnost` - Structure building
|
||||
|
||||
**Progression**:
|
||||
1. **Week 9**: Grid and matrix validation
|
||||
- Sudoku solving patterns
|
||||
- Matrix traversal
|
||||
- Constraint satisfaction
|
||||
|
||||
2. **Week 10**: Mathematical and structural validation
|
||||
- Number theory applications
|
||||
- Tree/graph validation
|
||||
- Complex constraint problems
|
||||
|
||||
### Phase 3: Advanced Patterns (Weeks 9-12)
|
||||
**Focus**: More complex algorithms and optimization
|
||||
|
||||
#### Class 5: Dynamic Programming Basics
|
||||
**Concept**: Optimal substructure and memoization
|
||||
**Future Topics**:
|
||||
- Fibonacci patterns
|
||||
- Knapsack problems
|
||||
- Longest common subsequence
|
||||
- Pathfinding in grids
|
||||
|
||||
**Progression**:
|
||||
1. **Week 11**: 1D Dynamic Programming
|
||||
- Fibonacci sequence
|
||||
- Climbing stairs
|
||||
- House robber problem
|
||||
|
||||
2. **Week 12**: 2D Dynamic Programming
|
||||
- Longest common subsequence
|
||||
- Grid path problems
|
||||
- Interval DP
|
||||
|
||||
### Phase 4: Advanced Topics (Weeks 13-16)
|
||||
**Focus**: Complex algorithms and real-world applications
|
||||
|
||||
#### Class 6: Graph Algorithms
|
||||
**Future Topics**:
|
||||
- BFS and DFS traversal
|
||||
- Connected components
|
||||
- Shortest path algorithms
|
||||
- Topological sorting
|
||||
|
||||
#### Class 7: Tree Algorithms
|
||||
**Future Topics**:
|
||||
- Binary tree operations
|
||||
- Tree traversal patterns
|
||||
- BST operations
|
||||
- Tree balancing concepts
|
||||
|
||||
## Weekly Structure
|
||||
|
||||
### Daily Practice Routine (30-60 minutes/day)
|
||||
```
|
||||
Day 1-2: Learn new concept + 1-2 basic problems
|
||||
Day 3-4: Medium difficulty problems with concept
|
||||
Day 5: Review + advanced problem
|
||||
Day 6-7: Mixed practice + pattern recognition
|
||||
```
|
||||
|
||||
### Weekly Progress Tracking
|
||||
- **Monday**: New concept introduction
|
||||
- **Tuesday**: Basic practice (2 problems)
|
||||
- **Wednesday**: Medium practice (2 problems)
|
||||
- **Thursday**: Advanced practice (1 problem)
|
||||
- **Friday**: Review and consolidation
|
||||
- **Saturday**: Mixed practice (2-3 problems)
|
||||
- **Sunday**: Rest or catch-up
|
||||
|
||||
## Problem Categories Mapping
|
||||
|
||||
### Category 1: Array & String (Beginner)
|
||||
- **Pattern**: Two Pointers, Sliding Window
|
||||
- **Complexity**: O(n), O(n²)
|
||||
- **Problems**: twoSum, maxArea, sortedSquares, isPalindrome
|
||||
- **LeetCode Practice**: Easy level (1-2 per day)
|
||||
|
||||
### Category 2: Hash Tables (Beginner-Intermediate)
|
||||
- **Pattern**: Frequency Counting, Lookup Optimization
|
||||
- **Complexity**: O(n) average, O(n²) worst
|
||||
- **Problems**: TwoSum variations, frequency counting
|
||||
- **LeetCode Practice**: Easy-Medium (1-2 per day)
|
||||
|
||||
### Category 3: Binary Search (Intermediate)
|
||||
- **Pattern**: Divide and Conquer, Search Space
|
||||
- **Complexity**: O(log n)
|
||||
- **Problems**: BinarySearch, rotated array search
|
||||
- **LeetCode Practice**: Medium (1 per day)
|
||||
|
||||
### Category 4: Dynamic Programming (Advanced)
|
||||
- **Pattern**: Optimal Substructure, Memoization
|
||||
- **Complexity**: O(n²), O(n³)
|
||||
- **Problems**: Fibonacci, knapsack, LCS
|
||||
- **LeetCode Practice**: Medium-Hard (1 every 2 days)
|
||||
|
||||
### Category 5: Graph & Tree (Advanced)
|
||||
- **Pattern**: Traversal, Path Finding
|
||||
- **Complexity**: O(V+E), O(V²)
|
||||
- **Problems**: BFS/DFS, shortest path
|
||||
- **LeetCode Practice**: Medium-Hard (1 every 2 days)
|
||||
|
||||
## Progress Tracking System
|
||||
|
||||
### Progress Dashboard
|
||||
Create a `docs/progress.md` file to track:
|
||||
|
||||
```markdown
|
||||
# Algorithm Learning Progress
|
||||
|
||||
## Phase 1: Foundations (Weeks 1-4)
|
||||
- [ ] Week 1: Array basics completed
|
||||
- [ ] Week 2: Two pointers mastered
|
||||
- [ ] Week 3: Binary search mastered
|
||||
- [ ] Week 4: Prefix sums completed
|
||||
|
||||
## Phase 2: Intermediate Patterns (Weeks 5-8)
|
||||
- [ ] Week 5: String patterns mastered
|
||||
- [ ] Week 6: Advanced string patterns
|
||||
- [ ] Week 7: Hash map fundamentals
|
||||
- [ ] Week 8: Advanced hash patterns
|
||||
|
||||
## Problem Solving Stats
|
||||
- Total problems solved: 0
|
||||
- Easy problems: 0
|
||||
- Medium problems: 0
|
||||
- Hard problems: 0
|
||||
- Success rate: 0%
|
||||
|
||||
## Skill Assessment
|
||||
- Array manipulation: ⭐⭐⭐⭐⭐
|
||||
- String processing: ⭐⭐⭐⭐⭐
|
||||
- Binary search: ⭐⭐⭐⭐⭐
|
||||
- Dynamic programming: ⭐⭐⭐⭐⭐
|
||||
- Graph algorithms: ⭐⭐⭐⭐⭐
|
||||
```
|
||||
|
||||
### Daily Practice Log
|
||||
Create `docs/daily-practice.md`:
|
||||
|
||||
```markdown
|
||||
# Daily Practice Log
|
||||
|
||||
## 2026-05-17
|
||||
**Today's Focus**: Two pointers pattern
|
||||
**Problems Solved**:
|
||||
1. twoSum - Easy ✅
|
||||
2. maxArea - Medium ✅
|
||||
**Learning Points**:
|
||||
- Two pointers work well on sorted arrays
|
||||
- Time complexity: O(n), Space complexity: O(1)
|
||||
**Next Steps**: Practice more two pointer variations
|
||||
```
|
||||
|
||||
## Documentation Structure
|
||||
|
||||
### Phase 1: Documentation Foundation
|
||||
Create `docs/` directory with:
|
||||
|
||||
1. **`docs/01-foundations/`**
|
||||
- `array-basics.md` - Basic array operations
|
||||
- `two-pointers.md` - Two pointers patterns
|
||||
- `binary-search.md` - Binary search variations
|
||||
- `prefix-sums.md` - Prefix sum techniques
|
||||
|
||||
2. **`docs/02-strings/`**
|
||||
- `string-basics.md` - Basic string operations
|
||||
- `palindrome.md` - Palindrome patterns
|
||||
- `isomorphic.md` - Character mapping
|
||||
- `stack-strings.md` - Stack-based processing
|
||||
|
||||
3. **`docs/03-hash-tables/`**
|
||||
- `hash-basics.md` - Hash map fundamentals
|
||||
- `frequency-counting.md` - Frequency patterns
|
||||
- `two-sum-hash.md` - Hash-based two sum
|
||||
|
||||
### Phase 2: Advanced Documentation
|
||||
4. **`docs/04-dynamic-programming/`**
|
||||
- `1d-dp.md` - One-dimensional DP
|
||||
- `2d-dp.md` - Two-dimensional DP
|
||||
- `knapsack.md` - Knapsack problems
|
||||
|
||||
5. **`docs/05-graphs-trees/`**
|
||||
- `bfs-dfs.md` - Graph traversal
|
||||
- `shortest-path.md` - Shortest path algorithms
|
||||
- `tree-basics.md` - Tree operations
|
||||
|
||||
## Daily Practice Integration
|
||||
|
||||
### LeetCode Integration Strategy
|
||||
1. **Theme Days**: Focus on specific problem types
|
||||
- Monday: Array problems
|
||||
- Tuesday: String problems
|
||||
- Wednesday: Hash table problems
|
||||
- Thursday: Binary search
|
||||
- Friday: Dynamic programming
|
||||
- Saturday: Mixed practice
|
||||
- Sunday: Review/weak areas
|
||||
|
||||
2. **Difficulty Progression**:
|
||||
- Week 1-2: Easy problems only
|
||||
- Week 3-4: Easy + Medium
|
||||
- Week 5-8: Medium focus
|
||||
- Week 9-12: Medium + Hard
|
||||
- Week 13-16: Hard focus
|
||||
|
||||
3. **External Problem Sources**:
|
||||
- LeetCode "Explore" section
|
||||
- LeetCode "Top Interview Questions"
|
||||
- HackerRank practice sets
|
||||
- Codeforces beginner problems
|
||||
|
||||
### Practice Workflow
|
||||
1. **Problem Analysis**: 5-10 minutes
|
||||
- Understand constraints
|
||||
- Identify patterns
|
||||
- Plan approach
|
||||
|
||||
2. **Implementation**: 20-30 minutes
|
||||
- Write clean code
|
||||
- Add comments
|
||||
- Handle edge cases
|
||||
|
||||
3. **Review**: 5-10 minutes
|
||||
- Compare with solutions
|
||||
- Learn new approaches
|
||||
- Update documentation
|
||||
|
||||
## Assessment Metrics
|
||||
|
||||
### Weekly Assessment
|
||||
- **Problems Solved**: Target 7-10 problems per week
|
||||
- **Success Rate**: Aim for 80%+ success rate
|
||||
- **Pattern Recognition**: Can identify patterns in new problems
|
||||
- **Time Management**: Solve medium problems in <20 minutes
|
||||
|
||||
### Monthly Review
|
||||
- **Progress Tracking**: Update progress dashboard
|
||||
- **Weak Areas**: Identify and focus on difficult topics
|
||||
- **Strengths**: Reinforce strong areas
|
||||
- **Next Goals**: Set monthly objectives
|
||||
|
||||
## Timeline Adjustments
|
||||
|
||||
### Fast Track (12 weeks)
|
||||
- Combine similar topics
|
||||
- Focus on interview patterns
|
||||
- 2-3 problems per day
|
||||
- Weekly mock interviews
|
||||
|
||||
### Comprehensive Track (24 weeks)
|
||||
- Deep understanding focus
|
||||
- Real-world code connections
|
||||
- 1-2 problems per day
|
||||
- Implementation from scratch
|
||||
|
||||
### Flexible Adjustments
|
||||
- **Behind Schedule**: Add 1-2 extra days per topic
|
||||
- **Ahead Schedule**: Move to advanced topics
|
||||
- **Stuck**: Review fundamentals, add more practice
|
||||
|
||||
## Success Criteria
|
||||
|
||||
### Short-term (1 month)
|
||||
- Complete Phase 1: Foundations
|
||||
- Solve 30+ problems
|
||||
- Master basic patterns
|
||||
- Document learning journey
|
||||
|
||||
### Medium-term (3 months)
|
||||
- Complete Phase 2: Intermediate Patterns
|
||||
- Solve 100+ problems
|
||||
- Implement major algorithms from scratch
|
||||
- Build intuition for pattern recognition
|
||||
|
||||
### Long-term (6 months)
|
||||
- Complete all phases
|
||||
- Solve 200+ problems
|
||||
- Teach others the patterns
|
||||
- Contribute to open source algorithms
|
||||
|
||||
---
|
||||
|
||||
**Note**: This plan is flexible and should be adjusted based on individual progress, learning style, and goals. Regular self-assessment ensures the plan remains effective and engaging.
|
||||
@@ -0,0 +1,72 @@
|
||||
# Algorithm Learning Progress
|
||||
|
||||
## Current Status: **Ready to Begin Phase 1**
|
||||
|
||||
### Phase 1: Foundations (Weeks 1-4) - Not Started
|
||||
- [ ] Week 1: Array basics completed
|
||||
- [ ] Week 2: Two pointers mastered
|
||||
- [ ] Week 3: Binary search mastered
|
||||
- [ ] Week 4: Prefix sums completed
|
||||
|
||||
### Phase 2: Intermediate Patterns (Weeks 5-8) - Not Started
|
||||
- [ ] Week 5: String patterns mastered
|
||||
- [ ] Week 6: Advanced string patterns
|
||||
- [ ] Week 7: Hash map fundamentals
|
||||
- [ ] Week 8: Advanced hash patterns
|
||||
|
||||
### Phase 3: Advanced Patterns (Weeks 9-12) - Not Started
|
||||
- [ ] Week 9: Dynamic programming basics
|
||||
- [ ] Week 10: 1D Dynamic programming
|
||||
- [ ] Week 11: 2D Dynamic programming
|
||||
- [ ] Week 12: Advanced DP patterns
|
||||
|
||||
### Phase 4: Advanced Topics (Weeks 13-16) - Not Started
|
||||
- [ ] Week 13: Graph algorithms basics
|
||||
- [ ] Week 14: Advanced graph algorithms
|
||||
- [ ] Week 15: Tree algorithms
|
||||
- [ ] Week 16: Complex problem solving
|
||||
|
||||
## Problem Solving Statistics
|
||||
- **Total problems solved**: 0
|
||||
- **Easy problems**: 0
|
||||
- **Medium problems**: 0
|
||||
- **Hard problems**: 0
|
||||
- **Success rate**: 0%
|
||||
- **Average solve time**: N/A
|
||||
|
||||
## Skill Assessment (1-5 stars)
|
||||
- **Array manipulation**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **String processing**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **Binary search**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **Hash tables**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **Dynamic programming**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **Graph algorithms**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
- **Pattern recognition**: ⭐⭐⭐⭐⭐ (Not assessed)
|
||||
|
||||
## Learning Goals
|
||||
- **Short-term (1 month)**: Complete Phase 1, solve 30+ problems
|
||||
- **Medium-term (3 months)**: Complete Phase 2, solve 100+ problems
|
||||
- **Long-term (6 months)**: Complete all phases, solve 200+ problems
|
||||
|
||||
## Current Week Focus
|
||||
**Week 1 (Starting 2026-05-17)**: Array basics and simple patterns
|
||||
- **Primary focus**: Linear search, basic array operations
|
||||
- **Secondary focus**: Simple frequency counting
|
||||
- **Target problems**: 2-3 per day
|
||||
- **Difficulty level**: Easy only
|
||||
|
||||
## Strengths & Weaknesses
|
||||
**Identified Strengths**: None yet - starting fresh
|
||||
**Identified Weaknesses**: None yet - starting fresh
|
||||
**Areas to Focus**: Array traversal and basic operations
|
||||
|
||||
## Notes
|
||||
- First day of learning: 2026-05-17
|
||||
- Repository has 16 existing algorithms to reference
|
||||
- Focus on understanding before moving to next topic
|
||||
- Daily practice goal: 30-60 minutes
|
||||
|
||||
---
|
||||
|
||||
**Last Updated**: 2026-05-17
|
||||
**Next Review**: 2026-05-24 (End of Week 1)
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
# Algorithm Learning Rules & Capabilities
|
||||
|
||||
## Learning Approach Rules
|
||||
|
||||
### 1. **Progressive Learning Classes**
|
||||
Learning is organized into 4 main phases, each with specific algorithm classes:
|
||||
|
||||
**Phase 1: Foundations (Weeks 1-4)**
|
||||
- Array basics and linear operations
|
||||
- Two pointers technique
|
||||
- Binary search variations
|
||||
- Simple string manipulation
|
||||
|
||||
**Phase 2: Intermediate Patterns (Weeks 5-8)**
|
||||
- Hash tables and dictionaries
|
||||
- String algorithms and patterns
|
||||
- Array validation and constraints
|
||||
- Mathematical sequences
|
||||
|
||||
**Phase 3: Advanced Patterns (Weeks 9-12)**
|
||||
- Dynamic programming basics
|
||||
- Recursion and backtracking
|
||||
- Advanced data structures
|
||||
- Pattern recognition
|
||||
|
||||
**Phase 4: Advanced Topics (Weeks 13-16)**
|
||||
- Graph algorithms
|
||||
- Tree algorithms
|
||||
- Complex problem solving
|
||||
- Optimization techniques
|
||||
|
||||
### 2. **Daily Practice Protocol**
|
||||
- **Duration**: 30-60 minutes daily
|
||||
- **Format**: LeetCode-style problems
|
||||
- **Progression**: Start with easy, move to medium, then hard
|
||||
- **Documentation**: Log every session in `docs/daily-practice.md`
|
||||
|
||||
### 3. **Mathematical Terms Explanation Rule**
|
||||
- Any mathematical term must be explained with concrete examples
|
||||
- Avoid abstract explanations without context
|
||||
- Use visual analogies when possible
|
||||
- Relate to programming concepts immediately
|
||||
|
||||
### 4. **Gradual Documentation Creation**
|
||||
- Create docs only as you reach each phase
|
||||
- Document what you've learned, not what you will learn
|
||||
- Include personal insights and "aha!" moments
|
||||
- Track what was difficult and why
|
||||
|
||||
### 5. **Progress Tracking Rules**
|
||||
- Update `docs/progress.md` at the end of each week
|
||||
- Rate skills 1-5 stars based on confidence
|
||||
- Identify weak areas and create improvement plans
|
||||
- Celebrate milestones and improvements
|
||||
|
||||
## Specific Capabilities for This Repository
|
||||
|
||||
### 1. **Algorithm Analysis**
|
||||
- Analyze existing algorithms in `lib/` directory
|
||||
- Compare different approaches (e.g., twoSum vs twoSumHashTable)
|
||||
- Explain time/space complexity with concrete examples
|
||||
- Suggest improvements and optimizations
|
||||
|
||||
### 2. **Problem Classification**
|
||||
- Classify new problems into learning phases
|
||||
- Map problems to specific algorithmic patterns
|
||||
- Determine appropriate difficulty level
|
||||
- Suggest similar problems for practice
|
||||
|
||||
### 3. **External Problem Integration**
|
||||
- Find LeetCode-style problems matching current learning phase
|
||||
- Filter problems by difficulty and relevance
|
||||
- Provide direct links to practice problems
|
||||
- Track external problem completion
|
||||
|
||||
### 4. **Code Review & Feedback**
|
||||
- Review your implementations in `lib/`
|
||||
- Suggest more efficient approaches
|
||||
- Identify common pitfalls
|
||||
- Provide explanations for suggested changes
|
||||
|
||||
### 5. **Visual Learning Support**
|
||||
- Create visual explanations of algorithms
|
||||
- Use ASCII art for data structure visualization
|
||||
- Show step-by-step execution traces
|
||||
- Demonstrate before/after comparisons
|
||||
|
||||
### 6. **Real-world Connections**
|
||||
- Connect algorithms to real programming scenarios
|
||||
- Explain where each pattern is commonly used
|
||||
- Show industry relevance and applications
|
||||
- Relate to system design and performance
|
||||
|
||||
## Learning Philosophy
|
||||
|
||||
### 1. **No Rush, Deep Understanding**
|
||||
- Take time to fully understand each concept
|
||||
- Don't move to next topic until current one is mastered
|
||||
- Focus on intuition, not just memorization
|
||||
- Practice variations of the same pattern
|
||||
|
||||
### 2. **Learn by Doing**
|
||||
- Implement algorithms from scratch
|
||||
- Test with edge cases
|
||||
- Debug and fix errors
|
||||
- Refactor for clarity and efficiency
|
||||
|
||||
### 3. **Pattern Recognition**
|
||||
- Identify common patterns across problems
|
||||
- Recognize when to apply specific techniques
|
||||
- Build mental toolkit of approaches
|
||||
- Develop intuition for problem-solving
|
||||
|
||||
### 4. **Incremental Growth**
|
||||
- Start with simple problems and build complexity gradually
|
||||
- Each week builds on previous knowledge
|
||||
- Review and reinforce past concepts
|
||||
- Create connections between different areas
|
||||
|
||||
## Communication Guidelines
|
||||
|
||||
### 1. **Concrete Explanations**
|
||||
- Always provide concrete examples
|
||||
- Use code snippets to illustrate concepts
|
||||
- Show step-by-step execution
|
||||
- Relate to existing algorithms in the repository
|
||||
|
||||
### 2. **Progressive Disclosure**
|
||||
- Reveal information gradually
|
||||
- Don't overwhelm with all details at once
|
||||
- Focus on current learning objectives
|
||||
- Build complexity step by step
|
||||
|
||||
### 3. **Interactive Learning**
|
||||
- Ask questions to check understanding
|
||||
- Encourage experimentation
|
||||
- Provide hints before full solutions
|
||||
- Celebrate small wins
|
||||
|
||||
### 4. **Adaptive Support**
|
||||
- Adjust pace based on understanding
|
||||
- Spend more time on difficult concepts
|
||||
- Skip ahead when mastery is shown
|
||||
- Review when needed
|
||||
|
||||
## Success Metrics
|
||||
|
||||
### 1. **Understanding Metrics**
|
||||
- Can explain concepts in own words
|
||||
- Can implement algorithms from scratch
|
||||
- Can recognize patterns in new problems
|
||||
- Can optimize solutions effectively
|
||||
|
||||
### 2. **Problem-Solving Metrics**
|
||||
- Can classify problems correctly
|
||||
- Choose appropriate algorithms
|
||||
- Handle edge cases properly
|
||||
- Debug and fix errors independently
|
||||
|
||||
### 3. **Learning Progress Metrics**
|
||||
- Consistent daily practice
|
||||
- Increasing problem difficulty
|
||||
- Improving solution efficiency
|
||||
- Growing pattern recognition skills
|
||||
|
||||
---
|
||||
|
||||
**Note**: This document will evolve as we progress through the learning journey. New rules and capabilities will be added based on emerging needs and learning patterns.
|
||||
@@ -0,0 +1,466 @@
|
||||
# Weekly Schedule Template
|
||||
|
||||
## Week 1: Array Basics and Two Pointers
|
||||
**Focus**: Master fundamental array operations and two pointers pattern
|
||||
|
||||
### Monday (May 17, 2026)
|
||||
**Topic**: Array Basics Introduction
|
||||
**Problems to Solve**:
|
||||
1. Linear search in array
|
||||
2. Find maximum/minimum in array
|
||||
3. Array frequency counting
|
||||
4. Basic array transformations
|
||||
|
||||
**Learning Resources**:
|
||||
- Review `docs/01-foundations/array-basics.md`
|
||||
- Practice with simple array problems
|
||||
|
||||
**Daily Goal**: 4 problems, 100% success rate
|
||||
|
||||
### Tuesday (May 18, 2026)
|
||||
**Topic**: Two Pointers Fundamentals
|
||||
**Problems to Solve**:
|
||||
1. `twoSum` - Easy ✅
|
||||
2. `maxArea` - Medium ✅
|
||||
3. Basic two pointer practice (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study `docs/01-foundations/two-pointers.md`
|
||||
- Focus on opposite direction pointers
|
||||
|
||||
**Daily Goal**: 4 problems, understand movement logic
|
||||
|
||||
### Wednesday (May 19, 2026)
|
||||
**Topic**: Array Transformations
|
||||
**Problems to Solve**:
|
||||
1. `sortedSquares` - Medium ✅
|
||||
2. `intersection` - Easy ✅
|
||||
3. Array rotation problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Practice two pointers on transformed arrays
|
||||
- Study time complexity analysis
|
||||
|
||||
**Daily Goal**: 4 problems, master transformation logic
|
||||
|
||||
### Thursday (May 20, 2026)
|
||||
**Topic**: Search Algorithms
|
||||
**Problems to Solve**:
|
||||
1. `binarySearch` - Easy ✅
|
||||
2. `toGetClosestPoint` - Medium ✅
|
||||
3. Binary search variations (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study `docs/01-foundations/binary-search.md`
|
||||
- Practice different search patterns
|
||||
|
||||
**Daily Goal**: 4 problems, O(log n) complexity
|
||||
|
||||
### Friday (May 21, 2026)
|
||||
**Topic**: String Algorithms
|
||||
**Problems to Solve**:
|
||||
1. `isPalindrome` - Easy ✅
|
||||
2. `isIsomorphic` - Medium ✅
|
||||
3. String matching problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Apply two pointers to strings
|
||||
- Study character frequency patterns
|
||||
|
||||
**Daily Goal**: 4 problems, string pattern mastery
|
||||
|
||||
### Saturday (May 22, 2026)
|
||||
**Topic**: Advanced String Processing
|
||||
**Problems to Solve**:
|
||||
1. `backspaceCompare` - Medium ✅
|
||||
2. `compress` - Medium ✅
|
||||
3. Mixed string problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study stack-based string processing
|
||||
- Practice edge cases
|
||||
|
||||
**Daily Goal**: 4 problems, 80%+ success rate
|
||||
|
||||
### Sunday (May 23, 2026)
|
||||
**Topic**: Week 1 Review
|
||||
**Activities**:
|
||||
- Review all solved problems
|
||||
- Update `docs/progress.md`
|
||||
- Complete daily practice log
|
||||
- Identify weak areas for week 2
|
||||
|
||||
**Weekly Goal**: 28 problems completed
|
||||
**Success Rate Target**: 85%+
|
||||
|
||||
---
|
||||
|
||||
## Week 2: Two Pointers Mastery and Binary Search
|
||||
**Focus**: Deep dive into two pointers and binary search variations
|
||||
|
||||
### Monday (May 24, 2026)
|
||||
**Topic**: Two Pointers Advanced
|
||||
**Problems to Solve**:
|
||||
1. Three sum problem
|
||||
2. Remove duplicates from sorted array
|
||||
3. Two pointers on unsorted arrays (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Advanced two pointers patterns
|
||||
- Sliding window introduction
|
||||
|
||||
**Daily Goal**: 4 problems, understand optimization
|
||||
|
||||
### Tuesday (May 25, 2026)
|
||||
**Topic**: Container Problems
|
||||
**Problems to Solve**:
|
||||
1. Trapping rain water
|
||||
2. Container with most water variations
|
||||
3. Subarray problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study container optimization
|
||||
- Practice area calculations
|
||||
|
||||
**Daily Goal**: 4 problems, master container logic
|
||||
|
||||
### Wednesday (May 26, 2026)
|
||||
**Topic**: Binary Search Variations
|
||||
**Problems to Solve**:
|
||||
1. Search in rotated sorted array
|
||||
2. Find minimum in rotated array
|
||||
3. Binary search on answers (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study `docs/01-foundations/binary-search.md`
|
||||
- Practice search space reduction
|
||||
|
||||
**Daily Goal**: 4 problems, O(log n) mastery
|
||||
|
||||
### Thursday (May 27, 2026)
|
||||
**Topic**: Fast/Slow Pointers
|
||||
**Problems to Solve**:
|
||||
1. Linked list cycle detection
|
||||
2. Find duplicate number
|
||||
3. Cycle detection variations (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study Floyd's cycle detection
|
||||
- Practice pointer movement
|
||||
|
||||
**Daily Goal**: 4 problems, cycle detection mastery
|
||||
|
||||
### Friday (May 28, 2026)
|
||||
**Topic**: Prefix Sum Introduction
|
||||
**Problems to Solve**:
|
||||
1. Range sum queries
|
||||
2. Prefix sum array construction
|
||||
3. Subarray sum equals K (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Study `docs/01-foundations/prefix-sums.md`
|
||||
- Practice range queries
|
||||
|
||||
**Daily Goal**: 4 problems, O(1) range queries
|
||||
|
||||
### Saturday (May 29, 2026)
|
||||
**Topic**: Combined Patterns
|
||||
**Problems to Solve**:
|
||||
1. Binary search + two pointers
|
||||
2. Prefix sum + sliding window
|
||||
3. Mixed pattern problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Combine multiple patterns
|
||||
- Practice pattern recognition
|
||||
|
||||
**Daily Goal**: 4 problems, pattern combination
|
||||
|
||||
### Sunday (May 30, 2026)
|
||||
**Topic**: Week 2 Review
|
||||
**Activities**:
|
||||
- Review week 2 concepts
|
||||
- Update progress tracking
|
||||
- Practice weak areas
|
||||
- Plan week 3
|
||||
|
||||
**Weekly Goal**: 28 problems completed
|
||||
**Success Rate Target**: 80%+
|
||||
|
||||
---
|
||||
|
||||
## Week 3: String Patterns and Hash Tables
|
||||
**Focus**: Master string algorithms and hash table fundamentals
|
||||
|
||||
### Monday (May 31, 2026)
|
||||
**Topic**: String Fundamentals
|
||||
**Problems to Solve**:
|
||||
1. Reverse string
|
||||
2. Valid palindrome
|
||||
3. String rotation problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- String manipulation patterns
|
||||
- Two pointers for strings
|
||||
|
||||
**Daily Goal**: 4 problems, string mastery
|
||||
|
||||
### Tuesday (June 1, 2026)
|
||||
**Topic**: String Matching
|
||||
**Problems to Solve**:
|
||||
1. Longest substring without repeating chars
|
||||
2. Minimum window substring
|
||||
3. Pattern matching problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Sliding window for strings
|
||||
- Hash tables for character tracking
|
||||
|
||||
**Daily Goal**: 4 problems, O(n) string algorithms
|
||||
|
||||
### Wednesday (June 2, 2026)
|
||||
**Topic**: Hash Table Basics
|
||||
**Problems to Solve**:
|
||||
1. Two sum with hash map
|
||||
2. Contains duplicate
|
||||
3. Frequency counting problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Hash table fundamentals
|
||||
- Time complexity analysis
|
||||
|
||||
**Daily Goal**: 4 problems, O(1) lookups
|
||||
|
||||
### Thursday (June 3, 2026)
|
||||
**Topic**: Hash Table Advanced
|
||||
**Problems to Solve**:
|
||||
1. Group anagrams
|
||||
2. Hash map + two pointers
|
||||
3. Advanced hash problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Complex hash table patterns
|
||||
- Multiple data structures
|
||||
|
||||
**Daily Goal**: 4 problems, hash optimization
|
||||
|
||||
### Friday (June 4, 2026)
|
||||
**Topic**: Data Structure Validation
|
||||
**Problems to Solve**:
|
||||
1. `isValidSudoku` - Medium ✅
|
||||
2. `isDevided11` - Medium ✅
|
||||
3. Validation problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Constraint satisfaction
|
||||
- Grid validation patterns
|
||||
|
||||
**Daily Goal**: 4 problems, validation mastery
|
||||
|
||||
### Saturday (June 5, 2026)
|
||||
**Topic**: Combined String and Hash
|
||||
**Problems to Solve**:
|
||||
1. Longest substring with at most K distinct chars
|
||||
2. String hash problems (2 problems)
|
||||
3. Mixed pattern problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Combine string and hash patterns
|
||||
- Practice optimization
|
||||
|
||||
**Daily Goal**: 4 problems, pattern combination
|
||||
|
||||
### Sunday (June 6, 2026)
|
||||
**Topic**: Week 3 Review
|
||||
**Activities**:
|
||||
- Review string and hash concepts
|
||||
- Update progress tracking
|
||||
- Practice interview-style problems
|
||||
- Plan week 4
|
||||
|
||||
**Weekly Goal**: 28 problems completed
|
||||
**Success Rate Target**: 80%+
|
||||
|
||||
---
|
||||
|
||||
## Week 4: Dynamic Programming Introduction
|
||||
**Focus**: Introduction to dynamic programming concepts
|
||||
|
||||
### Monday (June 7, 2026)
|
||||
**Topic**: DP Fundamentals
|
||||
**Problems to Solve**:
|
||||
1. Fibonacci sequence
|
||||
2. Climbing stairs
|
||||
3. Basic DP problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- DP patterns: memoization vs tabulation
|
||||
- Time/space complexity analysis
|
||||
|
||||
**Daily Goal**: 4 problems, DP introduction
|
||||
|
||||
### Tuesday (June 8, 2026)
|
||||
**Topic**: 1D Dynamic Programming
|
||||
**Problems to Solve**:
|
||||
1. House robber
|
||||
2. Maximum subarray
|
||||
3. 1D DP problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- State definition
|
||||
- Transition equations
|
||||
- Space optimization
|
||||
|
||||
**Daily Goal**: 4 problems, 1D DP mastery
|
||||
|
||||
### Wednesday (June 9, 2026)
|
||||
**Topic**: DP Optimization
|
||||
**Problems to Solve**:
|
||||
1. Coin change (unbounded knapsack)
|
||||
2. Longest common subsequence
|
||||
3. DP optimization problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- Space optimization techniques
|
||||
- State compression
|
||||
|
||||
**Daily Goal**: 4 problems, DP optimization
|
||||
|
||||
### Thursday (June 10, 2026)
|
||||
**Topic**: DP on Strings
|
||||
**Problems to Solve**:
|
||||
1. Word break
|
||||
2. Edit distance
|
||||
3. String DP problems (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- DP for string problems
|
||||
- 2D DP patterns
|
||||
|
||||
**Daily Goal**: 4 problems, string DP mastery
|
||||
|
||||
### Friday (June 11, 2026)
|
||||
**Topic**: DP Review and Practice
|
||||
**Problems to Solve**:
|
||||
1. DP review problems
|
||||
2. Mixed DP problems
|
||||
3. Interview-style DP (2 problems)
|
||||
|
||||
**Learning Resources**:
|
||||
- DP pattern recognition
|
||||
- Optimization techniques
|
||||
|
||||
**Daily Goal**: 4 problems, DP pattern mastery
|
||||
|
||||
### Saturday (June 12, 2026)
|
||||
**Topic**: Month 1 Review
|
||||
**Activities**:
|
||||
- Complete month 1 assessment
|
||||
- Review all concepts
|
||||
- Update progress tracking
|
||||
- Identify areas for improvement
|
||||
|
||||
**Monthly Goal**: 112 problems completed
|
||||
**Success Rate Target**: 80%+
|
||||
|
||||
### Sunday (June 13, 2026)
|
||||
**Topic**: Rest and Planning
|
||||
**Activities**:
|
||||
- Rest day
|
||||
- Plan month 2 goals
|
||||
- Review weak areas
|
||||
- Prepare for advanced topics
|
||||
|
||||
---
|
||||
|
||||
## Monthly Review Template
|
||||
|
||||
### Month 1 Summary
|
||||
**Total Problems**: 112 problems
|
||||
**Success Rate**: [Calculate percentage]
|
||||
**Patterns Mastered**: Array basics, two pointers, binary search, strings, hash tables, DP introduction
|
||||
**Time Spent**: [Hours]
|
||||
**Key Achievements**:
|
||||
- [List major accomplishments]
|
||||
- [Patterns mastered]
|
||||
- [Improvements noted]
|
||||
|
||||
**Areas for Improvement**:
|
||||
- [Weak areas identified]
|
||||
- [Concepts needing more practice]
|
||||
- [Speed/accuracy targets]
|
||||
|
||||
### Month 2 Goals
|
||||
- **Focus**: Intermediate patterns and advanced topics
|
||||
- **Target**: 112 problems, 85%+ success rate
|
||||
- **Key Areas**: Graph algorithms, tree algorithms, advanced DP
|
||||
- **Practice**: More interview-style problems
|
||||
|
||||
---
|
||||
|
||||
## Daily Practice Template
|
||||
|
||||
For each day, use this structure:
|
||||
|
||||
### Date (Week X, Day Y)
|
||||
**Today's Focus**: [Main topic/pattern]
|
||||
**Problems Solved**:
|
||||
1. [Problem Name] - [Difficulty] ✅/❌
|
||||
- Pattern: [Pattern name]
|
||||
- Time: [Complexity], Space: [Complexity]
|
||||
- Learning: [Key insights]
|
||||
2. [Problem Name] - [Difficulty] ✅/❌
|
||||
- Pattern: [Pattern name]
|
||||
- Time: [Complexity], Space: [Complexity]
|
||||
- Learning: [Key insights]
|
||||
|
||||
**Key Insights**:
|
||||
- [Main takeaways from today]
|
||||
- [Surprising findings]
|
||||
- [Important connections]
|
||||
|
||||
**Struggles**:
|
||||
- [Areas where you had difficulty]
|
||||
- [Concepts that were confusing]
|
||||
|
||||
**Questions for Review**:
|
||||
- [Things to clarify later]
|
||||
- [Concepts needing more practice]
|
||||
|
||||
**Tomorrow's Plan**:
|
||||
- [Next day's focus areas]
|
||||
- [Specific problems to practice]
|
||||
- [Review topics]
|
||||
|
||||
---
|
||||
|
||||
## Tips for Success
|
||||
|
||||
### 1. Consistency
|
||||
- Practice daily, even if only 1-2 problems
|
||||
- Don't skip days
|
||||
- Review regularly
|
||||
|
||||
### 2. Quality over Quantity
|
||||
- Focus on understanding, not just completion
|
||||
- Review solutions and learn new approaches
|
||||
- Document insights
|
||||
|
||||
### 3. Pattern Recognition
|
||||
- Identify patterns in problems
|
||||
- Group similar problems
|
||||
- Learn common approaches
|
||||
|
||||
### 4. Time Management
|
||||
- Set realistic daily goals
|
||||
- Track progress and adjust
|
||||
- Don't spend too much time on one problem
|
||||
|
||||
### 5. Review and Reflect
|
||||
- Weekly reviews help identify weak areas
|
||||
- Monthly reviews show overall progress
|
||||
- Adjust plan based on performance
|
||||
|
||||
---
|
||||
|
||||
**Note**: This schedule is flexible. Adjust based on your progress, learning style, and available time. The key is consistent practice and continuous learning.
|
||||
+3
-3
@@ -24,9 +24,9 @@
|
||||
"add:module": "node ./add-component.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitest/coverage-v8": "^2.1.2",
|
||||
"@vitest/coverage-v8": "^4.1.7",
|
||||
"typescript": "^5.1.3",
|
||||
"vite": "^5.4.8",
|
||||
"vitest": "^2.1.2"
|
||||
"vite": "^8.0.14",
|
||||
"vitest": "^4.1.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
|
||||
I will be studying algorithms and collecting different cases to improve my knowledge on this repository.
|
||||
|
||||
проверям update
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user