data
import requests
import os
import json
import re
import time
import html
import html2text
root_url = 'https://leetcode.com'
contest_info_api = 'https://leetcode.com/contest/api/info/weekly-contest-'
question_urls = 'https://leetcode.com/graphql'
save_json = True
leet_dict = {}
raw_data = 'raw_data'
def get_problems(res_json):
ret_dict = {}
probs = res_json['stat_status_pairs']
for prob in probs:
# 跳过付费
if prob['paid_only']:
continue
front_id = prob['stat']['frontend_question_id']
# 访问problem需要的是titleSlug
ret_dict[front_id] = {}
ret_dict[front_id]['titleSlug'] = prob['stat']['question__title_slug']
return ret_dict
def get_problems_raw():
all_questions_url = 'https://leetcode.com/api/problems/all'
req = requests.get(url=all_questions_url)
htmlstr = req.text
problems_json = json.loads(htmlstr)
if save_json:
with open('problems.json', 'w') as f:
json.dump(problems_json, f, indent=4)
return problems_json, get_problems(problems_json)
def get_one_problem_detail(res_json):
prob_dict = {}
question = res_json['data']['question']
prob_dict['questionFrontendId'] = question['questionFrontendId']
prob_dict['title'] = question['title']
prob_dict['titleSlug'] = question['titleSlug']
prob_dict['difficulty'] = question['difficulty']
prob_dict['likes'] = question['likes']
prob_dict['dislikes'] = question['dislikes']
# prob_dict['similarQuestions'] = question['similarQuestions']
prob_dict['similarQuestions'] = []
similar_json = json.loads(question['similarQuestions'])
for similar in similar_json:
prob_dict['similarQuestions'] .append(similar['titleSlug'])
# prob_dict['topicTags'] = question['topicTags']
prob_dict['topicTags'] = []
for tag in question['topicTags']:
prob_dict['topicTags'].append(tag['name'])
# prob_dict['stats'] = question['stats']
stats = json.loads(question['stats'])
prob_dict['totalAcceptedRaw'] = stats['totalAcceptedRaw']
prob_dict['totalSubmissionRaw'] = stats['totalSubmissionRaw']
prob_dict['acceptRatio'] = prob_dict['totalAcceptedRaw'] / \
prob_dict['totalSubmissionRaw']
prob_dict['hints'] = question['hints']
content = question['content']
content = content.replace('\t', '').replace('\n\n', '\n')
content = html2text.html2text(content)
content = content.replace('**Input:**', 'Input:').replace('**Output:**',
'Output:').replace('**Explanation:**', 'Explanation:')
prob_dict['content'] = content
with open('markdown/{}.md'.format(prob_dict['questionFrontendId']), 'w') as f:
f.write(content)
return prob_dict
def get_one_problem_raw(question_url):
if question_url[-1] == '/':
question_url = question_url[:-1]
question_str = question_url.split('/')[-1]
req_json = {"operationName": "questionData", "variables": {"titleSlug": "check-if-the-sentence-is-pangram"},
"query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n exampleTestcases\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n paidOnly\n hasVideoSolution\n paidOnlyVideo\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n enableDebugger\n envInfo\n libraryUrl\n adminUrl\n __typename\n }\n}\n"}
req_json['variables']['titleSlug'] = question_str
headers = {'Content-Type': 'application/json;charset=UTF-8'}
res = requests.request("post", question_urls,
json=req_json, headers=headers, timeout=60)
res_json = json.loads(res.text)
if save_json:
front_id = res_json['data']['question']['questionFrontendId']
filename = 'raw_data/{}.json'.format(front_id)
with open(filename, 'w') as f:
json.dump(res_json, f, indent=4)
return res_json
_, prob_dict = get_problems_raw()
output_dict = {}
for front_id, problem_detail in prob_dict.items():
if os.path.exists('raw_data/{}.json'.format(front_id)):
with open('raw_data/{}.json'.format(front_id)) as f:
raw = json.load(f)
# continue
else:
titleSlug = problem_detail['titleSlug']
question_url = 'https://leetcode.com/problems/' + titleSlug
raw = get_one_problem_raw(question_url)
data = get_one_problem_detail(raw)
output_dict[front_id] = data
with open('output.json', 'w')as f:
json.dump(output_dict, f, indent=4)
process
import json
with open('output.json') as f:
problems = json.load(f)
datas = []
for id,problem in problems.items():
if problem['dislikes'] ==0:
problem['dislikes'] = 1
# likeRatio = problem['likes']/ (problem['likes'] + problem['dislikes'])
likeRatio = problem['likes']/ ( problem['dislikes'])
data = {}
data['id'] = id
data['titleSlug']= problem['titleSlug']
data['likeRatio'] = likeRatio
data['acceptRatio'] = problem['acceptRatio']
data['difficulty'] = problem['difficulty']
data['title'] = problem['title']
datas.append(data)
datas = sorted(datas,reverse= True,key = lambda data:data['acceptRatio'])
steps = [10,20,30,40,50,60,70,80,90,100]
for step in steps:
count = 0
for data in datas:
count = count +1
if count >= len(datas) * step / 100:
# print( "{}%".format(step), data['acceptRatio'])
break
newdatas = []
for data in datas:
if data['acceptRatio'] > 0.5824890307140008 or data['acceptRatio'] < 0.4303226863502119:
continue
if data['likeRatio'] < 2:
continue
newdatas.append(data)
newdatas = sorted(newdatas,reverse=True,key= lambda data:data['likeRatio'])
# print(newdatas)
for n in newdatas:
mstr = '[{} {}. {}](https://leetcode.com/problems/{})\n'.format(n['id'],n['difficulty'],n['title'],n['titleSlug'])
print(mstr)
正确率分数段
10% 0.7095958690429233
20% 0.6340542320781645
30% 0.5824890307140008
40% 0.5411619367900896
50% 0.5014095348054072
60% 0.46498277841561425
70% 0.4303226863502119
80% 0.38708126302292034
90% 0.3427132331515631
100% 0.1391385259566549
990 Medium. Satisfiability of Equality Equations
1579 Hard. Remove Max Number of Edges to Keep Graph Fully Traversable
1235 Hard. Maximum Profit in Job Scheduling
1368 Hard. Minimum Cost to Make at Least One Valid Path in a Grid
958 Medium. Check Completeness of a Binary Tree
1713 Hard. Minimum Operations to Make a Subsequence
1388 Hard. Pizza With 3n Slices
1546 Medium. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
1671 Hard. Minimum Number of Removals to Make Mountain Array
1643 Hard. Kth Smallest Instructions
208 Medium. Implement Trie (Prefix Tree)
1751 Hard. Maximum Number of Events That Can Be Attended II
1293 Hard. Shortest Path in a Grid with Obstacles Elimination
1547 Hard. Minimum Cost to Cut a Stick
992 Hard. Subarrays with K Different Integers
1585 Hard. Check If String Is Transformable With Substring Sort Operations
329 Hard. Longest Increasing Path in a Matrix
1449 Hard. Form Largest Integer With Digits That Add up to Target
1530 Medium. Number of Good Leaf Nodes Pairs
1493 Medium. Longest Subarray of 1's After Deleting One Element
632 Hard. Smallest Range Covering Elements from K Lists
1745 Hard. Palindrome Partitioning IV
1319 Medium. Number of Operations to Make Network Connected
240 Medium. Search a 2D Matrix II
1125 Hard. Smallest Sufficient Team
106 Medium. Construct Binary Tree from Inorder and Postorder Traversal
295 Hard. Find Median from Data Stream
1372 Medium. Longest ZigZag Path in a Binary Tree
863 Medium. All Nodes Distance K in Binary Tree
416 Medium. Partition Equal Subset Sum
1697 Hard. Checking Existence of Edge Length Limited Paths
407 Hard. Trapping Rain Water II
1209 Medium. Remove All Adjacent Duplicates in String II
300 Medium. Longest Increasing Subsequence
102 Medium. Binary Tree Level Order Traversal
1482 Medium. Minimum Number of Days to Make m Bouquets
583 Medium. Delete Operation for Two Strings
1458 Hard. Max Dot Product of Two Subsequences
1749 Medium. Maximum Absolute Sum of Any Subarray
1349 Hard. Maximum Students Taking Exam
105 Medium. Construct Binary Tree from Preorder and Inorder Traversal
1092 Hard. Shortest Common Supersequence
1444 Hard. Number of Ways of Cutting a Pizza
962 Medium. Maximum Width Ramp
1775 Medium. Equal Sum Arrays With Minimum Number of Operations
886 Medium. Possible Bipartition
129 Medium. Sum Root to Leaf Numbers
1262 Medium. Greatest Sum Divisible by Three
718 Medium. Maximum Length of Repeated Subarray
1653 Medium. Minimum Deletions to Make String Balanced
1248 Medium. Count Number of Nice Subarrays
435 Medium. Non-overlapping Intervals
714 Medium. Best Time to Buy and Sell Stock with Transaction Fee
960 Hard. Delete Columns to Make Sorted III
827 Hard. Making A Large Island
1695 Medium. Maximum Erasure Value
309 Medium. Best Time to Buy and Sell Stock with Cooldown
131 Medium. Palindrome Partitioning
1722 Medium. Minimize Hamming Distance After Swap Operations
926 Medium. Flip String to Monotone Increasing
567 Medium. Permutation in String
1559 Hard. Detect Cycles in 2D Grid
109 Medium. Convert Sorted List to Binary Search Tree
560 Medium. Subarray Sum Equals K
668 Hard. Kth Smallest Number in Multiplication Table
1593 Medium. Split a String Into the Max Number of Unique Substrings
236 Medium. Lowest Common Ancestor of a Binary Tree
452 Medium. Minimum Number of Arrows to Burst Balloons
1758 Easy. Minimum Changes To Make Alternating Binary String
96 Medium. Unique Binary Search Trees
1734 Medium. Decode XORed Permutation
540 Medium. Single Element in a Sorted Array
410 Hard. Split Array Largest Sum
1202 Medium. Smallest String With Swaps
450 Medium. Delete Node in a BST
1647 Medium. Minimum Deletions to Make Character Frequencies Unique
834 Hard. Sum of Distances in Tree
881 Medium. Boats to Save People
103 Medium. Binary Tree Zigzag Level Order Traversal
873 Medium. Length of Longest Fibonacci Subsequence
1595 Hard. Minimum Cost to Connect Two Groups of Points
1499 Hard. Max Value of Equation
1521 Hard. Find a Value of a Mysterious Function Closest to Target
768 Hard. Max Chunks To Make Sorted II
1049 Medium. Last Stone Weight II
144 Medium. Binary Tree Preorder Traversal
1288 Medium. Remove Covered Intervals
1162 Medium. As Far from Land as Possible
239 Hard. Sliding Window Maximum
1263 Hard. Minimum Moves to Move a Box to Their Target Location
235 Easy. Lowest Common Ancestor of a Binary Search Tree
930 Medium. Binary Subarrays With Sum
121 Easy. Best Time to Buy and Sell Stock
1462 Medium. Course Schedule IV
1691 Hard. Maximum Height by Stacking Cuboids
1675 Hard. Minimize Deviation in Array
1014 Medium. Best Sightseeing Pair
945 Medium. Minimum Increment to Make Array Unique
1679 Medium. Max Number of K-Sum Pairs
1631 Medium. Path With Minimum Effort
1723 Hard. Find Minimum Time to Finish All Jobs
145 Medium. Binary Tree Postorder Traversal
297 Hard. Serialize and Deserialize Binary Tree
210 Medium. Course Schedule II
813 Medium. Largest Sum of Averages
1575 Hard. Count All Possible Routes
1425 Hard. Constrained Subsequence Sum
301 Hard. Remove Invalid Parentheses
1155 Medium. Number of Dice Rolls With Target Sum
844 Easy. Backspace String Compare
1438 Medium. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
128 Hard. Longest Consecutive Sequence
387 Easy. First Unique Character in a String
572 Easy. Subtree of Another Tree
996 Hard. Number of Squareful Arrays
438 Medium. Find All Anagrams in a String
1707 Hard. Maximum XOR With an Element From Array
1423 Medium. Maximum Points You Can Obtain from Cards
116 Medium. Populating Next Right Pointers in Each Node
1726 Medium. Tuple with Same Product
449 Medium. Serialize and Deserialize BST
1192 Hard. Critical Connections in a Network
424 Medium. Longest Repeating Character Replacement
378 Medium. Kth Smallest Element in a Sorted Matrix
199 Medium. Binary Tree Right Side View
241 Medium. Different Ways to Add Parentheses
993 Easy. Cousins in Binary Tree
1352 Medium. Product of the Last K Numbers
1422 Easy. Maximum Score After Splitting a String
1642 Medium. Furthest Building You Can Reach
1760 Medium. Minimum Limit of Balls in a Bag
1539 Easy. Kth Missing Positive Number
1269 Hard. Number of Ways to Stay in the Same Place After Some Steps
786 Hard. K-th Smallest Prime Fraction
486 Medium. Predict the Winner
1673 Medium. Find the Most Competitive Subsequence
24 Medium. Swap Nodes in Pairs
380 Medium. Insert Delete GetRandom O(1)
1799 Hard. Maximize Score After N Operations
1027 Medium. Longest Arithmetic Subsequence
1793 Hard. Maximum Score of a Good Subarray
83 Easy. Remove Duplicates from Sorted List
477 Medium. Total Hamming Distance
875 Medium. Koko Eating Bananas
1010 Medium. Pairs of Songs With Total Durations Divisible by 60
1334 Medium. Find the City With the Smallest Number of Neighbors at a Threshold Distance
1584 Medium. Min Cost to Connect All Points
543 Easy. Diameter of Binary Tree
1803 Hard. Count Pairs With XOR in a Range
1105 Medium. Filling Bookcase Shelves
1456 Medium. Maximum Number of Vowels in a Substring of Given Length
1218 Medium. Longest Arithmetic Subsequence of Given Difference
798 Hard. Smallest Rotation with Highest Score
795 Medium. Number of Subarrays with Bounded Maximum
1048 Medium. Longest String Chain
730 Hard. Count Different Palindromic Subsequences
698 Medium. Partition to K Equal Sum Subsets
765 Hard. Couples Holding Hands
792 Medium. Number of Matching Subsequences
646 Medium. Maximum Length of Pair Chain
692 Medium. Top K Frequent Words
110 Easy. Balanced Binary Tree
551 Easy. Student Attendance Record I
689 Hard. Maximum Sum of 3 Non-Overlapping Subarrays
1419 Medium. Minimum Number of Frogs Croaking
95 Medium. Unique Binary Search Trees II
1657 Medium. Determine if Two Strings Are Close
516 Medium. Longest Palindromic Subsequence
448 Easy. Find All Numbers Disappeared in an Array
1292 Medium. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
1535 Medium. Find the Winner of an Array Game
1208 Medium. Get Equal Substrings Within Budget
915 Medium. Partition Array into Disjoint Intervals
653 Easy. Two Sum IV - Input is a BST
778 Hard. Swim in Rising Water
1718 Medium. Construct the Lexicographically Largest Valid Sequence
530 Easy. Minimum Absolute Difference in BST
611 Medium. Valid Triangle Number
11 Medium. Container With Most Water
974 Medium. Subarray Sums Divisible by K
318 Medium. Maximum Product of Word Lengths
462 Medium. Minimum Moves to Equal Array Elements II
995 Hard. Minimum Number of K Consecutive Bit Flips
1331 Easy. Rank Transform of an Array
430 Medium. Flatten a Multilevel Doubly Linked List
1765 Medium. Map of Highest Peak
445 Medium. Add Two Numbers II
903 Hard. Valid Permutations for DI Sequence
735 Medium. Asteroid Collision
1404 Medium. Number of Steps to Reduce a Number in Binary Representation to One
1376 Medium. Time Needed to Inform All Employees
17 Medium. Letter Combinations of a Phone Number
785 Medium. Is Graph Bipartite?
222 Medium. Count Complete Tree Nodes
153 Medium. Find Minimum in Rotated Sorted Array
1053 Medium. Previous Permutation With One Swap
691 Hard. Stickers to Spell Word
1545 Medium. Find Kth Bit in Nth Binary String
1156 Medium. Swap For Longest Repeated Character Substring
1052 Medium. Grumpy Bookstore Owner
1005 Easy. Maximize Sum Of Array After K Negations
114 Medium. Flatten Binary Tree to Linked List
1239 Medium. Maximum Length of a Concatenated String with Unique Characters
232 Easy. Implement Queue using Stacks
743 Medium. Network Delay Time
1267 Medium. Count Servers that Communicate
828 Hard. Count Unique Characters of All Substrings of a Given String
849 Medium. Maximize Distance to Closest Person
1137 Easy. N-th Tribonacci Number
1798 Medium. Maximum Number of Consecutive Values You Can Make
1326 Hard. Minimum Number of Taps to Open to Water a Garden
1544 Easy. Make The String Great
1552 Medium. Magnetic Force Between Two Balls
1296 Medium. Divide Array in Sets of K Consecutive Numbers
459 Easy. Repeated Substring Pattern
1291 Medium. Sequential Digits
920 Hard. Number of Music Playlists
641 Medium. Design Circular Deque
421 Medium. Maximum XOR of Two Numbers in an Array
1184 Easy. Distance Between Bus Stops
395 Medium. Longest Substring with At Least K Repeating Characters
847 Hard. Shortest Path Visiting All Nodes
287 Medium. Find the Duplicate Number
594 Easy. Longest Harmonious Subsequence
183 Easy. Customers Who Never Order
1443 Medium. Minimum Time to Collect All Apples in a Tree
1481 Medium. Least Number of Unique Integers after K Removals
328 Medium. Odd Even Linked List
160 Easy. Intersection of Two Linked Lists
25 Hard. Reverse Nodes in k-Group
377 Medium. Combination Sum IV
1359 Hard. Count All Valid Pickup and Delivery Options
1461 Medium. Check If a String Contains All Binary Codes of Size K
21 Easy. Merge Two Sorted Lists
107 Medium. Binary Tree Level Order Traversal II
1523 Easy. Count Odd Numbers in an Interval Range
857 Hard. Minimum Cost to Hire K Workers
981 Medium. Time Based Key-Value Store
1424 Medium. Diagonal Traverse II
171 Easy. Excel Sheet Column Number
1601 Hard. Maximum Number of Achievable Transfer Requests
386 Medium. Lexicographical Numbers
1013 Easy. Partition Array Into Three Parts With Equal Sum
652 Medium. Find Duplicate Subtrees
674 Easy. Longest Continuous Increasing Subsequence
1081 Medium. Smallest Subsequence of Distinct Characters
738 Medium. Monotone Increasing Digits
1509 Medium. Minimum Difference Between Largest and Smallest Value in Three Moves
1335 Hard. Minimum Difficulty of a Job Schedule
622 Medium. Design Circular Queue
823 Medium. Binary Trees With Factors
1039 Medium. Minimum Score Triangulation of Polygon
1366 Medium. Rank Teams by Votes
433 Medium. Minimum Genetic Mutation
1774 Medium. Closest Dessert Cost
769 Medium. Max Chunks To Make Sorted
491 Medium. Increasing Subsequences
1032 Hard. Stream of Characters
1569 Hard. Number of Ways to Reorder Array to Get Same BST
1489 Hard. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
943 Hard. Find the Shortest Superstring
1792 Medium. Maximum Average Pass Ratio
1805 Easy. Number of Different Integers in a String
1007 Medium. Minimum Domino Rotations For Equal Row
1764 Medium. Form Array by Concatenating Subarrays of Another Array
989 Easy. Add to Array-Form of Integer
1232 Easy. Check If It Is a Straight Line
725 Medium. Split Linked List in Parts
119 Easy. Pascal's Triangle II
1203 Hard. Sort Items by Groups Respecting Dependencies
1283 Medium. Find the Smallest Divisor Given a Threshold
623 Medium. Add One Row to Tree
1220 Hard. Count Vowels Permutation
1109 Medium. Corporate Flight Bookings
1536 Medium. Minimum Swaps to Arrange a Binary Grid
1835 Hard. Find XOR Sum of All Pairs Bitwise AND
688 Medium. Knight Probability in Chessboard
939 Medium. Minimum Area Rectangle
967 Medium. Numbers With Same Consecutive Differences
1405 Medium. Longest Happy String
802 Medium. Find Eventual Safe States
988 Medium. Smallest String Starting From Leaf
978 Medium. Longest Turbulent Subarray
1139 Medium. Largest 1-Bordered Square
1615 Medium. Maximal Network Rank
1145 Medium. Binary Tree Coloring Game
1670 Medium. Design Front Middle Back Queue
746 Easy. Min Cost Climbing Stairs
1735 Hard. Count Ways to Make Array With Product
676 Medium. Implement Magic Dictionary
830 Easy. Positions of Large Groups
405 Easy. Convert a Number to Hexadecimal
1417 Easy. Reformat The String
1071 Easy. Greatest Common Divisor of Strings
1797 Medium. Design Authentication Manager
1541 Medium. Minimum Insertions to Balance a Parentheses String
350 Easy. Intersection of Two Arrays II
352 Hard. Data Stream as Disjoint Intervals
1138 Medium. Alphabet Board Path
936 Hard. Stamping The Sequence
1813 Medium. Sentence Similarity III
783 Easy. Minimum Distance Between BST Nodes
417 Medium. Pacific Atlantic Water Flow
539 Medium. Minimum Time Difference
382 Medium. Linked List Random Node
964 Hard. Least Operators to Express Number
436 Medium. Find Right Interval
167 Easy. Two Sum II - Input array is sorted
628 Easy. Maximum Product of Three Numbers
1041 Medium. Robot Bounded In Circle
524 Medium. Longest Word in Dictionary through Deleting
1275 Easy. Find Winner on a Tic Tac Toe Game
1210 Hard. Minimum Moves to Reach Target with Rotations
1018 Easy. Binary Prefix Divisible By 5
659 Medium. Split Array into Consecutive Subsequences
599 Easy. Minimum Index Sum of Two Lists
1223 Hard. Dice Roll Simulation
485 Easy. Max Consecutive Ones
947 Medium. Most Stones Removed with Same Row or Column
501 Easy. Find Mode in Binary Search Tree
470 Medium. Implement Rand10() Using Rand7()
775 Medium. Global and Local Inversions
1361 Medium. Validate Binary Tree Nodes
820 Medium. Short Encoding of Words
341 Medium. Flatten Nested List Iterator
971 Medium. Flip Binary Tree To Match Preorder Traversal
953 Easy. Verifying an Alien Dictionary
80 Medium. Remove Duplicates from Sorted Array II
1576 Easy. Replace All ?'s to Avoid Consecutive Repeating Characters