mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
docs: add new problem documentation pages and update redirects
This commit is contained in:
1 parent
ea44a99c40
commit
f1a952f2bd
7 files changed
+358
No files matched your search
+68
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: '380. Insert Delete GetRandom O(1)'
|
||||
description: 'Implement the RandomizedSet class:'
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Array", slug: "array" }
|
||||
- { name: "Hash Table", slug: "hash-table" }
|
||||
- { name: "Math", slug: "math" }
|
||||
- { name: "Design", slug: "design" }
|
||||
- { name: "Randomized", slug: "randomized" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/90"
|
||||
sidebar:
|
||||
label: 'Insert Delete GetRandom O(1)'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
You must implement the functions of the class such that each function works in average O(1) time complexity.
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: ``
|
||||
- Output: ``
|
||||
- Explanation: Input ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"] `[[]`, `[1]`, `[2]`, `[2]`, `[]`, `[1]`, `[2]`, []] Output [null, true, false, true, `2`, true, false, 2] Explanation RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); // Inserts `1` to the set. Returns true as `1` was inserted successfully. randomizedSet.remove(2); // Returns false as `2` does not exist in the set. randomizedSet.insert(2); // Inserts `2` to the set, returns true. Set now contains `[1,2]`. randomizedSet.getRandom(); // getRandom() should return either `1` or `2` randomly. randomizedSet.remove(1); // Removes `1` from the set, returns true. Set now contains `[2]`. randomizedSet.insert(2); // `2` was already in the set, so return false. randomizedSet.getRandom(); // Since `2` is the only number in the set, getRandom() will always return `2`.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `-2^31 <= val <= 2^31 - 1`
|
||||
- At most 2 * 10^5 calls will be made to insert, remove, and getRandom.
|
||||
- There will be at least one element in the data structure when getRandom is called.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
from random import choice
|
||||
|
||||
|
||||
class RandomizedSet:
|
||||
def __init__(self):
|
||||
self.list = []
|
||||
self.dict = {}
|
||||
|
||||
def insert(self, val: int) -> bool:
|
||||
if val in self.dict:
|
||||
return False
|
||||
self.dict[val] = len(self.list)
|
||||
self.list.append(val)
|
||||
return True
|
||||
|
||||
def remove(self, val: int) -> bool:
|
||||
if val in self.dict:
|
||||
last, idx = self.list[-1], self.dict[val]
|
||||
self.list[idx], self.dict[last] = last, idx
|
||||
self.list.pop()
|
||||
del self.dict[val]
|
||||
return True
|
||||
return False
|
||||
|
||||
def getRandom(self) -> int:
|
||||
return choice(self.list)
|
||||
|
||||
|
||||
# Your RandomizedSet object will be instantiated and called as such:
|
||||
# obj = RandomizedSet()
|
||||
# param_1 = obj.insert(val)
|
||||
# param_2 = obj.remove(val)
|
||||
# param_3 = obj.getRandom()
|
||||
```
|
||||
@@ -0,0 +1,64 @@
|
||||
---
|
||||
title: '19. Remove Nth Node From End of List'
|
||||
description: Given the head of a linked list, remove the n^th node from the end of the list and return its head
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/80"
|
||||
sidebar:
|
||||
label: 'Remove Nth Node From End of List'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
Could you do this in one pass?
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5], n = 2`
|
||||
- Output: `[1,2,3,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1], n = 1`
|
||||
- Output: `[]`
|
||||
|
||||
### Example 3:
|
||||
- Input: `head = [1,2], n = 1`
|
||||
- Output: `[1]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is sz.
|
||||
- `1 <= sz <= 30`
|
||||
- `0 <= Node.val <= 100`
|
||||
- `1 <= n <= sz`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None:
|
||||
dummy = ListNode(0)
|
||||
dummy.next = head
|
||||
|
||||
fast = dummy
|
||||
for _ in range(n):
|
||||
if fast.next is None:
|
||||
return head
|
||||
fast = fast.next
|
||||
|
||||
slow = dummy
|
||||
while fast.next is not None:
|
||||
slow = slow.next
|
||||
fast = fast.next
|
||||
|
||||
slow.next = slow.next.next
|
||||
|
||||
return dummy.next
|
||||
```
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: '82. Remove Duplicates from Sorted List II'
|
||||
description: You are given the head of a sorted linked list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/84"
|
||||
sidebar:
|
||||
label: 'Remove Duplicates from Sorted List II'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,3,4,4,5]`
|
||||
- Output: `[1,2,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1,1,1,2,3]`
|
||||
- Output: `[2,3]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is in the range [0, 300].
|
||||
- `-100 <= Node.val <= 100`
|
||||
- The list is guaranteed to be sorted in ascending order.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def deleteDuplicates(self, head: ListNode | None) -> ListNode | None:
|
||||
sentinel = ListNode(0, head)
|
||||
pred = sentinel
|
||||
|
||||
while head:
|
||||
if head.next and head.val == head.next.val:
|
||||
while head.next and head.val == head.next.val:
|
||||
head = head.next
|
||||
|
||||
pred.next = head.next
|
||||
else:
|
||||
pred = pred.next
|
||||
|
||||
head = head.next
|
||||
|
||||
return sentinel.next
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: '876. Middle of the Linked List'
|
||||
description: Given the head of a singly linked list, return the middle node of the linked list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/82"
|
||||
sidebar:
|
||||
label: 'Middle of the Linked List'
|
||||
badge: 'Easy'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5]`
|
||||
- Output: `[3,4,5]`
|
||||
- Explanation: The middle node of the list is node `3`.
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1,2,3,4,5,6]`
|
||||
- Output: `[4,5,6]`
|
||||
- Explanation: Since the list has two middle nodes with values `3` and `4`, we return the second one.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is in the range [1, 100].
|
||||
- `1 <= Node.val <= 100`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def middleNode(self, head: ListNode | None) -> ListNode | None:
|
||||
slow = fast = head
|
||||
while fast and fast.next:
|
||||
fast = fast.next.next
|
||||
slow = slow.next
|
||||
|
||||
return slow
|
||||
```
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: '92. Reverse Linked List II'
|
||||
description: Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/83"
|
||||
sidebar:
|
||||
label: 'Reverse Linked List II'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
Could you do it in one pass?
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5], left = 2, right = 4`
|
||||
- Output: `[1,4,3,2,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [5], left = 1, right = 1`
|
||||
- Output: `[5]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is n.
|
||||
- `1 <= n <= 500`
|
||||
- `-500 <= Node.val <= 500`
|
||||
- `1 <= left <= right <= n`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def reverseBetween(
|
||||
self, head: ListNode | None, left: int, right: int
|
||||
) -> ListNode | None:
|
||||
cur, prev = head, None
|
||||
while left > 1:
|
||||
prev = cur
|
||||
cur = cur.next
|
||||
left, right = left - 1, right - 1
|
||||
|
||||
tail, con = cur, prev
|
||||
while right:
|
||||
third = cur.next
|
||||
cur.next = prev
|
||||
prev = cur
|
||||
cur = third
|
||||
right -= 1
|
||||
|
||||
if con:
|
||||
con.next = prev
|
||||
else:
|
||||
head = prev
|
||||
|
||||
tail.next = cur
|
||||
return head
|
||||
```
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
title: '36. Valid Sudoku'
|
||||
description: 'Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:'
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Array", slug: "array" }
|
||||
- { name: "Hash Table", slug: "hash-table" }
|
||||
- { name: "Matrix", slug: "matrix" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/94"
|
||||
sidebar:
|
||||
label: 'Valid Sudoku'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]]`
|
||||
- Output: `true`
|
||||
|
||||
### Example 2:
|
||||
- Input: `board = [["8","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]]`
|
||||
- Output: `false`
|
||||
- Explanation: Same as Example `1`, except with the `5` in the top left corner being modified to `8`. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `board.length == 9`
|
||||
- `board[i].length == 9`
|
||||
- board[i][j] is a digit 1-9 or '.'.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
import collections
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValidSudoku(self, board: list[list[str]]) -> bool:
|
||||
cols = collections.defaultdict(set)
|
||||
rows = collections.defaultdict(set)
|
||||
squares = collections.defaultdict(set) # (r/3, c/3)
|
||||
|
||||
for r in range(9):
|
||||
for c in range(9):
|
||||
if board[r][c] == ".":
|
||||
continue
|
||||
if (
|
||||
board[r][c] in rows[r]
|
||||
or board[r][c] in cols[c]
|
||||
or board[r][c] in squares[(r // 3, c // 3)]
|
||||
):
|
||||
return False
|
||||
cols[c].add(board[r][c])
|
||||
rows[r].add(board[r][c])
|
||||
squares[(r // 3, c // 3)].add(board[r][c])
|
||||
|
||||
return True
|
||||
```
|
||||
@@ -7,12 +7,14 @@ export default [
|
||||
{ from: "/4", to: "/binary-search/median-of-two-sorted-arrays" },
|
||||
{ from: "/11", to: "/two-pointers/container-with-most-water" },
|
||||
{ from: "/15", to: "/two-pointers/3sum" },
|
||||
{ from: "/19", to: "/linked-list/remove-nth-node-from-end-of-list" },
|
||||
{ from: "/20", to: "/stack/valid-parentheses" },
|
||||
{ from: "/21", to: "/linked-list/merge-two-sorted-lists" },
|
||||
{ from: "/23", to: "/heap-priority-queue/merge-k-sorted-lists" },
|
||||
{ from: "/33", to: "/binary-search/search-in-rotated-sorted-array" },
|
||||
{ from: "/34", to: "/binary-search/find-first-and-last-position-of-element-in-sorted-array" },
|
||||
{ from: "/35", to: "/binary-search/search-insert-position" },
|
||||
{ from: "/36", to: "/matrix-index-math/valid-sudoku" },
|
||||
{ from: "/39", to: "/backtracking/combination-sum" },
|
||||
{ from: "/42", to: "/two-pointers/trapping-rain-water" },
|
||||
{ from: "/46", to: "/backtracking/permutations" },
|
||||
@@ -32,7 +34,9 @@ export default [
|
||||
{ from: "/76", to: "/sliding-window/minimum-window-substring" },
|
||||
{ from: "/78", to: "/backtracking/subsets" },
|
||||
{ from: "/80", to: "/two-pointers/remove-duplicates-from-sorted-array-ii" },
|
||||
{ from: "/82", to: "/linked-list/remove-duplicates-from-sorted-list-ii" },
|
||||
{ from: "/84", to: "/monotonic-stack/largest-rectangle-in-histogram" },
|
||||
{ from: "/92", to: "/linked-list/reverse-linked-list-ii" },
|
||||
{ from: "/98", to: "/binary-search-tree/validate-binary-search-tree" },
|
||||
{ from: "/102", to: "/tree-bfs/binary-tree-level-order-traversal" },
|
||||
{ from: "/104", to: "/tree-dfs/maximum-depth-of-binary-tree" },
|
||||
@@ -75,6 +79,7 @@ export default [
|
||||
{ from: "/323", to: "/union-find/number-of-connected-components-in-an-undirected-graph" },
|
||||
{ from: "/344", to: "/two-pointers/reverse-string" },
|
||||
{ from: "/347", to: "/hash-table/top-k-frequent-elements" },
|
||||
{ from: "/380", to: "/hybrid-structures/insert-delete-getrandom-o1" },
|
||||
{ from: "/387", to: "/hash-table/first-unique-character-in-a-string" },
|
||||
{ from: "/392", to: "/two-pointers/is-subsequence" },
|
||||
{ from: "/417", to: "/graph-dfs/pacific-atlantic-water-flow" },
|
||||
@@ -91,6 +96,7 @@ export default [
|
||||
{ from: "/739", to: "/monotonic-stack/daily-temperatures" },
|
||||
{ from: "/853", to: "/monotonic-stack/car-fleet" },
|
||||
{ from: "/875", to: "/binary-search/koko-eating-bananas" },
|
||||
{ from: "/876", to: "/linked-list/middle-of-the-linked-list" },
|
||||
{ from: "/901", to: "/monotonic-stack/online-stock-span" },
|
||||
{ from: "/973", to: "/heap-priority-queue/k-closest-points-to-origin" },
|
||||
{ from: "/974", to: "/prefix-sum/subarray-sums-divisible-by-k" },
|
||||
|
||||
Reference in new issue
Block a user