Skip to content

Commit 2de1b51

Browse files
authored
feat: add solutions for lc problems (#5004)
1 parent 57da1fc commit 2de1b51

9 files changed

Lines changed: 83 additions & 11 deletions

File tree

solution/3800-3899/3827.Count Monobit Integers/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3827.Co
6666

6767
<!-- solution:start -->
6868

69-
### 方法一
69+
### 方法一:模拟
70+
71+
根据题目描述,单比特整数,要么是 $0$,要么其二进制表示中所有位都是 $1$。
72+
73+
因此,我们首先将 $0$ 计入答案中,然后从 $1$ 开始,依次生成二进制表示中所有位都是 $1$ 的整数,直到该整数大于 $n$ 为止。
74+
75+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
7076

7177
<!-- tabs:start -->
7278

@@ -79,7 +85,7 @@ class Solution:
7985
i = 1
8086
while x <= n:
8187
ans += 1
82-
x += (1 << i)
88+
x += 1 << i
8389
i += 1
8490
return ans
8591
```

solution/3800-3899/3827.Count Monobit Integers/README_EN.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3827.Co
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Simulation
68+
69+
According to the problem description, a Monobit integer is either $0$, or its binary representation consists of all $1$s.
70+
71+
Therefore, we first include $0$ in the answer, then starting from $1$, we sequentially generate integers whose binary representations consist of all $1$s, until the integer exceeds $n$.
72+
73+
The time complexity is $O(\log n)$ and the space complexity is $O(1)$.
6874

6975
<!-- tabs:start -->
7076

@@ -77,7 +83,7 @@ class Solution:
7783
i = 1
7884
while x <= n:
7985
ans += 1
80-
x += (1 << i)
86+
x += 1 << i
8187
i += 1
8288
return ans
8389
```

solution/3800-3899/3827.Count Monobit Integers/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def countMonobit(self, n: int) -> int:
44
i = 1
55
while x <= n:
66
ans += 1
7-
x += (1 << i)
7+
x += 1 << i
88
i += 1
99
return ans

solution/3800-3899/3828.Final Element After Subarray Deletions/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3828.Fi
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:脑筋急转弯
79+
80+
由于 Alice 先手,Alice 可以选择移除除第一个元素和最后一个元素之外的所有元素,那么答案至少为 $\max(nums[0], nums[n - 1])$。
81+
82+
而对于 $n = 1,2,..n-2$ 的情况,即便 Alice 想要保留这中间的某个元素,Bob 也可以选择移除它,因此答案至多为 $\max(nums[0], nums[n - 1])$。
83+
84+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
7985

8086
<!-- tabs:start -->
8187

solution/3800-3899/3828.Final Element After Subarray Deletions/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3828.Fi
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Brain Teaser
77+
78+
Since Alice goes first, Alice can choose to remove all elements except the first and last elements, so the answer is at least $\max(nums[0], nums[n - 1])$.
79+
80+
For the cases of elements at indices $1, 2, ..., n-2$ (the middle elements), even if Alice wants to keep any of these middle elements, Bob can choose to remove it, so the answer is at most $\max(nums[0], nums[n - 1])$.
81+
82+
Therefore, the answer is exactly $\max(nums[0], nums[n - 1])$.
83+
84+
The time complexity is $O(1)$ and the space complexity is $O(1)$.
7785

7886
<!-- tabs:start -->
7987

solution/3800-3899/3829.Design Ride Sharing System/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,20 @@ rideSharingSystem.matchDriverWithRider(); // 返回 [-1, -1]</div>
8787

8888
<!-- solution:start -->
8989

90-
### 方法一
90+
### 方法一:有序集合 + 哈希表
91+
92+
我们使用两个有序集合 $\textit{riders}$ 和 $\textit{drivers}$ 分别存储等待的乘客和空闲的司机。其中,每个元素为一个二元组 $(t, \textit{id})$,表示该乘客/司机的 ID 以及其加入系统的时间戳 $t$。时间戳 $t$ 用于区分先后顺序,初始时 $t = 0$,每次添加乘客或司机时,$t$ 自增 $1$。
93+
94+
此外,我们使用哈希表 $\textit{d}$ 存储每个乘客的 ID 和其时间戳的映射关系,方便在取消乘客请求时进行查找。
95+
96+
具体地:
97+
98+
- 在添加乘客时,我们将 $(t, \textit{riderId})$ 添加到 $\textit{riders}$ 中,并将 $\textit{d}[\textit{riderId}] = t$,然后将 $t$ 自增 $1$。
99+
- 在添加司机时,我们将 $(t, \textit{driverId})$ 添加到 $\textit{drivers}$ 中,然后将 $t$ 自增 $1$。
100+
- 在匹配司机和乘客时,如果 $\textit{riders}$ 或 $\textit{drivers}$ 为空,则返回 $[-1, -1]$。否则,我们分别取出 $\textit{riders}$ 和 $\textit{drivers}$ 中时间戳最小的元素 $(t_r, \textit{riderId})$ 和 $(t_d, \textit{driverId})$,将它们从各自的集合中移除,并返回 $[\textit{driverId}, \textit{riderId}]$。
101+
- 在取消乘客请求时,我们通过 $\textit{d}$ 查找该乘客的时间戳 $t$,然后将 $(t, \textit{riderId})$ 从 $\textit{riders}$ 中移除。
102+
103+
时间复杂度为每次操作 $O(\log n)$,其中 $n$ 是当前乘客或司机的数量。空间复杂度为 $O(n)$。
91104

92105
<!-- tabs:start -->
93106

solution/3800-3899/3829.Design Ride Sharing System/README_EN.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,20 @@ rideSharingSystem.matchDriverWithRider(); // returns [-1, -1]</div>
8585

8686
<!-- solution:start -->
8787

88-
### Solution 1
88+
### Solution 1: Sorted Set + Hash Table
89+
90+
We use two sorted sets $\textit{riders}$ and $\textit{drivers}$ to store waiting riders and available drivers respectively. Each element is a tuple $(t, \textit{id})$, representing the ID of the rider/driver and their timestamp $t$ when they joined the system. The timestamp $t$ is used to distinguish the order of arrival. Initially, $t = 0$, and each time a rider or driver is added, $t$ is incremented by $1$.
91+
92+
Additionally, we use a hash table $\textit{d}$ to store the mapping between each rider's ID and their timestamp, which facilitates lookup when canceling a rider's request.
93+
94+
Specifically:
95+
96+
- When adding a rider, we add $(t, \textit{riderId})$ to $\textit{riders}$, set $\textit{d}[\textit{riderId}] = t$, and then increment $t$ by $1$.
97+
- When adding a driver, we add $(t, \textit{driverId})$ to $\textit{drivers}$ and then increment $t$ by $1$.
98+
- When matching a driver with a rider, if either $\textit{riders}$ or $\textit{drivers}$ is empty, we return $[-1, -1]$. Otherwise, we remove the elements with the smallest timestamps from both $\textit{riders}$ and $\textit{drivers}$, namely $(t_r, \textit{riderId})$ and $(t_d, \textit{driverId})$, and return $[\textit{driverId}, \textit{riderId}]$.
99+
- When canceling a rider's request, we look up the rider's timestamp $t$ through $\textit{d}$, and then remove $(t, \textit{riderId})$ from $\textit{riders}$.
100+
101+
The time complexity is $O(\log n)$ per operation, where $n$ is the current number of riders or drivers. The space complexity is $O(n)$.
89102

90103
<!-- tabs:start -->
91104

solution/3800-3899/3830.Longest Alternating Subarray After Removing At Most One Element/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3830.Lo
9696

9797
<!-- solution:start -->
9898

99-
### 方法一
99+
### 方法一:前后缀分解 + 枚举
100+
101+
我们用两个数组 $l_1$ 和 $l_2$ 分别表示以位置 $i$ 结尾且最后一次比较为“<”和“>”的最长交替子数组的长度。同理,我们用 $r_1$ 和 $r_2$ 分别表示以位置 $i$ 开头且第一次比较为“<”和“>”的最长交替子数组的长度。
102+
103+
我们可以通过一次从左到右的遍历计算出 $l_1$ 和 $l_2$,再通过一次从右到左的遍历计算出 $r_1$ 和 $r_2$。
104+
105+
接下来,我们初始化答案为 $\max(\max(l_1), \max(l_2))$,表示不移除任何元素的情况下的最长交替子数组长度。
106+
107+
然后,我们枚举要移除的元素位置 $i$,如果移除位置 $i$ 后,位置 $i-1$ 和位置 $i+1$ 仍然可以构成交替关系,那么我们可以将 $l_1[i-1]$ 和 $r_1[i+1]$(或 $l_2[i-1]$ 和 $r_2[i+1]$)相加,更新答案。
108+
109+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
100110

101111
<!-- tabs:start -->
102112

solution/3800-3899/3830.Longest Alternating Subarray After Removing At Most One Element/README_EN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3830.Lo
9494

9595
<!-- solution:start -->
9696

97-
### Solution 1
97+
### Solution 1: Prefix-Suffix Decomposition + Enumeration
98+
99+
We use two arrays $l_1$ and $l_2$ to represent the length of the longest alternating subarray ending at position $i$ with the last comparison being "<" and ">", respectively. Similarly, we use $r_1$ and $r_2$ to represent the length of the longest alternating subarray starting at position $i$ with the first comparison being "<" and ">", respectively.
100+
101+
We can compute $l_1$ and $l_2$ through a single left-to-right traversal, and then compute $r_1$ and $r_2$ through a single right-to-left traversal.
102+
103+
Next, we initialize the answer as $\max(\max(l_1), \max(l_2))$, which represents the length of the longest alternating subarray without removing any elements.
104+
105+
Then, we enumerate the position $i$ of the element to be removed. If after removing position $i$, positions $i-1$ and $i+1$ can still form an alternating relationship, we can add $l_1[i-1]$ and $r_1[i+1]$ (or $l_2[i-1]$ and $r_2[i+1]$) together to update the answer.
106+
107+
The time complexity is $O(n)$ and the space complexity is $O(n)$.
98108

99109
<!-- tabs:start -->
100110

0 commit comments

Comments
 (0)