-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
executable file
·268 lines (173 loc) · 7.27 KB
/
Copy pathpractice.py
File metadata and controls
executable file
·268 lines (173 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# sum1 = 0 #answer is 233168
# for i in range(1,1000):
# if i%3 == 0 or i%5 == 0:
# sum1 += i
# print(sum1)
######################################################################
# cache = {}
# def fib(n):
# if n in cache:
# return cache[n]
# if n < 2:
# value = 1
# else:
# value = fib(n-1) + fib(n-2)
# cache[n] = value
# return value
# print(fib(10))
####################################################################### Find target sum
# arr = [2, 5, 6, 7, 9, 13, 16, 19, 23]
# def targetSum(array, target):
# i = 0
# j = len(array)-1
# while i != j:
# if array[i] + array[j] == target:
# return [i,j]
# elif array[i] + array[j] > target:
# j -= 1
# else:
# i += 1
# return -1
# print(targetSum(arr, 42))
####################################################################### Count inversions in an array
#How many operations to make it sorted
# array = [10, 3, 9, 6, 1]
# output = []
# for i in range(len(array)):
# for j in range(i+1, len(array[i:])):
# if array[i] > array[j]:
# output.append((array[i], array[j]))
# print(output)
# print(len(output))
####################################################################### 1481 Least Number of unique integers
# class Solution:
# def findLeastNumOfUniqueInts(self, nums: List[int], k: int) -> int:
# cache = {}
# for num in range(len(nums)):
# if nums[num] not in cache:
# cache[nums[num]] = 0
# cache[nums[num]] += 1
# cache = sorted(cache.items(), key = lambda x:x[1])
# count = 0
# l = 0
# # for i, j in enumerate(cache):
# # print(i,j)
# # if j[1] <= k:
# # cache.pop(i)
# # k -= j[1]
# # if k == 0:
# # break
# while k > 0:
# if cache[l][1] <= k:
# # times.append(cache[l])
# count += 1
# k -= cache[l][1]
# l += 1
# return len(cache) - count
# ####################################################################### 206 Reverse Linked List 1
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# class Solution:
# def reverseList(self, head: ListNode) -> ListNode:
# prev = None
# while head:
# temp = head
# head = head.next
# temp.next = prev
# prev = temp
# return prev
####################################################################### 387 First unique character in string
# from collections import OrderedDict
# class Solution:
# def firstUniqChar(self, s: str) -> int:
# cache = OrderedDict()
# for i in range(len(s)):
# if s[i] not in cache:
# cache[s[i]] = [0, i]
# cache[s[i]][0] += 1
# for x in cache:
# if cache[x][0] == 1:
# return cache[x][1]
# return -1
####################################################################### K Closest points to origin
# class Solution:
# def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
# p = sorted(points, key = lambda x: x[0]**2 + x[1]**2)
# return p[:k]
# heap = [[-(i**2 + j**2), i, j] for i,j in points[:k]]
# heapq.heapify(heap)
# for i,j in points[k:]:
# d = (i**2 + j**2)
# if -(heap[0][0]) > d:
# heapq.heapreplace(heap, [-d, i, j])
# return [[i,j] for d,i,j in heap]
####################################################################### Economy Mart
# entries = [['INSERT', 'milk', 4], ['INSERT', 'coffee', 3], ['VIEW', '-', '-'], ['INSERT', 'gum', 1], ['VIEW', '-', '-'], ['INSERT', 'pizza', 2], ['INSERT', 'mouse', 6], ['INSERT', 'water', 2], ['INSERT', 'bag', 1], ['VIEW', '-', '-'], ['INSERT', 'creatine', 2], ['VIEW', '-', '-']]
# entries = [['INSERT', 'milk', 4], ['INSERT', 'coffee', 3], ['VIEW', '-', '-'], ['INSERT', 'pizza', 5], ['INSERT', 'gum', 1], ['VIEW', '-', '-']]
# entries2 = [['INSERT', 'milk', 4], ['INSERT', 'coffee', 3], ['INSERT', 'pizza', 5], ['INSERT', 'gum', 1], ['VIEW', '-', '-']]
# entries1 = [['INSERT', 'fries', 4], ['INSERT', 'soda', 2], ['VIEW', '-', '-'], ['VIEW', '-', '-'], ['INSERT', 'hamburger', 5], ['VIEW', '-', '-'], ['INSERT', 'nuggets', 4], ['INSERT', 'cookie', 1], ['VIEW', '-', '-'], ['VIEW', '-', '-']]
# def getItems(entries):
# db, result, count = [], [], 0
# for entry in entries:
# if entry[0] == "INSERT":
# db.append([entry[1], int(entry[2])])
# if entry[0] == "VIEW":
# db1 = updateDatabase(db)
# count += 1
# result.append(db1[count-1][0])
# print(result)
# def updateDatabase(database):
# database.sort(key = lambda x: (x[1], x[0]))
# return database
# getItems(entries)
####################################################################### Is same tree
def isSameTree(self, p, q):
if not p and not q:
return True
if not p or not q or (p.val != q.val):
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
####################################################################### Subtree of another tree
def isSubtree(self, root, subRoot):
def isSame(t, s):
if not t and not s:
return True
if not t or not s:
return False
return (t.val == s.val) and isSame(t.left, s.left) and isSame(t.right, s.right)
if not subRoot:
return True
if not root:
return False
if isSame(root, subRoot):
return True
return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)
################################################################## Course schedule
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
preMap = { i:[] for i in range(numCourses)}
for crs, prereq in prerequisites:
preMap[crs].append(prereq)
visited = set()
def dfs(crs):
if crs in visited:
return False
if preMap[crs] == []:
return True
visited.add(crs)
for pre in preMap[crs]:
if not dfs(pre):
return False
visited.remove(crs)
preMap[crs] = []
return True
for crs in range(numCourses):
if not dfs(crs):
return False
return True