Skip to content

Commit 7ed2c29

Browse files
authored
Add files via upload
1 parent 724ebb6 commit 7ed2c29

File tree

1 file changed

+312
-0
lines changed

1 file changed

+312
-0
lines changed
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Question 1: Number of Good Pairs, by Microsoft and Amazon\n",
8+
"- Given an array of integers nums.\n",
9+
"- A pair (i,j) is called good if nums[i] == nums[j] and i < j.\n",
10+
"- Return the number of good pairs.\n",
11+
"- https://leetcode.com/problems/number-of-good-pairs/"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 8,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"data": {
21+
"text/plain": [
22+
"4"
23+
]
24+
},
25+
"execution_count": 8,
26+
"metadata": {},
27+
"output_type": "execute_result"
28+
}
29+
],
30+
"source": [
31+
"# solution 1: brutal force & a nested for loop\n",
32+
"def good_pair(nums):\n",
33+
" count = 0 \n",
34+
" for i in range(len(nums)):\n",
35+
" for j in range(i+1,len(nums)):\n",
36+
" if nums[i] ==nums[j]:\n",
37+
" count+=1\n",
38+
" return count\n",
39+
"\n",
40+
"# test case \n",
41+
"nums = [1,2,3,1,1,3]\n",
42+
"\n",
43+
"good_pair(nums)"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 17,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"4"
55+
]
56+
},
57+
"execution_count": 17,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"# solution 2: dictionary & hashmap\n",
64+
"def good_pair(nums):\n",
65+
" \n",
66+
" hashmap = {}\n",
67+
" count = 0 \n",
68+
" \n",
69+
" for num in nums: # read elements from nums sequentially \n",
70+
" if num in hashmap:\n",
71+
" count += hashmap[num]\n",
72+
" hashmap[num]+=1\n",
73+
"\n",
74+
" else: \n",
75+
" hashmap[num]=1\n",
76+
" return count \n",
77+
"\n",
78+
"# test case \n",
79+
"nums = [1,2,3,1,1,3]\n",
80+
"\n",
81+
"good_pair(nums)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 18,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"text/plain": [
92+
"12"
93+
]
94+
},
95+
"execution_count": 18,
96+
"metadata": {},
97+
"output_type": "execute_result"
98+
}
99+
],
100+
"source": [
101+
"number = 123\n",
102+
"#num = number%10\n",
103+
"\n",
104+
"number/=10\n",
105+
"int(number)"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"metadata": {},
111+
"source": [
112+
"--- "
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"# Question 2: Armstrong Number, by Amazon\n",
120+
"- The k-digit number N is an Armstrong number if and only if the k-th power of each digit sums to N.\n",
121+
"- Given a positive integer N, return true if and only if it is an Armstrong number.\n",
122+
"- https://leetcode.com/problems/armstrong-number/"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 19,
128+
"metadata": {},
129+
"outputs": [
130+
{
131+
"data": {
132+
"text/plain": [
133+
"True"
134+
]
135+
},
136+
"execution_count": 19,
137+
"metadata": {},
138+
"output_type": "execute_result"
139+
}
140+
],
141+
"source": [
142+
"# solution 1: change data type using int()\n",
143+
"\n",
144+
"def Armstrong(num):\n",
145+
" \n",
146+
" k = len(str(num))\n",
147+
" \n",
148+
" digit_sum =0\n",
149+
" \n",
150+
" for i in str(num):\n",
151+
" digit_sum += int(i)**k\n",
152+
"\n",
153+
" return digit_sum == num\n",
154+
"\n",
155+
"# test case \n",
156+
"Armstrong(num = 153)"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 2,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"data": {
166+
"text/plain": [
167+
"True"
168+
]
169+
},
170+
"execution_count": 2,
171+
"metadata": {},
172+
"output_type": "execute_result"
173+
}
174+
],
175+
"source": [
176+
"# solution 2: pop and push \n",
177+
"\n",
178+
"def Armstrong_1(num):\n",
179+
" num1 = num # prepare num1 for comparison\n",
180+
" k = len(str(num))\n",
181+
"\n",
182+
" digit_sum= 0 \n",
183+
" \n",
184+
" while num != 0:\n",
185+
" digit = num%10\n",
186+
" digit_sum += digit**k\n",
187+
" num = int(num/10)\n",
188+
" return digit_sum == num1\n",
189+
"\n",
190+
"# test case\n",
191+
"Armstrong_1(num = 153)"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"metadata": {},
197+
"source": [
198+
"---"
199+
]
200+
},
201+
{
202+
"cell_type": "markdown",
203+
"metadata": {},
204+
"source": [
205+
"# Question 3: Count Primes, by FAANG\n",
206+
"- Count the number of prime numbers less than a non-negative number, n.\n",
207+
"- https://leetcode.com/problems/count-primes/"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 32,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"data": {
217+
"text/plain": [
218+
"3"
219+
]
220+
},
221+
"execution_count": 32,
222+
"metadata": {},
223+
"output_type": "execute_result"
224+
}
225+
],
226+
"source": [
227+
"# solution 1: brutal force not efficient\n",
228+
"def countPrimes(n):\n",
229+
" if n <=2:\n",
230+
" return 0\n",
231+
" count = 0 \n",
232+
" for i in range(2,n):\n",
233+
" for j in range(2,i):\n",
234+
" if i%j ==0:\n",
235+
" break\n",
236+
" else:\n",
237+
" count+=1\n",
238+
" return count\n",
239+
"\n",
240+
"# test case\n",
241+
"countPrimes(7)# primes: 2,3,5"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 31,
247+
"metadata": {},
248+
"outputs": [
249+
{
250+
"data": {
251+
"text/plain": [
252+
"9592"
253+
]
254+
},
255+
"execution_count": 31,
256+
"metadata": {},
257+
"output_type": "execute_result"
258+
}
259+
],
260+
"source": [
261+
"# solution 2: Sieve of Eratosthene\n",
262+
"# super fast but have to remember the math\n",
263+
"def countPrimes(n):\n",
264+
" if n < 3:\n",
265+
" return 0\n",
266+
" \n",
267+
" primes = [True] * n\n",
268+
" \n",
269+
" primes[0] = primes[1] = False\n",
270+
" \n",
271+
" for i in range(2, int(n ** 0.5) + 1):\n",
272+
" \n",
273+
" if primes[i]:\n",
274+
" \n",
275+
" primes[i * i: n: i] = [False] * len(primes[i * i: n: i])\n",
276+
" \n",
277+
" return sum(primes)\n",
278+
"\n",
279+
"# test case\n",
280+
"countPrimes(100000)"
281+
]
282+
},
283+
{
284+
"cell_type": "markdown",
285+
"metadata": {},
286+
"source": [
287+
"---"
288+
]
289+
}
290+
],
291+
"metadata": {
292+
"kernelspec": {
293+
"display_name": "Python 3",
294+
"language": "python",
295+
"name": "python3"
296+
},
297+
"language_info": {
298+
"codemirror_mode": {
299+
"name": "ipython",
300+
"version": 3
301+
},
302+
"file_extension": ".py",
303+
"mimetype": "text/x-python",
304+
"name": "python",
305+
"nbconvert_exporter": "python",
306+
"pygments_lexer": "ipython3",
307+
"version": "3.7.4"
308+
}
309+
},
310+
"nbformat": 4,
311+
"nbformat_minor": 2
312+
}

0 commit comments

Comments
 (0)