LeetCode, solved in a notebook.
A JupyterLab extension that turns any LeetCode question into a notebook. Write the solution, run the examples, and submit from the toolbar.
A JupyterLab extension that turns any LeetCode question into a notebook. Write the solution, run the examples, and submit from the toolbar.
pip install jupyterlab-leetcode
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
from typing import Listclass Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
for i, n in enumerate(nums):
if target - n in seen:
return [seen[target - n], i]
seen[n] = is = Solution()
s.twoSum(nums=[2, 7, 11, 15], target=9)[0, 1]
Open the LeetCode launcher in JupyterLab and pick the browser where you're already signed in to leetcode.com. The extension reads your LeetCode session cookie from that browser. It never sees or stores your password.
On macOS the system may ask for your keychain password the first time. This lets the extension read the browser's cookie store. You can choose "Always Allow".
Search by keyword, then filter by difficulty, status, topic, or company. Company filters need LeetCode Premium. Your profile, solved counts, and submission heatmap are on the same page.
| # | Title | Difficulty | Acceptance | ||
|---|---|---|---|---|---|
| β | 1 | Two Sum Array, Hash Table | Easy | 55.8% | Open notebook |
| 3 | Longest Substring Without Repeating Characters Hash Table, String, Sliding Window | Medium | 36.9% | Open notebook | |
| 4 | Median of Two Sorted Arrays Array, Binary Search, Divide and Conquer | Hard | 43.1% | Open notebook | |
| β | 21 | Merge Two Sorted Lists Linked List, Recursion | Easy | 66.4% | Open notebook |
Click a question and the extension writes a ready-to-run .ipynb next to your other work. Notebooks are grouped in folders of fifty. Your solutions stay on disk and can go in git.
your-workspace/ ββ 1-50/ β ββ 1.two-sum.ipynb β ββ 2.add-two-numbers.ipynb β ββ 21.merge-two-sorted-lists.ipynb ββ 51-100/ β ββ 70.climbing-stairs.ipynb ββ 201-250/ ββ 206.reverse-linked-list.ipynb
descriptionThe question, its difficulty, topic tags, hints, and a link back to LeetCode.solutionThe Python 3 starter code. Add more solution cells when you want another approach. Submit sends the last one.examplesOne cell per example test case. LeetCode's null, true, and false are already converted to Python.The Submit button saves the notebook and sends your solution to LeetCode. When judging finishes, the result is written to a cell at the end of the notebook. The full result is also saved in the notebook's metadata.
Check that the frontend extension is installed:
jupyter labextension list
Check that the server extension is enabled, then restart JupyterLab:
jupyter server extension list
Sign in to leetcode.com in that browser, then choose it again. Safari isn't supported because it doesn't allow other apps to read its cookies.
In the folder JupyterLab was started from, under folders like 1-50/ and 51-100/.
pip uninstall jupyterlab-leetcode
pip install jupyterlab-leetcode