-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneet_code_solutions
More file actions
269 lines (182 loc) · 10.1 KB
/
neet_code_solutions
File metadata and controls
269 lines (182 loc) · 10.1 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
269
#*************************************************************************************************
#*******************************Arrays & Hashing**************************************************
class Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
if len(nums) == len(set(nums)):
return False
else:
return True
#*************************************************************************************************
import pandas as pd
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
x = pd.Series(list(s)).value_counts().to_dict()
y = pd.Series(list(t)).value_counts().to_dict()
if x == y:
return True
else:
return False
#*************************************************************************************************
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
if not (2 <= len(nums) <= 1000 and all(-10_000_000 <= n <= 10_000_000 for n in nums) and -10_000_000 <= target <= 10_000_000):
raise ValueError("Constraints violated")
for idx, val in enumerate(nums[:-1]):
for idx1, val1 in enumerate(nums[idx+1:]):
if nums[idx]+val1 == target:
return [idx, (idx + 1 + idx1)]
#*************************************************************************************************
from collections import Counter, defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
assert 1 <= len(strs) <= 1000
assert all(0 <= len(s) <= 100 for s in strs)
assert all(len(s) == 0 or (s.islower() and s.isalpha()) for s in strs)
dd = defaultdict(list)
for word in strs:
key = tuple(sorted(Counter(word).items()))
dd[key].append(word)
return list(dd.values())
#*************************************************************************************************
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
most_common = Counter(nums).most_common(k)
return list(dict(sorted(most_common)).keys())
#*************************************************************************************************
import json
class Solution:
def __init__(self):
self.str = ""
def encode(self, strs: List[str]) -> str:
return json.dumps(strs)
def decode(self, s: str) -> List[str]:
return json.loads(s)
#*************************************************************************************************
from itertools import combinations
import math
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result = []
for i in combinations(nums, len(nums)-1):
result.append(math.prod(i))
return result[::-1]
#*************************************************************************************************
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = list(sorted(set(nums)))
if len(nums) == 0 or len(nums)==1:
return len(nums)
lcs = []
lcn = 0
for idx, val in enumerate(nums[:-1]):
if abs(nums[idx+1] - nums[idx]) >= 2:
lcs.append(lcn+1)
lcn = 0
else:
lcn +=1
if idx == len(nums)-2:
lcs.append(lcn+1)
return max(lcs)
#*************************************************************************************************
#*******************************Two Pointers**************************************************
class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join([ch for ch in s if ch.isalnum()]).replace(' ', '').lower()
s1 = s[::-1]
if s == s1:
return True
else:
return False
#*************************************************************************************************
from itertools import combinations
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
answer = []
for i in combinations(nums, 3):
if sum(i) == 0:
answer.append(list(sorted(i))) if list(sorted(i)) not in answer else None
return answer
#*************************************************************************************************
from itertools import combinations
class Solution:
def maxArea(self, heights: List[int]) -> int:
max_area_list = []
for combo in combinations(range(len(heights)), 2):
distance_between_chosen_bars = combo[1]-combo[0]
height_limit = min(heights[combo[0]], heights[combo[1]])
area = distance_between_chosen_bars * height_limit
max_area_list.append(area)
return max(max_area_list)
#*************************************************************************************************
from itertools import combinations
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit_list = []
if len(prices) in [0,1]:
return 0
for combo in combinations(prices, 2):
if combo[0]<combo[1]:
profit_list.append(abs(combo[0]-combo[1]))
else:
profit_list.append(0)
if profit_list:
return max(profit_list)
#*************************************************************************************************
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
s = list(s)
if len(s) == 0:
return 0
left = right = 0
main_list = []
for idx, val in enumerate(s):
unique_list = []
while right<len(s) and s[right] not in unique_list:
unique_list.append(s[right])
right += 1
right = idx+1
main_list.append(len(unique_list))
return max(main_list)
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************
#*************************************************************************************************