Skip to content

Commit 207078b

Browse files
authored
Add files via upload
1 parent c09ba86 commit 207078b

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed

Math.ipynb

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Question 1: Count Odd Numbers in an Interval Range, by Microsoft\n",
8+
"- Given two non-negative integers low and high. \n",
9+
"- Return the count of odd numbers between low and high (inclusive).\n",
10+
"- https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 26,
16+
"metadata": {
17+
"scrolled": false
18+
},
19+
"outputs": [
20+
{
21+
"data": {
22+
"text/plain": [
23+
"3"
24+
]
25+
},
26+
"execution_count": 26,
27+
"metadata": {},
28+
"output_type": "execute_result"
29+
}
30+
],
31+
"source": [
32+
"# solution 1: inefficient and exceed time limit for large numbers\n",
33+
"def countOdds(low,high):\n",
34+
" count = 0 \n",
35+
" for i in range(low,high+1):\n",
36+
" if i%2 != 0:\n",
37+
" count+=1\n",
38+
" return count\n",
39+
"\n",
40+
"#test case\n",
41+
"countOdds(3,7)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 27,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"3611111111111110"
53+
]
54+
},
55+
"execution_count": 27,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"# solution 2: math\n",
62+
"# calculate the number of odd numbers between 1 and low-1: low//2\n",
63+
"# calculate the number of odd numbers between 1 and high: (high+1)//2\n",
64+
"# the difference is our result\n",
65+
"\n",
66+
"def countOdds(low, high) -> int:\n",
67+
" return (high + 1) // 2 - low // 2\n",
68+
"\n",
69+
"#test case\n",
70+
"countOdds(3,7222222222222222)"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"---"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"# Question 2: Arranging Coins, by Bloomberg\n",
85+
"- You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.\n",
86+
"- Given n, find the total number of full staircase rows that can be formed.\n",
87+
"- n is a non-negative integer and fits within the range of a 32-bit signed integer.\n",
88+
"- https://leetcode.com/problems/arranging-coins/"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 28,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"data": {
98+
"text/plain": [
99+
"3"
100+
]
101+
},
102+
"execution_count": 28,
103+
"metadata": {},
104+
"output_type": "execute_result"
105+
}
106+
],
107+
"source": [
108+
"# solution: binary search \n",
109+
"def arranging(n):\n",
110+
" left, right = 0, n \n",
111+
" while left<=right: \n",
112+
" k = (left+right)//2\n",
113+
" current = k*(k+1)//2\n",
114+
" if current == n:\n",
115+
" return k\n",
116+
" elif current >n:\n",
117+
" right = k-1\n",
118+
" else:\n",
119+
" left = k+1\n",
120+
" return right\n",
121+
"\n",
122+
"# test case \n",
123+
"n= 8\n",
124+
"arranging(n)"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"metadata": {},
130+
"source": [
131+
"----"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"# Question 3: Strobogrammatic Number, by Facebook\n",
139+
"- A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).\n",
140+
"- Write a function to determine if a number is strobogrammatic. The number is represented as a string.\n",
141+
"- https://leetcode.com/problems/strobogrammatic-number/"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 30,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"data": {
151+
"text/plain": [
152+
"True"
153+
]
154+
},
155+
"execution_count": 30,
156+
"metadata": {},
157+
"output_type": "execute_result"
158+
}
159+
],
160+
"source": [
161+
"def isStrobogrammatic(num):\n",
162+
" temp = \"\"\n",
163+
" valid = {'0':'0','1':'1', '6':'9', '8':'8', '9':'6'}\n",
164+
" for i in reversed(num): # this is the key: we have to reverse the order first\n",
165+
" if i not in valid: \n",
166+
" return False\n",
167+
" \n",
168+
" else: \n",
169+
" temp+=valid[i]\n",
170+
" \n",
171+
" return temp == num\n",
172+
"\n",
173+
"# test case \n",
174+
"num = \"69\"\n",
175+
"isStrobogrammatic(num)"
176+
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"metadata": {},
181+
"source": [
182+
"---"
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"metadata": {},
188+
"source": [
189+
"# Question 4: Set Mismatch, by Amazon\n",
190+
"- The set S originally contains numbers from 1 to n. \n",
191+
"- But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.\n",
192+
"- Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.\n",
193+
"- https://leetcode.com/problems/set-mismatch/"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 31,
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"data": {
203+
"text/plain": [
204+
"[1, 2]"
205+
]
206+
},
207+
"execution_count": 31,
208+
"metadata": {},
209+
"output_type": "execute_result"
210+
}
211+
],
212+
"source": [
213+
"# solution: find the pattern \n",
214+
"def findErrorNums(nums):\n",
215+
" return [sum(nums)-sum(set(nums)),sum(range(1,len(nums)+1)) - sum(set(nums))]# twice & missing value \n",
216+
"nums = [1,1]\n",
217+
"findErrorNums(nums)"
218+
]
219+
},
220+
{
221+
"cell_type": "markdown",
222+
"metadata": {},
223+
"source": [
224+
"---"
225+
]
226+
},
227+
{
228+
"cell_type": "markdown",
229+
"metadata": {},
230+
"source": [
231+
"# Question 5: Power of Three, by Goldman Sachs and Hulu\n",
232+
"- Given an integer n, return true if it is a power of three. Otherwise, return false.\n",
233+
"- An integer n is a power of three, if there exists an integer x such that n == 3x.\n",
234+
"- https://leetcode.com/problems/power-of-three/"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 21,
240+
"metadata": {},
241+
"outputs": [
242+
{
243+
"data": {
244+
"text/plain": [
245+
"True"
246+
]
247+
},
248+
"execution_count": 21,
249+
"metadata": {},
250+
"output_type": "execute_result"
251+
}
252+
],
253+
"source": [
254+
"# solution 1: while loop\n",
255+
"def isPowerOfThree(n):\n",
256+
" \n",
257+
" if n <1:\n",
258+
" return False\n",
259+
" \n",
260+
" while n % 3 == 0:\n",
261+
" n/=3\n",
262+
" \n",
263+
" return n==1\n",
264+
" \n",
265+
"# test case \n",
266+
"isPowerOfThree(27)"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 23,
272+
"metadata": {},
273+
"outputs": [
274+
{
275+
"data": {
276+
"text/plain": [
277+
"False"
278+
]
279+
},
280+
"execution_count": 23,
281+
"metadata": {},
282+
"output_type": "execute_result"
283+
}
284+
],
285+
"source": [
286+
"# solution 2: recursion\n",
287+
"def isPowerOfThree(n):\n",
288+
" \n",
289+
" if n <1:\n",
290+
" return False\n",
291+
" \n",
292+
" if n==1:\n",
293+
" return True\n",
294+
" \n",
295+
" if n%3 !=0:\n",
296+
" return False\n",
297+
" \n",
298+
" return n%3==0 and isPowerOfThree(n/3)\n",
299+
"\n",
300+
"# test case \n",
301+
"isPowerOfThree(n=20)"
302+
]
303+
}
304+
],
305+
"metadata": {
306+
"kernelspec": {
307+
"display_name": "Python 3",
308+
"language": "python",
309+
"name": "python3"
310+
},
311+
"language_info": {
312+
"codemirror_mode": {
313+
"name": "ipython",
314+
"version": 3
315+
},
316+
"file_extension": ".py",
317+
"mimetype": "text/x-python",
318+
"name": "python",
319+
"nbconvert_exporter": "python",
320+
"pygments_lexer": "ipython3",
321+
"version": "3.7.4"
322+
}
323+
},
324+
"nbformat": 4,
325+
"nbformat_minor": 2
326+
}

0 commit comments

Comments
 (0)