-
Notifications
You must be signed in to change notification settings - Fork 0
/
30daychallenge.py
2669 lines (2171 loc) · 65.9 KB
/
30daychallenge.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# In[55]:
#playground for leet code
# In[ ]:
#1>. given a non-empty array of integers, every element appears twice except for one, find the single one
# In[23]:
class Solution:
def __init__(self,list1):
self.list=[]
for i in range(len(list1)):
if list1[i] in ['[',']']:
1+1
else:
self.list.append(list1[i])
def singleNumber(self):
self.queue=[]
for i in range(len(self.list)):
if self.list[i] not in self.queue:
self.queue.append(self.list[i])
else:
self.queue.remove(self.list[i])
print(self.queue[0])
ss=Solution(input())
ss.singleNumber()
# In[130]:
#2>. write a algorithm to determine if a number is happy
# In[9]:
class Solution:
def isHappy(self,n:int) -> bool:
n=Solution.multiadd(n)
pool=[]
while (n!=1):
n= Solution.multiadd(n)
if n in pool:
break
else:
pool.append(n)
#print("after calculation is : ",n)
return(n==1)
def multiadd(n):
#print(n)
mi=0
while n/10 >0:
sin = n%10
#print(sin)
mis = sin*sin
mi += mis
n = n//10
#print(n,"left, and the sum now is",mi)
return mi
# In[13]:
x = Solution()
x.isHappy(100)
# May 3rd
# Ransom Note
#
# Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
#
# Each letter in the magazine string can only be used once in your ransom note.
#
#
#
# Example 1:
#
# Input: ransomNote = "a", magazine = "b"
# Output: false
#
# Example 2:
#
# Input: ransomNote = "aa", magazine = "ab"
# Output: false
#
# Example 3:
#
# Input: ransomNote = "aa", magazine = "aab"
# Output: true
#
#
#
# Constraints:
#
# You may assume that both strings contain only lowercase letters.
#
#
# In[326]:
from collections import Counter
def canConstruct(ransomNote: str, magazine: str):
For i in Counter(ransomNote):
if i
canConstruct('baa','aab')
# In[331]:
Counter('baa').values()
# #3>. given an inteeger array nums, find the contiguous subarray (containing at least one number)
# #which has the largest sum and return its sum
# #**********important
# Example:
#
# Input: [-2,1,-3,4,-1,2,1,-5,4],
# Output: 6
# Explanation: [4,-1,2,1] has the largest sum = 6.
#
# May 4th Number Complement
#
# Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.
#
#
#
# Example 1:
#
# Input: num = 5
# Output: 2
# Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
#
# Example 2:
#
# Input: num = 1
# Output: 0
# Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
#
#
#
# Constraints:
#
# The given integer num is guaranteed to fit within the range of a 32-bit signed integer.
# num >= 1
# You could assume no leading zero bit in the integer’s binary representation.
# This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
#
#
# In[ ]:
class Solution:
def findComplement(self, num: int) -> int:
bina = bin(num)[2:]
new = ''
print(bina)
for i in range(len(bina)):
if int(bina[i]) == 1:
new+='0'
else:
new+='1'
new = '0b'+new
return int(new,2)
# May 5th
# First Unique Character in a String
#
# Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
#
# Examples:
#
# s = "leetcode"
# return 0.
#
# s = "loveleetcode",
# return 2.
#
#
#
# Note: You may assume the string contain only lowercase English letters.
#
# In[ ]:
class Solution:
def firstUniqChar(self, s: str) -> int:
out= -1
for i in range(len(s)):
n = s[i]
if n in s[:i]:
pass
elif n in s[i+1:]:
pass
else:
print('found')
out = i
break
return out
# May 6th Majority Element
#
# Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
#
# You may assume that the array is non-empty and the majority element always exist in the array.
#
# Example 1:
#
# Input: [3,2,3]
# Output: 3
#
# Example 2:
#
# Input: [2,2,1,1,1,2,2]
# Output: 2
#
#
# In[ ]:
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort()
return nums[len(nums)//2]
# May 7th Cousins in Binary Tree
#
# In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
#
# Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
#
# We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
#
# Return true if and only if the nodes corresponding to the values x and y are cousins.
#
#
#
# Example 1:
#
# Input: root = [1,2,3,4], x = 4, y = 3
# Output: false
#
# Example 2:
#
# Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
# Output: true
#
# Example 3:
#
# Input: root = [1,2,3,null,4], x = 2, y = 3
# Output: false
#
#
#
# Constraints:
#
# The number of nodes in the tree will be between 2 and 100.
# Each node has a unique integer value from 1 to 100.
#
#
# In[ ]:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
self.dictionary ={}
value =1
depth =0
if root != None:
value = root.val
depth += 1
self.dictionary[value] = (depth,0)
self.checkchild(root,depth)
if (self.dictionary[x][0]==self.dictionary[y][0]):
if (self.dictionary[x][1]!=self.dictionary[y][1]):
return True
else:
return False
else:
return False
def checkchild(self,root,depth):
leftdepth = depth
rightdepth = depth
if root.left != None:
leftvalue = root.left.val
leftdepth += 1
parent = root.val
self.dictionary[leftvalue] = (leftdepth,parent)
self.checkchild(root.left,leftdepth)
else:
pass
if root.right != None:
rightvalue = root.right.val
rightdepth += 1
parent = root.val
self.dictionary [rightvalue] = (rightdepth,parent)
self.checkchild(root.right,rightdepth)
else:
pass
# May 8th
# Check If It Is a Straight Line
#
# You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
#
#
#
#
#
# Example 1:
#
# Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
# Output: true
#
# Example 2:
#
# Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
# Output: false
#
#
#
# Constraints:
#
# 2 <= coordinates.length <= 1000
# coordinates[i].length == 2
# -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
# coordinates contains no duplicate point.
#
#
# In[ ]:
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
slope =self.liner(coordinates[0],coordinates[1])
newslope = slope
print(slope)
for i in range(len(coordinates)-1):
newslope = self.liner(coordinates[i],coordinates[i+1])
if newslope != slope:
return False
return True
def liner(self,point1,point2):
(x1,y1,x2,y2) = point1[0],point1[1],point2[0],point2[1]
if x2-x1 != 0:
slope = (y2-y1)/(x2-x1)
else:
slope = 99
return slope
# May 9th Valid Perfect Square
#
# Given a positive integer num, write a function which returns True if num is a perfect square else False.
#
# Follow up: Do not use any built-in library function such as sqrt.
#
#
#
# Example 1:
#
# Input: num = 16
# Output: true
#
# Example 2:
#
# Input: num = 14
# Output: false
#
#
#
# Constraints:
#
# 1 <= num <= 2^31 - 1
#
#
# In[ ]:
class Solution:
def isPerfectSquare(self, num: int) -> bool:
i = 1
while (i*i) <= num:
if (i*i) == num:
return True
i +=1
return False
# May 10th Find the Town Judge
#
# In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.
#
# If the town judge exists, then:
#
# The town judge trusts nobody.
# Everybody (except for the town judge) trusts the town judge.
# There is exactly one person that satisfies properties 1 and 2.
#
# You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
#
# If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
#
#
#
# Example 1:
#
# Input: N = 2, trust = [[1,2]]
# Output: 2
#
# Example 2:
#
# Input: N = 3, trust = [[1,3],[2,3]]
# Output: 3
#
# Example 3:
#
# Input: N = 3, trust = [[1,3],[2,3],[3,1]]
# Output: -1
#
# Example 4:
#
# Input: N = 3, trust = [[1,2],[2,3]]
# Output: -1
#
# Example 5:
#
# Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
# Output: 3
#
#
#
# Constraints:
#
# 1 <= N <= 1000
# 0 <= trust.length <= 10^4
# trust[i].length == 2
# trust[i] are all different
# trust[i][0] != trust[i][1]
# 1 <= trust[i][0], trust[i][1] <= N
#
#
# In[ ]:
class Solution:
def findJudge(self, N: int, trust: List[List[int]]) -> int:
count = [0] * (N+1)
for i,j in trust:
count[i] -= 1
count[j] += 1
for i in range(1,N+1):
if count[i] == N-1:
return i
return -1
# Flood Fill
#
# An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
#
# Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
#
# To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
#
# At the end, return the modified image.
#
# Example 1:
#
# Input:
# image = [[1,1,1],[1,1,0],[1,0,1]]
# sr = 1, sc = 1, newColor = 2
# Output: [[2,2,2],[2,2,0],[2,0,1]]
# Explanation:
# From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
# by a path of the same color as the starting pixel are colored with the new color.
# Note the bottom corner is not colored 2, because it is not 4-directionally connected
# to the starting pixel.
#
# Note:
# The length of image and image[0] will be in the range [1, 50].
# The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
# The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
#
# Hide Hint #1
# Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.
# In[ ]:
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
R, C = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r-1, c)
if r+1 < R: dfs(r+1, c)
if c >= 1: dfs(r, c-1)
if c+1 < C: dfs(r, c+1)
dfs(sr, sc)
return image
# Single Element in a Sorted Array
#
# You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once.
#
# Follow up: Your solution should run in O(log n) time and O(1) space.
#
#
#
# Example 1:
#
# Input: nums = [1,1,2,3,3,4,4,8,8]
# Output: 2
#
# Example 2:
#
# Input: nums = [3,3,7,7,10,11,11]
# Output: 10
#
#
#
# Constraints:
#
# 1 <= nums.length <= 10^5
# 0 <= nums[i] <= 10^5
#
#
# In[ ]:
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
return sum(set(nums))*2-sum(nums)
# Remove K Digits
#
# Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
#
# Note:
#
# The length of num is less than 10002 and will be ≥ k.
# The given num does not contain any leading zero.
#
# Example 1:
#
# Input: num = "1432219", k = 3
# Output: "1219"
# Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
#
# Example 2:
#
# Input: num = "10200", k = 1
# Output: "200"
# Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
#
# Example 3:
#
# Input: num = "10", k = 2
# Output: "0"
# Explanation: Remove all the digits from the number and it is left with nothing whic
# In[ ]:
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
if len(num) == k:
return '0'
for i in range(k):
j = 0
while num[j]<=num[j+1]:
j += 1
if j == len(num)-1:
break
num = num[:j]+num[j+1:]
return str(int(num))
# May 14th Implement Trie (Prefix Tree)
#
# Implement a trie with insert, search, and startsWith methods.
#
# Example:
#
# Trie trie = new Trie();
#
# trie.insert("apple");
# trie.search("apple"); // returns true
# trie.search("app"); // returns false
# trie.startsWith("app"); // returns true
# trie.insert("app");
# trie.search("app"); // returns true
#
# Note:
#
# You may assume that all inputs are consist of lowercase letters a-z.
# All inputs are guaranteed to be non-empty strings.
#
#
# In[ ]:
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.vol = {}
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
self.vol[word] = 1
print('success')
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
if word in self.vol.keys():
return True
return False
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
for keys in self.vol:
if prefix in keys:
for i in range(len(keys)):
if prefix == keys[:i+1]:
return True
return False
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
# May 15th
# Maximum Sum Circular Subarray
#
# Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.
#
# Here, a circular array means the end of the array connects to the beginning of the array. (Formally, C[i] = A[i] when 0 <= i < A.length, and C[i+A.length] = C[i] when i >= 0.)
#
# Also, a subarray may only include each element of the fixed buffer A at most once. (Formally, for a subarray C[i], C[i+1], ..., C[j], there does not exist i <= k1, k2 <= j with k1 % A.length = k2 % A.length.)
#
#
#
# Example 1:
#
# Input: [1,-2,3,-2]
# Output: 3
# Explanation: Subarray [3] has maximum sum 3
#
# Example 2:
#
# Input: [5,-3,5]
# Output: 10
# Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10
#
# Example 3:
#
# Input: [3,-1,2,-1]
# Output: 4
# Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4
#
# Example 4:
#
# Input: [3,-2,2,-3]
# Output: 3
# Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3
#
# Example 5:
#
# Input: [-2,-3,-1]
# Output: -1
# Explanation: Subarray [-1] has maximum sum -1
#
#
#
# Note:
#
# -30000 <= A[i] <= 30000
# 1 <= A.length <= 30000
#
# https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3330/
# In[ ]:
class Solution:
def maxSubarraySumCircular(self, A: List[int]) -> int:
# if the max subarray includes both the beginning and
# end, we can find the minimal subarray and subtract
# it from the sum
running_max = lambda a, x: max(a + x, x)
maximal = max(accumulate(A, running_max))
s = sum(A)
running_min = lambda a, x: min(a + x, x)
minimal = min(accumulate(A, running_min))
minimal = min(minimal, 0)
# handle edge cases with all negative elements
return maximal if s == minimal else max(maximal, s - minimal)
# May 16th
# Odd Even Linked List
#
# Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
#
# You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
#
# Example 1:
#
# Input: 1->2->3->4->5->NULL
# Output: 1->3->5->2->4->NULL
#
# Example 2:
#
# Input: 2->1->3->5->6->4->7->NULL
# Output: 2->3->6->7->1->5->4->NULL
#
#
#
# Constraints:
#
# The relative order inside both the even and odd groups should remain as it was in the input.
# The first node is considered odd, the second node even and so on ...
# The length of the linked list is between [0, 10^4].
# https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3331/
#
# In[ ]:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
order = 1
newlist=[]
tail= head
if head == None:
return head
while tail.next != None:
newlist.append(tail.val)
tail = tail.next
newlist.append(tail.val)
fuck=[]
for i in range(len(newlist)):
if i%2 == 0:
fuck.append(newlist[i])
for i in range(len(newlist)):
if i%2 == 1:
fuck.append(newlist[i])
def assign(head,list,start):
if head != None:
head.val = list[start]
assign(head.next,list,start+1)
assign(head,fuck,0)
return head
# Find All Anagrams in a String
#
# Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
#
# Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
#
# The order of output does not matter.
#
# Example 1:
#
# Input:
# s: "cbaebabacd" p: "abc"
#
# Output:
# [0, 6]
#
# Explanation:
# The substring with start index = 0 is "cba", which is an anagram of "abc".
# The substring with start index = 6 is "bac", which is an anagram of "abc".
#
# Example 2:
#
# Input:
# s: "abab" p: "ab"
#
# Output:
# [0, 1, 2]
#
# Explanation:
# The substring with start index = 0 is "ab", which is an anagram of "ab".
# The substring with start index = 1 is "ba", which is an anagram of "ab".
# The substring with start index = 2 is "ab", which is an anagram of "ab".
# https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3332/
#
# In[ ]:
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
slength = len(s)
pop = []
plength = len(p)
for i in range(slength):
if i+plength <= slength:
spart = s[i:i+plength]
if set(spart) == set(p):
for elements in set(spart):
if spart.count(elements) != p.count(elements):
print('found a fake')
break
pop.append(i)
pop = list(dict.fromkeys(pop))
return pop
# In[14]:
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxvalue= max(nums)
size = len(nums)
max_so_far = maxvalue
max_ending_here = 0
for i in range (0,size):
max_ending_here=max_ending_here+nums[i]
if max_ending_here <0:
max_ending_here=0
elif (max_so_far < max_ending_here):
max_so_far = max_ending_here
return max_so_far
# #4>. Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
#
# Example:
#
# Input: [0,1,0,3,12]
# Output: [1,3,12,0,0]
#
# Note:
#
# You must do this in-place without making a copy of the array.
# Minimize the total number of operations.
#
# In[4]:
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
size = len(nums)
cheat =[]
zeroter=0
x=0
for i in range(size):
#print(nums[x])
if nums[x] ==0:
#print(nums[x],"is a zero, removing")
nums.remove(0)
zeroter +=1
#print('current list is', nums)
x -=1
x +=1
for i in range(zeroter):
nums.append(0)
# #5>. Given an array of strings, group anagrams together.
#
# Example:
#
# Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
# Output:
# [
# ["ate","eat","tea"],
# ["nat","tan"],
# ["bat"]
# ]
#
# Note:
#
# All inputs will be in lowercase.
# The order of your output does not matter.
#
#
# In[37]:
from collections import defaultdict
class Solution(object):
def groupAnagrams(self, strs):
ans = defaultdict(list)