LeetCode Solver
Purpose
This skill guides the agent in solving LeetCode problems efficiently, ensuring code quality, performance optimization, and rigorous complexity analysis.
Workflow
- •
Understand & Analyze:
- •Identify the core algorithm (Greedy, DP, BFS/DFS, Bit Manipulation, etc.).
- •Check constraints ($N \le 10^5 \implies O(N \log N)$ or $O(N)$).
- •
Implementation Guidelines:
- •Language: JavaScript (
src/*.js). - •Style:
- •Use
var functionName = function(args) { ... }for the main solution. - •Use
constfor immutable variables,letfor mutable ones. - •MUST include JSDoc for all functions.
- •Comments: Minimal. Only JSDoc and key algorithmic steps. No tutorial-style comments.
- •Use
- •Optimization:
- •Use Bit Manipulation for state compression or cyclic operations.
- •Use TypedArrays (e.g.,
Int32Array) for fixed-size integer arrays to save memory and improve cache locality. - •Use
Math.clz32for fast MSB calculations. - •Use
BigIntfor numbers exceeding $2^{53}-1$.
- •Language: JavaScript (
- •
Complexity Analysis (Required):
- •Explicitly state Time Complexity and Space Complexity.
- •Explain the derivation (e.g., "Sorting takes $O(N \log N)$, loop takes $O(N)$").
- •
Verification:
- •Append
console.logtest cases at the end of the file. - •Example:
console.log(solve([1,2]), "expected_output", "desc");
- •Append
Examples
Example 1: Bit Manipulation & Optimization
Input: "Solve this bitwise AND maximization problem."
Output:
javascript
/**
* @param {number[]} nums
* @return {number}
*/
var maximumAND = function(nums) {
// Optimization: Use Int32Array for better performance
const costs = new Int32Array(nums.length);
let ans = 0;
// ... implementation ...
return ans;
};
// Time Complexity: O(N log N)
// Space Complexity: O(N)
Example 2: Standard Solution
Input: "Two Sum"
Output:
javascript
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
// ...
}
return [];
};