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\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"]}", + "libraryUrl": null, + "adminUrl": null, + "challengeQuestion": null, + "__typename": "QuestionNode" + } + } +} \ No newline at end of file diff --git a/算法题/search-in-rotated-sorted-array.html b/算法题/search-in-rotated-sorted-array.html new file mode 100644 index 00000000..58c7ac24 --- /dev/null +++ b/算法题/search-in-rotated-sorted-array.html @@ -0,0 +1,29 @@ +

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:

+ + diff --git a/算法题/search-in-rotated-sorted-array.json b/算法题/search-in-rotated-sorted-array.json new file mode 100644 index 00000000..530de013 --- /dev/null +++ b/算法题/search-in-rotated-sorted-array.json @@ -0,0 +1,172 @@ +{ + "data": { + "question": { + "questionId": "33", + "questionFrontendId": "33", + "boundTopicId": null, + "title": "Search in Rotated Sorted Array", + "titleSlug": "search-in-rotated-sorted-array", + "content": "

There is an integer array nums sorted in ascending order (with distinct values).

\n\n

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].

\n\n

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.

\n\n

You must write an algorithm with O(log n) runtime complexity.

\n\n

 

\n

Example 1:

\n
Input: nums = [4,5,6,7,0,1,2], target = 0\nOutput: 4\n

Example 2:

\n
Input: nums = [4,5,6,7,0,1,2], target = 3\nOutput: -1\n

Example 3:

\n
Input: nums = [1], target = 0\nOutput: -1\n
\n

 

\n

Constraints:

\n\n\n", + "translatedTitle": null, + "translatedContent": null, + "isPaidOnly": false, + "difficulty": "Medium", + "likes": 13266, + "dislikes": 866, + "isLiked": null, + "similarQuestions": "[{\"title\": \"Search in Rotated Sorted Array II\", \"titleSlug\": \"search-in-rotated-sorted-array-ii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Find Minimum in Rotated Sorted Array\", \"titleSlug\": \"find-minimum-in-rotated-sorted-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Pour Water Between Buckets to Make Water Levels Equal\", \"titleSlug\": \"pour-water-between-buckets-to-make-water-levels-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", + "exampleTestcases": "[4,5,6,7,0,1,2]\n0\n[4,5,6,7,0,1,2]\n3\n[1]\n0", + "categoryTitle": "Algorithms", + "contributors": [], + "topicTags": [ + { + "name": "Array", + "slug": "array", + "translatedName": null, + "__typename": "TopicTagNode" + }, + { + "name": "Binary Search", + "slug": "binary-search", + "translatedName": null, + "__typename": "TopicTagNode" + } + ], + "companyTagStats": null, + "codeSnippets": [ + { + "lang": "C++", + "langSlug": "cpp", + "code": "class Solution {\npublic:\n int search(vector& nums, int target) {\n \n }\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Java", + "langSlug": "java", + "code": "class Solution {\n public int search(int[] nums, int target) {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python", + "langSlug": "python", + "code": "class Solution(object):\n def search(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n ", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python3", + "langSlug": "python3", + "code": "class Solution:\n def search(self, nums: List[int], target: int) -> int:\n ", + "__typename": "CodeSnippetNode" + }, + { + "lang": "C", + "langSlug": "c", + "code": "\n\nint search(int* nums, int numsSize, int target){\n\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "C#", + "langSlug": "csharp", + "code": "public class Solution {\n public int Search(int[] nums, int target) {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "JavaScript", + "langSlug": "javascript", + "code": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar search = function(nums, target) {\n \n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Ruby", + "langSlug": "ruby", + "code": "# @param {Integer[]} nums\n# @param {Integer} target\n# @return {Integer}\ndef search(nums, target)\n \nend", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Swift", + "langSlug": "swift", + "code": "class Solution {\n func search(_ nums: [Int], _ target: Int) -> Int {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Go", + "langSlug": "golang", + "code": "func search(nums []int, target int) int {\n \n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Scala", + "langSlug": "scala", + "code": "object Solution {\n def search(nums: Array[Int], target: Int): Int = {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Kotlin", + "langSlug": "kotlin", + "code": "class Solution {\n fun search(nums: IntArray, target: Int): Int {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Rust", + "langSlug": "rust", + "code": "impl Solution {\n pub fn search(nums: Vec, target: i32) -> i32 {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "PHP", + "langSlug": "php", + "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $target\n * @return Integer\n */\n function search($nums, $target) {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "TypeScript", + "langSlug": "typescript", + "code": "function search(nums: number[], target: number): number {\n\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Racket", + "langSlug": "racket", + "code": "(define/contract (search nums target)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Erlang", + "langSlug": "erlang", + "code": "-spec search(Nums :: [integer()], Target :: integer()) -> integer().\nsearch(Nums, Target) ->\n .", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Elixir", + "langSlug": "elixir", + "code": "defmodule Solution do\n @spec search(nums :: [integer], target :: integer) :: integer\n def search(nums, target) do\n\n end\nend", + "__typename": "CodeSnippetNode" + } + ], + "stats": "{\"totalAccepted\": \"1.4M\", \"totalSubmission\": \"3.7M\", \"totalAcceptedRaw\": 1396892, \"totalSubmissionRaw\": 3716064, \"acRate\": \"37.6%\"}", + "hints": [], + "solution": { + "id": "662", + "canSeeDetail": false, + "paidOnly": true, + "hasVideoSolution": false, + "paidOnlyVideo": true, + "__typename": "ArticleNode" + }, + "status": null, + "sampleTestCase": "[4,5,6,7,0,1,2]\n0", + "metaData": "{\r\n \"name\": \"search\",\r\n \"params\": [\r\n {\r\n \"name\": \"nums\",\r\n \"type\": \"integer[]\"\r\n },\r\n {\r\n \"name\": \"target\",\r\n \"type\": \"integer\"\r\n }\r\n ],\r\n \"return\": {\r\n \"type\": \"integer\"\r\n }\r\n}", + "judgerAvailable": true, + "judgeType": "large", + "mysqlSchemas": [], + "enableRunCode": true, + "enableTestMode": false, + "enableDebugger": true, + "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 17 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

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.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

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\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note 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.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O1). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. 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#\", \"

C# 10 with .NET 6 runtime

\\r\\n\\r\\n

Your code is compiled with debug flag enabled (/debug).

\"], \"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use datastructures-js/priority-queue and datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.1

\\r\\n\\r\\n

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.

\"], \"golang\": [\"Go\", \"

Go 1.17.6.

\\r\\n\\r\\n

Support https://godoc.org/github.com/emirpasic/gods library.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

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\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 2.13.7.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.3.10.

\"], \"rust\": [\"Rust\", \"

Rust 1.58.1

\\r\\n\\r\\n

Supports rand v0.6\\u00a0from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.1.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Run with Racket 8.3.

\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 24.2\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.0 with Erlang/OTP 24.2\"]}", + "libraryUrl": null, + "adminUrl": null, + "challengeQuestion": null, + "__typename": "QuestionNode" + } + } +} \ No newline at end of file diff --git a/算法题/second-highest-salary.html b/算法题/second-highest-salary.html new file mode 100644 index 00000000..4028c3be --- /dev/null +++ b/算法题/second-highest-salary.html @@ -0,0 +1,57 @@ +

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
\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.

\n\n

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\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"]}", + "libraryUrl": null, + "adminUrl": null, + "challengeQuestion": null, + "__typename": "QuestionNode" + } + } +} \ No newline at end of file diff --git a/算法题/the-dining-philosophers.html b/算法题/the-dining-philosophers.html new file mode 100644 index 00000000..7e459af6 --- /dev/null +++ b/算法题/the-dining-philosophers.html @@ -0,0 +1,46 @@ +

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:

+ + + +

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:

+ + diff --git a/算法题/the-dining-philosophers.json b/算法题/the-dining-philosophers.json new file mode 100644 index 00000000..95e2a5b9 --- /dev/null +++ b/算法题/the-dining-philosophers.json @@ -0,0 +1,75 @@ +{ + "data": { + "question": { + "questionId": "1340", + "questionFrontendId": "1226", + "boundTopicId": null, + "title": "The Dining Philosophers", + "titleSlug": "the-dining-philosophers", + "content": "

Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.

\n\n

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.

\n\n

Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.

\n\n

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.

\n\n

\"\"

\n\n

The 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:

\n\n\n\n

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\n\n", + "translatedTitle": null, + "translatedContent": null, + "isPaidOnly": false, + "difficulty": "Medium", + "likes": 175, + "dislikes": 198, + "isLiked": null, + "similarQuestions": "[]", + "exampleTestcases": "1", + "categoryTitle": "Concurrency", + "contributors": [], + "topicTags": [ + { + "name": "Concurrency", + "slug": "concurrency", + "translatedName": null, + "__typename": "TopicTagNode" + } + ], + "companyTagStats": null, + "codeSnippets": [ + { + "lang": "C++", + "langSlug": "cpp", + "code": "class DiningPhilosophers {\npublic:\n DiningPhilosophers() {\n \n }\n\n void wantsToEat(int philosopher,\n function pickLeftFork,\n function pickRightFork,\n function eat,\n function putLeftFork,\n function putRightFork) {\n\t\t\n }\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Java", + "langSlug": "java", + "code": "class DiningPhilosophers {\n\n public DiningPhilosophers() {\n \n }\n\n // call the run() method of any runnable to execute its code\n public void wantsToEat(int philosopher,\n Runnable pickLeftFork,\n Runnable pickRightFork,\n Runnable eat,\n Runnable putLeftFork,\n Runnable putRightFork) throws InterruptedException {\n \n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python", + "langSlug": "python", + "code": "class DiningPhilosophers(object):\n\n # call the functions directly to execute, for example, eat()\n def wantsToEat(self, philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork):\n \"\"\"\n :type philosopher: int\n :type pickLeftFork: method\n :type pickRightFork: method\n :type eat: method\n :type putLeftFork: method\n :type putRightFork: method\n :rtype: void\n \"\"\"\n ", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python3", + "langSlug": "python3", + "code": "class DiningPhilosophers:\n\n # call the functions directly to execute, for example, eat()\n def wantsToEat(self,\n philosopher: int,\n pickLeftFork: 'Callable[[], None]',\n pickRightFork: 'Callable[[], None]',\n eat: 'Callable[[], None]',\n putLeftFork: 'Callable[[], None]',\n putRightFork: 'Callable[[], None]') -> None:\n ", + "__typename": "CodeSnippetNode" + } + ], + "stats": "{\"totalAccepted\": \"18.5K\", \"totalSubmission\": \"32.1K\", \"totalAcceptedRaw\": 18505, \"totalSubmissionRaw\": 32071, \"acRate\": \"57.7%\"}", + "hints": [], + "solution": null, + "status": null, + "sampleTestCase": "1", + "metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"target\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"java\",\n \"python\",\n \"python3\",\n \"cpp\"\n ],\n \"manual\": true\n}", + "judgerAvailable": true, + "judgeType": "large", + "mysqlSchemas": [], + "enableRunCode": true, + "enableTestMode": false, + "enableDebugger": false, + "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 17 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

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.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

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\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

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\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"]}", + "libraryUrl": null, + "adminUrl": null, + "challengeQuestion": null, + "__typename": "QuestionNode" + } + } +} \ No newline at end of file