Skip to content

Commit 1ff40e9

Browse files
committed
contest w-352
1 parent 1cfbfa1 commit 1ff40e9

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed

00. Contests/28. LeetCode W-352.ipynb

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 155,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from typing import List"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 156,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from typing import List\n",
19+
"\n",
20+
"class Solution:\n",
21+
" def findPrimePairs(self, n: int) -> List[List[int]]:\n",
22+
" is_prime = [True] * (n + 1)\n",
23+
" primes = []\n",
24+
" is_prime[0] = is_prime[1] = False\n",
25+
" \n",
26+
" for i in range(2, n + 1):\n",
27+
" if is_prime[i]:\n",
28+
" for j in range(i * i, n + 1, i):\n",
29+
" is_prime[j] = False\n",
30+
" primes.append(i)\n",
31+
"\n",
32+
" res = set()\n",
33+
" added = {}\n",
34+
" for prime in primes:\n",
35+
" complement = n - prime\n",
36+
" if is_prime[complement]:\n",
37+
" if prime < complement:\n",
38+
" added[(prime, complement)] = True\n",
39+
" res.add((prime, complement))\n",
40+
" else:\n",
41+
" added[(complement, prime)] = True\n",
42+
" res.add((complement, prime))\n",
43+
" return sorted(res)\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 159,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"[(3, 7), (5, 5)]"
55+
]
56+
},
57+
"execution_count": 159,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"Solution().findPrimePairs(10)"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": []
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"class Solution:\n",
80+
" def continuousSubarrays(self, nums: List[int]) -> int:\n",
81+
" "
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 170,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"from typing import List\n",
91+
"\n",
92+
"class Solution:\n",
93+
" def continuousSubarrays(self, nums: List[int]) -> int:\n",
94+
" n = len(nums)\n",
95+
" count = 0\n",
96+
"\n",
97+
" for i in range(n):\n",
98+
" j = i\n",
99+
" while j < n and abs(nums[j] - nums[i]) <= 2:\n",
100+
" count += 1\n",
101+
" j += 1\n",
102+
"\n",
103+
" return count\n"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 181,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"from typing import List\n",
113+
"\n",
114+
"class Solution:\n",
115+
" def continuousSubarrays(self, nums: List[int]) -> int:\n",
116+
" count = 0\n",
117+
" n = len(nums)\n",
118+
"\n",
119+
" for i in range(n):\n",
120+
" for j in range(i, n):\n",
121+
" if max(nums[i:j+1]) - min(nums[i:j+1]) <= 2:\n",
122+
" count += 1\n",
123+
"\n",
124+
" return count\n"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 183,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"8"
136+
]
137+
},
138+
"execution_count": 183,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"nums = [5,4,2,4]\n",
145+
"#nums = [1,3,3,3,5]\n",
146+
"Solution().continuousSubarrays(nums)"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": []
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 184,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"from typing import List\n",
163+
"\n",
164+
"class Solution:\n",
165+
" def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
166+
" max_length = 0\n",
167+
" current_length = 0\n",
168+
"\n",
169+
" for i in range(len(nums)):\n",
170+
" if i > 0 and abs(nums[i] - nums[i - 1]) > threshold:\n",
171+
" current_length = 0\n",
172+
" if nums[i] % 2 == 0:\n",
173+
" current_length += 1\n",
174+
" else:\n",
175+
" current_length = 1\n",
176+
" \n",
177+
" max_length = max(max_length, current_length)\n",
178+
"\n",
179+
" return max_length\n"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 269,
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"from typing import List\n",
189+
"\n",
190+
"class Solution:\n",
191+
" def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
192+
" max_length = 0\n",
193+
" current_length = 0\n",
194+
" start_point = -1\n",
195+
" if len(nums)==1:\n",
196+
" if (nums[0] % 2 == 0) and (nums[0] <= threshold): return 1\n",
197+
" else: return 0 \n",
198+
" for i in range(len(nums)-1):\n",
199+
" #print(i)\n",
200+
" if (nums[i] % 2 == 0) and (start_point == -1):\n",
201+
" start_point = 1\n",
202+
" current_length = 1\n",
203+
" max_length = max(max_length, current_length)\n",
204+
" #print(\"asdf\")\n",
205+
" continue\n",
206+
" \n",
207+
" if (nums[i] % 2 != nums[i + 1] % 2) and (nums[i] <= threshold) and (start_point==1):\n",
208+
" current_length+=1\n",
209+
" else:\n",
210+
" current_length = 0\n",
211+
" start_point = -1\n",
212+
" \n",
213+
" max_length = max(max_length, current_length)\n",
214+
" if (nums[-1] <= threshold):\n",
215+
" max_length = max(max_length, current_length+1)\n",
216+
"\n",
217+
" return max_length\n"
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": 270,
223+
"metadata": {},
224+
"outputs": [
225+
{
226+
"data": {
227+
"text/plain": [
228+
"0"
229+
]
230+
},
231+
"execution_count": 270,
232+
"metadata": {},
233+
"output_type": "execute_result"
234+
}
235+
],
236+
"source": [
237+
"nums = [3,2,5,4]; threshold = 5\n",
238+
"nums = [1,2]; threshold = 2\n",
239+
"nums = [2,3,4,5]; threshold = 4\n",
240+
"nums = [1]; threshold = 1\n",
241+
"#nums = [2]; threshold = 2\n",
242+
"Solution().longestAlternatingSubarray(nums, threshold)"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": null,
248+
"metadata": {},
249+
"outputs": [],
250+
"source": []
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "base",
256+
"language": "python",
257+
"name": "python3"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 3
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython3",
269+
"version": "3.9.13"
270+
},
271+
"orig_nbformat": 4
272+
},
273+
"nbformat": 4,
274+
"nbformat_minor": 2
275+
}

0 commit comments

Comments
 (0)