Skip to content

Commit 9770542

Browse files
authored
Add files via upload
1 parent aaec7a1 commit 9770542

File tree

1 file changed

+385
-0
lines changed

1 file changed

+385
-0
lines changed

Python Data Type Stack.ipynb

Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Question 1: Remove All Adjacent Duplicates In String, by Facebook, Amazon, Bloomberg, Oracle\n",
8+
"- Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.\n",
9+
"- We repeatedly make duplicate removals on S until we no longer can.\n",
10+
"- Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.\n",
11+
"- https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 12,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"data": {
21+
"text/plain": [
22+
"'ca'"
23+
]
24+
},
25+
"execution_count": 12,
26+
"metadata": {},
27+
"output_type": "execute_result"
28+
}
29+
],
30+
"source": [
31+
"def removeDuplicates(S):\n",
32+
" list_1 = []\n",
33+
" for i in S:\n",
34+
" if list_1 and i==list_1[-1]:\n",
35+
" list_1.pop()\n",
36+
" else:\n",
37+
" list_1.append(i)\n",
38+
" return \"\".join(list_1)\n",
39+
"\n",
40+
"# test case \n",
41+
"S = \"abbaca\"\n",
42+
"removeDuplicates(S)"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"----"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"# Question 2: Make The String Great, by Google\n",
57+
"- Given a string s of lower and upper case English letters.\n",
58+
"- A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where: 0 <= i <= s.length - 2\n",
59+
"- s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.\n",
60+
"- To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.\n",
61+
"- Return the string after making it good. The answer is guaranteed to be unique under the given constraints.\n",
62+
"- Notice that an empty string is also good.\n",
63+
"- https://leetcode.com/problems/make-the-string-great/"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 13,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"data": {
73+
"text/plain": [
74+
"'leetcode'"
75+
]
76+
},
77+
"execution_count": 13,
78+
"metadata": {},
79+
"output_type": "execute_result"
80+
}
81+
],
82+
"source": [
83+
"# solution 1\n",
84+
"def makeGood(s):\n",
85+
" stack = []\n",
86+
" for i in s: \n",
87+
" if stack and stack[-1] != i and stack[-1].lower() == i.lower():\n",
88+
" stack.pop()\n",
89+
" else:\n",
90+
" stack.append(i)\n",
91+
" return \"\".join(stack)\n",
92+
"\n",
93+
"#test case\n",
94+
"s = 'leEeetcode'\n",
95+
"makeGood(s)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 14,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"'leetcode'"
107+
]
108+
},
109+
"execution_count": 14,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"# solution 2\n",
116+
"def makeGood(s):\n",
117+
" stack = []\n",
118+
" \n",
119+
" for i in s: \n",
120+
" if stack and stack[-1] == i.swapcase(): # easy way of changing letter case\n",
121+
" stack.pop()\n",
122+
" \n",
123+
" else:\n",
124+
" stack.append(i)\n",
125+
" return \"\".join(stack)\n",
126+
"\n",
127+
"#test case\n",
128+
"s = 'leEeetcode'\n",
129+
"makeGood(s)"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"---"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"# Question 3: Build an Array With Stack Operations, by Google\n",
144+
"- Given an array target and an integer n. In each iteration, you will read a number from list = {1,2,3..., n}.\n",
145+
"- Build the target array using the following operations:\n",
146+
" - Push: Read a new element from the beginning list, and push it in the array.\n",
147+
" - Pop: delete the last element of the array.\n",
148+
" - If the target array is already built, stop reading more elements.\n",
149+
"- Return the operations to build the target array. You are guaranteed that the answer is unique.\n",
150+
"- https://leetcode.com/problems/build-an-array-with-stack-operations/"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 15,
156+
"metadata": {},
157+
"outputs": [
158+
{
159+
"data": {
160+
"text/plain": [
161+
"['Push', 'Push', 'Pop', 'Push']"
162+
]
163+
},
164+
"execution_count": 15,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
169+
"source": [
170+
"def buildArray(target,n):\n",
171+
" stack = []\n",
172+
" \n",
173+
" result = []\n",
174+
" \n",
175+
" for i in range(1,n+1):\n",
176+
" if i in target: \n",
177+
" stack.append(i)\n",
178+
" result.append('Push')\n",
179+
" else:\n",
180+
" stack.append(i)\n",
181+
" result.append('Push')\n",
182+
" stack.pop()\n",
183+
" result.append('Pop')\n",
184+
" \n",
185+
" if stack == target:\n",
186+
" break\n",
187+
" \n",
188+
" return result\n",
189+
"\n",
190+
"# test case\n",
191+
"target = [1,3]\n",
192+
"n = 3\n",
193+
"buildArray(target,n)"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"----"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"# Question 4: Baseball Game, by Amazon\n",
208+
"- You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.\n",
209+
"- At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:\n",
210+
"- An integer x - Record a new score of x.\n",
211+
" - \"+\" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.\n",
212+
" - \"D\" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.\n",
213+
" - \"C\" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.\n",
214+
"- Return the sum of all the scores on the record.\n",
215+
"- https://leetcode.com/problems/baseball-game/"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 16,
221+
"metadata": {},
222+
"outputs": [
223+
{
224+
"data": {
225+
"text/plain": [
226+
"30"
227+
]
228+
},
229+
"execution_count": 16,
230+
"metadata": {},
231+
"output_type": "execute_result"
232+
}
233+
],
234+
"source": [
235+
"def calPoints(ops):\n",
236+
" stack = []\n",
237+
" for i in ops: \n",
238+
" if i == \"+\":\n",
239+
" stack.append(stack[-1]+stack[-2])\n",
240+
" elif i =='D':\n",
241+
" stack.append(stack[-1]*2)\n",
242+
" elif i == 'C':\n",
243+
" stack.pop()\n",
244+
" else:\n",
245+
" stack.append(int(i))\n",
246+
" return sum(stack)\n",
247+
"\n",
248+
"#test case\n",
249+
"ops = [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
250+
"calPoints(ops)"
251+
]
252+
},
253+
{
254+
"cell_type": "markdown",
255+
"metadata": {},
256+
"source": [
257+
"----"
258+
]
259+
},
260+
{
261+
"cell_type": "markdown",
262+
"metadata": {},
263+
"source": [
264+
"# Question 5: Next Greater Element I, by Amazon and Bloomberg\n",
265+
"- You are given two integer arrays nums1 and nums2 both of unique elements, where nums1 is a subset of nums2.\n",
266+
"- Find all the next greater numbers for nums1's elements in the corresponding places of nums2.\n",
267+
"- The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, return -1 for this number.\n",
268+
"- https://leetcode.com/problems/next-greater-element-i/"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": 17,
274+
"metadata": {},
275+
"outputs": [
276+
{
277+
"data": {
278+
"text/plain": [
279+
"[-1, 3, -1]"
280+
]
281+
},
282+
"execution_count": 17,
283+
"metadata": {},
284+
"output_type": "execute_result"
285+
}
286+
],
287+
"source": [
288+
"# solution 1: brutal force using dictionary\n",
289+
"def Next_Greater_Element(nums1,nums2):\n",
290+
" \n",
291+
" seen = {}\n",
292+
" \n",
293+
" for index,value in enumerate(nums2):\n",
294+
" seen[value]=index\n",
295+
" \n",
296+
" \n",
297+
" result = []\n",
298+
" \n",
299+
" for i in nums1:\n",
300+
" for j in range(seen[i],len(nums2)):\n",
301+
" if i < nums2[j]:\n",
302+
" result.append(nums2[j])\n",
303+
" break\n",
304+
" else:\n",
305+
" result.append(-1)\n",
306+
" return result\n",
307+
"\n",
308+
"# test case \n",
309+
"nums1 = [4,1,2]\n",
310+
"nums2 = [1,3,4,2]\n",
311+
"Next_Greater_Element(nums1,nums2)"
312+
]
313+
},
314+
{
315+
"cell_type": "code",
316+
"execution_count": 1,
317+
"metadata": {},
318+
"outputs": [
319+
{
320+
"data": {
321+
"text/plain": [
322+
"[-1, 3, -1]"
323+
]
324+
},
325+
"execution_count": 1,
326+
"metadata": {},
327+
"output_type": "execute_result"
328+
}
329+
],
330+
"source": [
331+
"#solution 2: stack \n",
332+
"def Next_Greater_Element(nums1,nums2):\n",
333+
" \n",
334+
" stack = []\n",
335+
" \n",
336+
" ans = {}\n",
337+
" \n",
338+
" for num in nums2:\n",
339+
"\n",
340+
" while stack and stack[-1] < num:\n",
341+
" \n",
342+
" anum = stack.pop()\n",
343+
" \n",
344+
" ans[anum] = num\n",
345+
"\n",
346+
" stack.append(num)\n",
347+
"\n",
348+
" return([ans.get(i,-1) for i in nums1]) # get i's value or return -1 if it does not exist\n",
349+
"\n",
350+
"# test case \n",
351+
"nums1 = [4,1,2]\n",
352+
"nums2 = [1,3,4,2]\n",
353+
"Next_Greater_Element(nums1,nums2)"
354+
]
355+
},
356+
{
357+
"cell_type": "markdown",
358+
"metadata": {},
359+
"source": [
360+
"----"
361+
]
362+
}
363+
],
364+
"metadata": {
365+
"kernelspec": {
366+
"display_name": "Python 3",
367+
"language": "python",
368+
"name": "python3"
369+
},
370+
"language_info": {
371+
"codemirror_mode": {
372+
"name": "ipython",
373+
"version": 3
374+
},
375+
"file_extension": ".py",
376+
"mimetype": "text/x-python",
377+
"name": "python",
378+
"nbconvert_exporter": "python",
379+
"pygments_lexer": "ipython3",
380+
"version": "3.7.4"
381+
}
382+
},
383+
"nbformat": 4,
384+
"nbformat_minor": 2
385+
}

0 commit comments

Comments
 (0)