Skip to content

Commit b8d62db

Browse files
committed
add more easy problem solutions
1 parent cfbdf2d commit b8d62db

9 files changed

+1938
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LeetCode
55

66
| # | Title | Solution | Difficulty |
77
|---| ----- | -------- | ---------- |
8+
|1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [Python](algorithms/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.ipynb)|Medium|
89
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Python](algorithms/1143-longest-common-subsequence.ipynb)|Medium|
910
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/) | [Python](algorithms/1004-max-consecutive-ones-iii.ipynb)|Medium|
1011
| 995|[Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [Python](algorithms/995-minimum-number-of-k-consecutive-bit-flips.ipynb)|Hard|
@@ -26,8 +27,15 @@ LeetCode
2627
| 468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address/) | [Python](algorithms/468-validate-ip-address.ipynb)|Medium|
2728
| 454|[4Sum II](https://leetcode.com/problems/4sum-ii/) | [Python](algorithms/454-4sum-ii.py)|Medium|
2829
| 445|[Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Python](algorithms/445-add-two-numbers-ii.ipynb)|Medium|
30+
| 441|[Arranging Coins](https://leetcode.com/problems/arranging-coins/) | [Python](algorithms/441-arranging-coins.ipynb)|Easy|
2931
| 429|[N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [Python](algorithms/429-n-ary-tree-level-order-traversal.ipynb) | Medium |
32+
| 415|[Add Strings](https://leetcode.com/problems/add-strings/) | [Python](algorithms/415-add-strings.ipynb)|Easy|
33+
| 414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](algorithms/414-third-maximum-number.ipynb) |Easy|
3034
| 409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Python](algorithms/409-longest-palindrome.ipynb)|Easy|
35+
| 405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Python](algorithms/405-convert-a-number-to-hexadecimal.ipynb)|Easy|
36+
| 401|[Binary Watch](https://leetcode.com/problems/binary-watch/) | [Python](algorithms/401-binary-watch.ipynb)|Easy|
37+
| 392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Python](algorithms/392-is-subsequence.ipynb)|Medium|
38+
| 383|[Ransom Note](https://leetcode.com/problems/ransom-note/) | [Python](algorithms/383-ransom-note.ipynb)|Easy|
3139
| 377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [Python](algorithms/377-combination-sum-iv.ipynb)|Medium|
3240
| 371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Python](algorithms/371-sum-of-two-integers.ipynb)|Easy|
3341
| 367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [Python](algorithms/367-valid-perfect-square.ipynb)|Easy|
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "floppy-arizona",
6+
"metadata": {},
7+
"source": [
8+
"### [绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)\n",
9+
"\n",
10+
"给你一个整数数组`nums`,和一个表示限制的整数`limit`,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于`limit`。\n",
11+
"\n",
12+
"如果不存在满足条件的子数组,则返回 0 。\n",
13+
"\n",
14+
"#### 示例 1:\n",
15+
"```\n",
16+
"输入:nums = [8,2,4,7], limit = 4\n",
17+
"输出:2 \n",
18+
"解释:所有子数组如下:\n",
19+
"[8] 最大绝对差 |8-8| = 0 <= 4.\n",
20+
"[8,2] 最大绝对差 |8-2| = 6 > 4. \n",
21+
"[8,2,4] 最大绝对差 |8-2| = 6 > 4.\n",
22+
"[8,2,4,7] 最大绝对差 |8-2| = 6 > 4.\n",
23+
"[2] 最大绝对差 |2-2| = 0 <= 4.\n",
24+
"[2,4] 最大绝对差 |2-4| = 2 <= 4.\n",
25+
"[2,4,7] 最大绝对差 |2-7| = 5 > 4.\n",
26+
"[4] 最大绝对差 |4-4| = 0 <= 4.\n",
27+
"[4,7] 最大绝对差 |4-7| = 3 <= 4.\n",
28+
"[7] 最大绝对差 |7-7| = 0 <= 4. \n",
29+
"因此,满足题意的最长子数组的长度为 2 。\n",
30+
"```\n",
31+
"\n",
32+
"#### 示例 2:\n",
33+
"```\n",
34+
"输入:nums = [10,1,2,4,7,2], limit = 5\n",
35+
"输出:4 \n",
36+
"解释:满足题意的最长子数组是 [2,4,7,2],其最大绝对差 |2-7| = 5 <= 5 。\n",
37+
"```\n",
38+
"\n",
39+
"#### 示例 3:\n",
40+
"```\n",
41+
"输入:nums = [4,2,2,2,4,4,2,2], limit = 0\n",
42+
"输出:3\n",
43+
"```\n",
44+
"\n",
45+
"#### 提示:\n",
46+
"- 1 <= nums.length <= 10^5\n",
47+
"- 1 <= nums[i] <= 10^9\n",
48+
"- 0 <= limit <= 10^9"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 1,
54+
"id": "outside-catch",
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"from typing import List\n",
59+
"# 常规解法,超时\n",
60+
"def longestSubarray(nums: List[int], limit: int) -> int:\n",
61+
" n, ans = len(nums), 0\n",
62+
" if n <= 1:\n",
63+
" return n\n",
64+
" for i in range(n):\n",
65+
" ni = nums[i]\n",
66+
" l, nmin, nmax = 1, ni, ni \n",
67+
" for j in range(i + 1, n):\n",
68+
" nj = nums[j]\n",
69+
" if nmin > nj:\n",
70+
" nmin = nj\n",
71+
" if nmax < nj:\n",
72+
" nmax = nj\n",
73+
" \n",
74+
" if nmax - nmin <= limit:\n",
75+
" ans = max(ans, j - i + 1)\n",
76+
" return ans"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 2,
82+
"id": "rental-matrix",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"# 超时\n",
87+
"def longestSubarray(nums: List[int], limit: int) -> int:\n",
88+
" n, ans = len(nums), 0\n",
89+
" if n <= 1:\n",
90+
" return n \n",
91+
" \n",
92+
" n0 = nums[0]\n",
93+
" \n",
94+
" ans, maxlen, queue = 1, 1, [n0]\n",
95+
" for i in range(1, n):\n",
96+
" ni, qmin, qmax = nums[i], min(queue), max(queue)\n",
97+
"\n",
98+
" if abs(ni - qmin) <= limit and abs(qmax - ni) <= limit:\n",
99+
" queue.append(ni)\n",
100+
" else:\n",
101+
" ans = max(ans, maxlen)\n",
102+
" j = len(queue)\n",
103+
" while abs(ni - queue[j - 1]) <= limit:\n",
104+
" j -= 1\n",
105+
" \n",
106+
" del queue[0:j]\n",
107+
" queue.append(ni)\n",
108+
" maxlen = len(queue)\n",
109+
"\n",
110+
" ans = max(ans, maxlen)\n",
111+
" return ans"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 3,
117+
"id": "accompanied-beatles",
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"def longestSubarray(nums: List[int], limit: int) -> int:\n",
122+
" n, ans = len(nums), 0\n",
123+
" if n <= 1:\n",
124+
" return n \n",
125+
" \n",
126+
" n0 = nums[0]\n",
127+
" qmax, qmin, ans, start = [n0], [n0], 1, 0\n",
128+
" \n",
129+
" for i in range(1, n):\n",
130+
" ni = nums[i]\n",
131+
" while qmin and ni < qmin[-1]:\n",
132+
" qmin.pop()\n",
133+
" qmin.append(ni)\n",
134+
" \n",
135+
" while qmax and ni > qmax[-1]:\n",
136+
" qmax.pop()\n",
137+
" qmax.append(ni)\n",
138+
" \n",
139+
" while qmax[0] - qmin[0] > limit:\n",
140+
" if qmin[0] == nums[start]:\n",
141+
" qmin.pop(0)\n",
142+
" if qmax[0] == nums[start]:\n",
143+
" qmax.pop(0)\n",
144+
" start += 1\n",
145+
" else:\n",
146+
" ans = max(ans, i - start + 1)\n",
147+
" \n",
148+
" return ans"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 4,
154+
"id": "found-airline",
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"5"
161+
]
162+
},
163+
"execution_count": 4,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
168+
"source": [
169+
"longestSubarray([1,5,6,7,8,10,6,5,6], 4)"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 5,
175+
"id": "square-liverpool",
176+
"metadata": {},
177+
"outputs": [
178+
{
179+
"data": {
180+
"text/plain": [
181+
"2"
182+
]
183+
},
184+
"execution_count": 5,
185+
"metadata": {},
186+
"output_type": "execute_result"
187+
}
188+
],
189+
"source": [
190+
"longestSubarray([8,2,4,7], 4)"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": 6,
196+
"id": "weird-philippines",
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"data": {
201+
"text/plain": [
202+
"4"
203+
]
204+
},
205+
"execution_count": 6,
206+
"metadata": {},
207+
"output_type": "execute_result"
208+
}
209+
],
210+
"source": [
211+
"longestSubarray([10,1,2,4,7,2], 5)"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": 7,
217+
"id": "superior-camel",
218+
"metadata": {},
219+
"outputs": [
220+
{
221+
"data": {
222+
"text/plain": [
223+
"3"
224+
]
225+
},
226+
"execution_count": 7,
227+
"metadata": {},
228+
"output_type": "execute_result"
229+
}
230+
],
231+
"source": [
232+
"longestSubarray([4,2,2,2,4,4,2,2], 0)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 8,
238+
"id": "motivated-graphic",
239+
"metadata": {},
240+
"outputs": [
241+
{
242+
"data": {
243+
"text/plain": [
244+
"6"
245+
]
246+
},
247+
"execution_count": 8,
248+
"metadata": {},
249+
"output_type": "execute_result"
250+
}
251+
],
252+
"source": [
253+
"longestSubarray([2, 2, 2, 2, 2, 2], 0)"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": 9,
259+
"id": "aerial-smith",
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"data": {
264+
"text/plain": [
265+
"3"
266+
]
267+
},
268+
"execution_count": 9,
269+
"metadata": {},
270+
"output_type": "execute_result"
271+
}
272+
],
273+
"source": [
274+
"longestSubarray([2, 2, 1, 2, 2, 2], 0)"
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": 10,
280+
"id": "cloudy-explanation",
281+
"metadata": {},
282+
"outputs": [
283+
{
284+
"data": {
285+
"text/plain": [
286+
"25"
287+
]
288+
},
289+
"execution_count": 10,
290+
"metadata": {},
291+
"output_type": "execute_result"
292+
}
293+
],
294+
"source": [
295+
"a = [24,12,71,33,5,87,10,11,3,58,2,97,97,36,32,35,15,80,24,45,38,9,22,21,33,68,22,85,35,83,92,38,59,90,42,64,61,15,4,40,50,44,54,25,34,14,33,94,66,27,78,56,3,29,3,51,19,5,93,21,58,91,65,87,55,70,29,81,89,67,58,29,68,84,4,51,87,74,42,85,81,55,8,95,39]\n",
296+
"longestSubarray(a, 87)"
297+
]
298+
}
299+
],
300+
"metadata": {
301+
"kernelspec": {
302+
"display_name": "Python 3",
303+
"language": "python",
304+
"name": "python3"
305+
},
306+
"language_info": {
307+
"codemirror_mode": {
308+
"name": "ipython",
309+
"version": 3
310+
},
311+
"file_extension": ".py",
312+
"mimetype": "text/x-python",
313+
"name": "python",
314+
"nbconvert_exporter": "python",
315+
"pygments_lexer": "ipython3",
316+
"version": "3.8.6"
317+
}
318+
},
319+
"nbformat": 4,
320+
"nbformat_minor": 5
321+
}

0 commit comments

Comments
 (0)