Skip to content

Instantly share code, notes, and snippets.

@paulonteri
Created April 30, 2024 19:16
Show Gist options
  • Save paulonteri/41da4741f766256bfa983f209fd8dae5 to your computer and use it in GitHub Desktop.
Save paulonteri/41da4741f766256bfa983f209fd8dae5 to your computer and use it in GitHub Desktop.
Leetcode Scraper
const axios = require('axios');
let data = JSON.stringify({
query: `query questionContent($titleSlug: String!) {
question(titleSlug: $titleSlug) {
content
mysqlSchemas
dataSchemas
questionId
questionFrontendId
title
titleSlug
isPaidOnly
difficulty
likes
dislikes
categoryTitle
hints
similarQuestionList {
difficulty
titleSlug
title
translatedTitle
isPaidOnly
}
solution {
id
title
content
contentTypeId
paidOnly
hasVideoSolution
paidOnlyVideo
canSeeDetail
rating {
count
average
userRating {
score
}
}
topic {
id
commentCount
topLevelCommentCount
viewCount
subscribed
solutionTags {
name
slug
}
post {
id
status
creationDate
author {
username
isActive
profile {
userAvatar
reputation
}
}
}
}
}
}
}`,
variables: {"titleSlug":"two-sum"}
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://leetcode.com/graphql/',
headers: {
'Cookie': 'LEETCODE_SESSION= ...',
'Content-Type': 'application/json'
},
data : data
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
import requests
import json
url = "https://leetcode.com/graphql/"
payload = "{\"query\":\"query questionContent($titleSlug: String!) {\\n question(titleSlug: $titleSlug) {\\n content\\n mysqlSchemas\\n dataSchemas\\n questionId\\n questionFrontendId\\n title\\n titleSlug\\n isPaidOnly\\n difficulty\\n likes\\n dislikes\\n categoryTitle\\n hints\\n similarQuestionList {\\n difficulty\\n titleSlug\\n title\\n translatedTitle\\n isPaidOnly\\n }\\n solution {\\n id\\n title\\n content\\n contentTypeId\\n paidOnly\\n hasVideoSolution\\n paidOnlyVideo\\n canSeeDetail\\n rating {\\n count\\n average\\n userRating {\\n score\\n }\\n }\\n topic {\\n id\\n commentCount\\n topLevelCommentCount\\n viewCount\\n subscribed\\n solutionTags {\\n name\\n slug\\n }\\n post {\\n id\\n status\\n creationDate\\n author {\\n username\\n isActive\\n profile {\\n userAvatar\\n reputation\\n }\\n }\\n }\\n }\\n }\\n }\\n}\\n\",\"variables\":{\"titleSlug\":\"two-sum\"}}"
headers = {
'Cookie': 'LEETCODE_SESSION= ...',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
{
"data": {
"question": {
"content": "<p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>\n\n<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>\n\n<p>You can return the answer in any order.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,7,11,15], target = 9\n<strong>Output:</strong> [0,1]\n<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,4], target = 6\n<strong>Output:</strong> [1,2]\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,3], target = 6\n<strong>Output:</strong> [0,1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>\n\t<li><strong>Only one valid answer exists.</strong></li>\n</ul>\n\n<p>&nbsp;</p>\n<strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code><font face=\"monospace\">&nbsp;</font>time complexity?",
"mysqlSchemas": [],
"dataSchemas": [],
"questionId": "1",
"questionFrontendId": "1",
"title": "Two Sum",
"titleSlug": "two-sum",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 56000,
"dislikes": 1931,
"categoryTitle": "Algorithms",
"hints": [
"A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.",
"So, if we fix one of the numbers, say <code>x</code>, we have to scan the entire array to find the next number <code>y</code> which is <code>value - x</code> where value is the input parameter. Can we change our array somehow so that this search becomes faster?",
"The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?"
],
"similarQuestionList": [
{
"difficulty": "Medium",
"titleSlug": "3sum",
"title": "3Sum",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Medium",
"titleSlug": "4sum",
"title": "4Sum",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Medium",
"titleSlug": "two-sum-ii-input-array-is-sorted",
"title": "Two Sum II - Input Array Is Sorted",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "two-sum-iii-data-structure-design",
"title": "Two Sum III - Data structure design",
"translatedTitle": null,
"isPaidOnly": true
},
{
"difficulty": "Medium",
"titleSlug": "subarray-sum-equals-k",
"title": "Subarray Sum Equals K",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "two-sum-iv-input-is-a-bst",
"title": "Two Sum IV - Input is a BST",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "two-sum-less-than-k",
"title": "Two Sum Less Than K",
"translatedTitle": null,
"isPaidOnly": true
},
{
"difficulty": "Medium",
"titleSlug": "max-number-of-k-sum-pairs",
"title": "Max Number of K-Sum Pairs",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Medium",
"titleSlug": "count-good-meals",
"title": "Count Good Meals",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "count-number-of-pairs-with-absolute-difference-k",
"title": "Count Number of Pairs With Absolute Difference K",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Medium",
"titleSlug": "number-of-pairs-of-strings-with-concatenation-equal-to-target",
"title": "Number of Pairs of Strings With Concatenation Equal to Target",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "find-all-k-distant-indices-in-an-array",
"title": "Find All K-Distant Indices in an Array",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "first-letter-to-appear-twice",
"title": "First Letter to Appear Twice",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Hard",
"titleSlug": "number-of-excellent-pairs",
"title": "Number of Excellent Pairs",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "number-of-arithmetic-triplets",
"title": "Number of Arithmetic Triplets",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Medium",
"titleSlug": "node-with-highest-edge-score",
"title": "Node With Highest Edge Score",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "check-distances-between-same-letters",
"title": "Check Distances Between Same Letters",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "find-subarrays-with-equal-sum",
"title": "Find Subarrays With Equal Sum",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "largest-positive-integer-that-exists-with-its-negative",
"title": "Largest Positive Integer That Exists With Its Negative",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "number-of-distinct-averages",
"title": "Number of Distinct Averages",
"translatedTitle": null,
"isPaidOnly": false
},
{
"difficulty": "Easy",
"titleSlug": "count-pairs-whose-sum-is-less-than-target",
"title": "Count Pairs Whose Sum is Less than Target",
"translatedTitle": null,
"isPaidOnly": false
}
],
"solution": {
"id": "7",
"title": "Two Sum",
"content": "[TOC]\n\n## Video Solution\n\n---\n\n<div>\n <div class=\"video-container\">\n <iframe src=\"https://player.vimeo.com/video/567281997\" width=\"640\" height=\"360\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen></iframe>\n </div>\n</div>\n\n<div>&nbsp;\n</div>\n\n## Solution Article\n\n---\n\n#### Approach 1: Brute Force\n\n**Algorithm**\n\nThe brute force approach is simple. Loop through each element $$x$$ and find if there is another value that equals to $$target - x$$.\n\n**Implementation**\n\n<iframe src=\"https://leetcode.com/playground/8tVQgwkg/shared\" frameBorder=\"0\" width=\"100%\" height=\"327\" name=\"8tVQgwkg\"></iframe>\n\n**Complexity Analysis**\n\n* Time complexity: $$O(n^2)$$.\nFor each element, we try to find its complement by looping through the rest of the array which takes $$O(n)$$ time. Therefore, the time complexity is $$O(n^2)$$.\n\n* Space complexity: $$O(1)$$.\nThe space required does not depend on the size of the input array, so only constant space is used.\n\n---\n#### Approach 2: Two-pass Hash Table\n\n**Intuition**\n\nTo improve our runtime complexity, we need a more efficient way to check if the complement exists in the array. If the complement exists, we need to get its index. What is the best way to maintain a mapping of each element in the array to its index? A hash table.\n\nWe can reduce the lookup time from $$O(n)$$ to $$O(1)$$ by trading space for speed. A hash table is well suited for this purpose because it supports fast lookup in *near* constant time. I say \"near\" because if a collision occurred, a lookup could degenerate to $$O(n)$$ time. However, lookup in a hash table should be amortized $$O(1)$$ time as long as the hash function was chosen carefully.\n\n**Algorithm**\n\nA simple implementation uses two iterations. In the first iteration, we add each element's value as a key and its index as a value to the hash table. Then, in the second iteration, we check if each element's complement ($$target - nums[i]$$) exists in the hash table. If it does exist, we return current element's index and its complement's index. Beware that the complement must not be $$nums[i]$$ itself!\n\n**Implementation**\n\n<iframe src=\"https://leetcode.com/playground/DJEn22mp/shared\" frameBorder=\"0\" width=\"100%\" height=\"500\" name=\"DJEn22mp\"></iframe> \n\n**Complexity Analysis**\n\n* Time complexity: $$O(n)$$.\nWe traverse the list containing $$n$$ elements exactly twice. Since the hash table reduces the lookup time to $$O(1)$$, the overall time complexity is $$O(n)$$.\n\n* Space complexity: $$O(n)$$.\nThe extra space required depends on the number of items stored in the hash table, which stores exactly $$n$$ elements.\n\n---\n#### Approach 3: One-pass Hash Table\n\n**Algorithm**\n \nIt turns out we can do it in one-pass. While we are iterating and inserting elements into the hash table, we also look back to check if current element's complement already exists in the hash table. If it exists, we have found a solution and return the indices immediately.\n\n**Implementation** \n \n<iframe src=\"https://leetcode.com/playground/aSsGLpfg/shared\" frameBorder=\"0\" width=\"100%\" height=\"500\" name=\"aSsGLpfg\"></iframe>\n\n**Complexity Analysis**\n\n* Time complexity: $$O(n)$$.\nWe traverse the list containing $$n$$ elements only once. Each lookup in the table costs only $$O(1)$$ time.\n\n* Space complexity: $$O(n)$$.\nThe extra space required depends on the number of items stored in the hash table, which stores at most $$n$$ elements.",
"contentTypeId": "107",
"paidOnly": false,
"hasVideoSolution": true,
"paidOnlyVideo": false,
"canSeeDetail": true,
"rating": {
"count": 3677,
"average": "4.595",
"userRating": null
},
"topic": {
"id": 127810,
"commentCount": 3325,
"topLevelCommentCount": 2302,
"viewCount": 7987449,
"subscribed": false,
"solutionTags": [],
"post": {
"id": 260381,
"status": null,
"creationDate": 1457528847,
"author": {
"username": "LeetCode",
"isActive": true,
"profile": {
"userAvatar": "https://assets.leetcode.com/users/leetcode/avatar_1568224780.png",
"reputation": 33753
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment