# Add Time Space

> Add standardized Javadoc time and space complexity comments to LeetCode Java solutions. Use when a directory under leetcode_java/ needs its `time = O(...)` / `space = O(...)` annotations added, normalised from inline comments, or filled in by hand where the script could not infer them.

- Skill: `yennanliu/add-time-space` (Agent Skill)
- Install (CLI): `npx skillmds@latest add yennanliu/add-time-space`
- Raw SKILL.md: https://api.skillmd.com/api/skills/yennanliu/add-time-space/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: yennanliu (https://skillmd.com/u/yennanliu)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/yennanliu/add-time-space

---


# Add Time/Space Complexity

You are an expert at adding time and space complexity documentation to Java code.

When the user runs `/add-time-space [DIRECTORY]`, follow these steps:

1. **Validate Input**:
   - Directory should be a subdirectory name under `leetcode_java/src/main/java/LeetCodeJava/`
   - Common directories: Array, BackTrack, BinarySearch, BinaryTree, DataStructure, DynamicProgramming, Graph, LinkedList, String, TwoPointers
   - If no directory specified, ask the user which directory to process

2. **Run the Python Script**:
   ```bash
   python3 script/add_javadoc_complexity.py leetcode_java/src/main/java/LeetCodeJava/[DIRECTORY]/*.java
   ```

3. **Manual Processing (if needed)**:
   - Check for any files that weren't processed automatically
   - For files without inline comments, analyze the code and add Javadoc manually:
     - Single loop: O(N) time
     - Nested loops: O(N²) time
     - Sorting: O(N log N) time
     - Binary search: O(log N) time
     - Backtracking: O(2^N) or O(N!) depending on problem
     - New array of size N: O(N) space
     - Only variables: O(1) space

4. **Verify Changes**:
   ```bash
   git diff leetcode_java/src/main/java/LeetCodeJava/[DIRECTORY]/
   ```
   - Confirm only comments changed, no logic modified

5. **Commit Changes**:
   ```bash
   git add leetcode_java/src/main/java/LeetCodeJava/[DIRECTORY]/
   git commit -m "Add time/space complexity Javadoc comments to LeetCode Java [DIRECTORY] solutions"
   ```

6. **Report Results**:
   - Number of files processed
   - Number of files that needed manual processing
   - Any files skipped and why
   - Git commit hash

**Transformation Pattern**:
```java
// BEFORE:
// time: O(N), space: O(1)
public int method() {

// AFTER:
/**
 * time = O(N)
 * space = O(1)
 */
public int method() {
```

**Important**:
- NEVER modify actual code logic, only add/update comments
- Preserve all existing IDEA comments and problem descriptions
- If existing Javadoc exists, merge complexity at the top
- Use format `time = O(...)` and `space = O(...)` (equals sign, not colon)

