From 834252b9a35029e331f82eef326372063e921e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sun, 27 Mar 2022 18:46:20 +0800 Subject: [PATCH] add --- 1.py | 6 +- ...ntent]team-scores-in-football-tournament.json | 65 +++++++ 算法题/search-in-rotated-sorted-array.html | 29 +++ 算法题/search-in-rotated-sorted-array.json | 172 ++++++++++++++++++ 算法题/second-highest-salary.html | 57 ++++++ 算法题/second-highest-salary.json | 82 +++++++++ 算法题/the-dining-philosophers.html | 46 +++++ 算法题/the-dining-philosophers.json | 75 ++++++++ 8 files changed, 528 insertions(+), 4 deletions(-) create mode 100644 算法题/[no content]team-scores-in-football-tournament.json create mode 100644 算法题/search-in-rotated-sorted-array.html create mode 100644 算法题/search-in-rotated-sorted-array.json create mode 100644 算法题/second-highest-salary.html create mode 100644 算法题/second-highest-salary.json create mode 100644 算法题/the-dining-philosophers.html create mode 100644 算法题/the-dining-philosophers.json diff --git a/1.py b/1.py index 3a08520e..aa9d4b7d 100644 --- a/1.py +++ b/1.py @@ -8,8 +8,6 @@ import requests from requests.exceptions import RequestException from bs4 import BeautifulSoup -import random - def get_proble_set(url): try: response = requests.get(url) @@ -21,8 +19,8 @@ def get_proble_set(url): def parse_proble_set(problemSet): # print(len(problemSet)) # 2218 - # for i in range(len(problemSet)): - for i in range(930, len(problemSet)): + for i in range(len(problemSet)): + # for i in range(930, len(problemSet)): title = problemSet[i]["stat"]["question__title_slug"] if os.path.exists("[no content]{}.json".format(title)) or os.path.exists("{}.json".format(title)): print(i, "has been parsed.") diff --git a/算法题/[no content]team-scores-in-football-tournament.json b/算法题/[no content]team-scores-in-football-tournament.json new file mode 100644 index 00000000..eb4559fd --- /dev/null +++ b/算法题/[no content]team-scores-in-football-tournament.json @@ -0,0 +1,65 @@ +{ + "data": { + "question": { + "questionId": "1339", + "questionFrontendId": "1212", + "boundTopicId": null, + "title": "Team Scores in Football Tournament", + "titleSlug": "team-scores-in-football-tournament", + "content": null, + "translatedTitle": null, + "translatedContent": null, + "isPaidOnly": true, + "difficulty": "Medium", + "likes": 226, + "dislikes": 19, + "isLiked": null, + "similarQuestions": "[]", + "exampleTestcases": "{\"headers\": {\"Teams\": [\"team_id\", \"team_name\"], \"Matches\": [\"match_id\", \"host_team\", \"guest_team\", \"host_goals\", \"guest_goals\"]}, \"rows\": {\"Teams\": [[10, \"Leetcode FC\"], [20, \"NewYork FC\"], [30, \"Atlanta FC\"], [40, \"Chicago FC\"], [50, \"Toronto FC\"]], \"Matches\": [[1, 10, 20, 3, 0], [2, 30, 10, 2, 2], [3, 10, 50, 5, 1], [4, 20, 30, 1, 0], [5, 50, 30, 1, 0]]}}", + "categoryTitle": "Database", + "contributors": [], + "topicTags": [ + { + "name": "Database", + "slug": "database", + "translatedName": null, + "__typename": "TopicTagNode" + } + ], + "companyTagStats": null, + "codeSnippets": null, + "stats": "{\"totalAccepted\": \"24.2K\", \"totalSubmission\": \"42.2K\", \"totalAcceptedRaw\": 24194, \"totalSubmissionRaw\": 42249, \"acRate\": \"57.3%\"}", + "hints": [], + "solution": null, + "status": null, + "sampleTestCase": "{\"headers\": {\"Teams\": [\"team_id\", \"team_name\"], \"Matches\": [\"match_id\", \"host_team\", \"guest_team\", \"host_goals\", \"guest_goals\"]}, \"rows\": {\"Teams\": [[10, \"Leetcode FC\"], [20, \"NewYork FC\"], [30, \"Atlanta FC\"], [40, \"Chicago FC\"], [50, \"Toronto FC\"]], \"Matches\": [[1, 10, 20, 3, 0], [2, 30, 10, 2, 2], [3, 10, 50, 5, 1], [4, 20, 30, 1, 0], [5, 50, 30, 1, 0]]}}", + "metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Teams (team_id int, team_name varchar(30))\",\n \"Create table If Not Exists Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)\"\n ],\n \"mssql\": [\n \"Create table Teams (team_id int, team_name varchar(30))\",\n \"Create table Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)\"\n ],\n \"oraclesql\": [\n \"Create table Teams (team_id int, team_name varchar(30))\",\n \"Create table Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)\"\n ],\n \"database\": true\n}", + "judgerAvailable": true, + "judgeType": "large", + "mysqlSchemas": [ + "Create table If Not Exists Teams (team_id int, team_name varchar(30))", + "Create table If Not Exists Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)", + "Truncate table Teams", + "insert into Teams (team_id, team_name) values ('10', 'Leetcode FC')", + "insert into Teams (team_id, team_name) values ('20', 'NewYork FC')", + "insert into Teams (team_id, team_name) values ('30', 'Atlanta FC')", + "insert into Teams (team_id, team_name) values ('40', 'Chicago FC')", + "insert into Teams (team_id, team_name) values ('50', 'Toronto FC')", + "Truncate table Matches", + "insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('1', '10', '20', '3', '0')", + "insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('2', '30', '10', '2', '2')", + "insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('3', '10', '50', '5', '1')", + "insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('4', '20', '30', '1', '0')", + "insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('5', '50', '30', '1', '0')" + ], + "enableRunCode": true, + "enableTestMode": false, + "enableDebugger": false, + "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.
There is an integer array nums
sorted in ascending order (with distinct values).
Prior to being passed to your function, nums
is possibly rotated at an unknown pivot index k
(1 <= k < nums.length
) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(0-indexed). For example, [0,1,2,4,5,6,7]
might be rotated at pivot index 3
and become [4,5,6,7,0,1,2]
.
Given the array nums
after the possible rotation and an integer target
, return the index of target
if it is in nums
, or -1
if it is not in nums
.
You must write an algorithm with O(log n)
runtime complexity.
+
Example 1:
+Input: nums = [4,5,6,7,0,1,2], target = 0 +Output: 4 +
Example 2:
+Input: nums = [4,5,6,7,0,1,2], target = 3 +Output: -1 +
Example 3:
+Input: nums = [1], target = 0 +Output: -1 ++
+
Constraints:
+ +1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums
are unique.nums
is an ascending array that is possibly rotated.-104 <= target <= 104
There is an integer array nums
sorted in ascending order (with distinct values).
Prior to being passed to your function, nums
is possibly rotated at an unknown pivot index k
(1 <= k < nums.length
) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(0-indexed). For example, [0,1,2,4,5,6,7]
might be rotated at pivot index 3
and become [4,5,6,7,0,1,2]
.
Given the array nums
after the possible rotation and an integer target
, return the index of target
if it is in nums
, or -1
if it is not in nums
.
You must write an algorithm with O(log n)
runtime complexity.
\n
Example 1:
\nInput: nums = [4,5,6,7,0,1,2], target = 0\nOutput: 4\n
Example 2:
\nInput: nums = [4,5,6,7,0,1,2], target = 3\nOutput: -1\n
Example 3:
\nInput: nums = [1], target = 0\nOutput: -1\n\n
\n
Constraints:
\n\n1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums
are unique.nums
is an ascending array that is possibly rotated.-104 <= target <= 104
Compiled with clang 11
using the latest C++ 17 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \" OpenJDK 17
. Java 8 features such as lambda expressions and stream API can be used.
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"c\": [\"C\", \"Compiled with gcc 8.2
using the gnu99 standard.
Your code is compiled with level one optimization (-O1
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\\r\\n\\r\\nFor hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:
\\r\\n\\r\\n1. Adding an item to a hash.\\r\\n
\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
2. Looking up an item in a hash:\\r\\n
\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
3. Deleting an item in a hash:\\r\\n
\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n\\r\\n\"], \"csharp\": [\"C#\", \"\\r\\n\\r\\n
Your code is compiled with debug flag enabled (/debug
).
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use datastructures-js/priority-queue and datastructures-js/queue.
\"], \"ruby\": [\"Ruby\", \"Ruby 3.1
Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms
\"], \"swift\": [\"Swift\", \"Swift 5.5.2
.
Go 1.17.6
.
Support https://godoc.org/github.com/emirpasic/gods library.
\"], \"python3\": [\"Python3\", \"Python 3.10
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"], \"scala\": [\"Scala\", \"Scala 2.13.7
.
Kotlin 1.3.10
.
Rust 1.58.1
Supports rand v0.6\\u00a0from crates.io
\"], \"php\": [\"PHP\", \"PHP 8.1
.
With bcmath module
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"], \"racket\": [\"Racket\", \"Run with Racket 8.3
.
Table: Employee
++-------------+------+ +| Column Name | Type | ++-------------+------+ +| id | int | +| salary | int | ++-------------+------+ +id is the primary key column for this table. +Each row of this table contains information about the salary of an employee. ++ +
+ +
Write an SQL query to report the second highest salary from the Employee
table. If there is no second highest salary, the query should report null
.
The query result format is in the following example.
+ ++
Example 1:
+ ++Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| 200 | ++---------------------+ ++ +
Example 2:
+ ++Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| null | ++---------------------+ +diff --git a/算法题/second-highest-salary.json b/算法题/second-highest-salary.json new file mode 100644 index 00000000..466436e5 --- /dev/null +++ b/算法题/second-highest-salary.json @@ -0,0 +1,82 @@ +{ + "data": { + "question": { + "questionId": "176", + "questionFrontendId": "176", + "boundTopicId": null, + "title": "Second Highest Salary", + "titleSlug": "second-highest-salary", + "content": "
Table: Employee
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| id | int |\n| salary | int |\n+-------------+------+\nid is the primary key column for this table.\nEach row of this table contains information about the salary of an employee.\n\n\n
\n\n
Write an SQL query to report the second highest salary from the Employee
table. If there is no second highest salary, the query should report null
.
The query result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nEmployee table:\n+----+--------+\n| id | salary |\n+----+--------+\n| 1 | 100 |\n| 2 | 200 |\n| 3 | 300 |\n+----+--------+\nOutput: \n+---------------------+\n| SecondHighestSalary |\n+---------------------+\n| 200 |\n+---------------------+\n\n\n
Example 2:
\n\n\nInput: \nEmployee table:\n+----+--------+\n| id | salary |\n+----+--------+\n| 1 | 100 |\n+----+--------+\nOutput: \n+---------------------+\n| SecondHighestSalary |\n+---------------------+\n| null |\n+---------------------+\n\n", + "translatedTitle": null, + "translatedContent": null, + "isPaidOnly": false, + "difficulty": "Medium", + "likes": 1716, + "dislikes": 695, + "isLiked": null, + "similarQuestions": "[]", + "exampleTestcases": "{\"headers\":{\"Employee\":[\"id\",\"salary\"]},\"rows\":{\"Employee\":[[1,100],[2,200],[3,300]]}}\n{\"headers\":{\"Employee\":[\"id\",\"salary\"]},\"rows\":{\"Employee\":[[1,100]]}}", + "categoryTitle": "Database", + "contributors": [], + "topicTags": [ + { + "name": "Database", + "slug": "database", + "translatedName": null, + "__typename": "TopicTagNode" + } + ], + "companyTagStats": null, + "codeSnippets": [ + { + "lang": "MySQL", + "langSlug": "mysql", + "code": "# Write your MySQL query statement below\n", + "__typename": "CodeSnippetNode" + }, + { + "lang": "MS SQL Server", + "langSlug": "mssql", + "code": "/* Write your T-SQL query statement below */\n", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Oracle", + "langSlug": "oraclesql", + "code": "/* Write your PL/SQL query statement below */\n", + "__typename": "CodeSnippetNode" + } + ], + "stats": "{\"totalAccepted\": \"452.9K\", \"totalSubmission\": \"1.3M\", \"totalAcceptedRaw\": 452894, \"totalSubmissionRaw\": 1289634, \"acRate\": \"35.1%\"}", + "hints": [], + "solution": { + "id": "217", + "canSeeDetail": true, + "paidOnly": false, + "hasVideoSolution": false, + "paidOnlyVideo": true, + "__typename": "ArticleNode" + }, + "status": null, + "sampleTestCase": "{\"headers\":{\"Employee\":[\"id\",\"salary\"]},\"rows\":{\"Employee\":[[1,100],[2,200],[3,300]]}}", + "metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Employee (id int, salary int)\"\n ],\n \"mssql\": [\n \"Create table Employee (id int, salary int)\"\n ],\n \"oraclesql\": [\n \"Create table Employee (id int, salary int)\"\n ],\n \"database\": true\n}", + "judgerAvailable": true, + "judgeType": "large", + "mysqlSchemas": [ + "Create table If Not Exists Employee (id int, salary int)", + "Truncate table Employee", + "insert into Employee (id, salary) values ('1', '100')", + "insert into Employee (id, salary) values ('2', '200')", + "insert into Employee (id, salary) values ('3', '300')" + ], + "enableRunCode": true, + "enableTestMode": false, + "enableDebugger": false, + "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.
Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.
+ +Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.
+ +Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.
+ +Design a discipline of behaviour (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.
+ + + +The problem statement and the image above are taken from wikipedia.org
+ ++ +
The philosophers' ids are numbered from 0 to 4 in a clockwise order. Implement the function void wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)
where:
philosopher
is the id of the philosopher who wants to eat.pickLeftFork
and pickRightFork
are functions you can call to pick the corresponding forks of that philosopher.eat
is a function you can call to let the philosopher eat once he has picked both forks.putLeftFork
and putRightFork
are functions you can call to put down the corresponding forks of that philosopher.Five threads, each representing a philosopher, will simultaneously use one object of your class to simulate the process. The function may be called for the same philosopher more than once, even before the last call ends.
+ ++
Example 1:
+ ++Input: n = 1 +Output: [[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]] +Explanation: +n is the number of times each philosopher will call the function. +The output array describes the calls you made to the functions controlling the forks and the eat function, its format is: +output[i] = [a, b, c] (three integers) +- a is the id of a philosopher. +- b specifies the fork: {1 : left, 2 : right}. +- c specifies the operation: {1 : pick, 2 : put, 3 : eat}.+ +
+
Constraints:
+ +1 <= n <= 60
Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.
\n\nEach philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.
\n\nEating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.
\n\nDesign a discipline of behaviour (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.
\n\n\n\nThe problem statement and the image above are taken from wikipedia.org
\n\n\n\n
The philosophers' ids are numbered from 0 to 4 in a clockwise order. Implement the function void wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)
where:
philosopher
is the id of the philosopher who wants to eat.pickLeftFork
and pickRightFork
are functions you can call to pick the corresponding forks of that philosopher.eat
is a function you can call to let the philosopher eat once he has picked both forks.putLeftFork
and putRightFork
are functions you can call to put down the corresponding forks of that philosopher.Five threads, each representing a philosopher, will simultaneously use one object of your class to simulate the process. The function may be called for the same philosopher more than once, even before the last call ends.
\n\n\n
Example 1:
\n\n\nInput: n = 1\nOutput: [[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]]\nExplanation:\nn is the number of times each philosopher will call the function.\nThe output array describes the calls you made to the functions controlling the forks and the eat function, its format is:\noutput[i] = [a, b, c] (three integers)\n- a is the id of a philosopher.\n- b specifies the fork: {1 : left, 2 : right}.\n- c specifies the operation: {1 : pick, 2 : put, 3 : eat}.\n\n
\n
Constraints:
\n\n1 <= n <= 60
Compiled with clang 11
using the latest C++ 17 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \" OpenJDK 17
. Java 8 features such as lambda expressions and stream API can be used.
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"python3\": [\"Python3\", \"Python 3.10
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"]}", + "libraryUrl": null, + "adminUrl": null, + "challengeQuestion": null, + "__typename": "QuestionNode" + } + } +} \ No newline at end of file