diff --git a/findminmax.py b/findminmax.py new file mode 100644 index 00000000..7c3221e5 --- /dev/null +++ b/findminmax.py @@ -0,0 +1,34 @@ +# // Time Complexity : O(n) +# // Space Complexity : O(1) +# // Did this code successfully run on GeeksForGeeks : Yes +# // Any problem you faced while coding this : No + +class Solution: + def getMinMax(self, arr): + # code here + n=len(arr) + if n%2==0: + start=2 + if arr[0]>arr[1]: + min_e=arr[1] + max_e=arr[0] + else: + min_e=arr[0] + max_e=arr[1] + else: + start=1 + min_e=arr[0] + max_e=arr[0] + + i=start + while(iarr[i+1]): + min_e=min(min_e,arr[i+1]) + max_e=max(max_e,arr[i]) + else: + min_e=min(min_e,arr[i]) + max_e=max(max_e,arr[i+1]) + i+=2 + + + return [min_e,max_e] \ No newline at end of file diff --git a/findmissing.py b/findmissing.py new file mode 100644 index 00000000..9ed5af49 --- /dev/null +++ b/findmissing.py @@ -0,0 +1,25 @@ +# // Time Complexity : O(n) +# // Space Complexity : O(1) +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : No + +class Solution(object): + def findDisappearedNumbers(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + n=len(nums) + res=[] + + for i in range(n): + ind = abs(nums[i])-1 + if nums[ind]>0: + nums[ind]*=-1 + + for i in range(n): + if nums[i]>0: + res.append(i+1) + + + return res \ No newline at end of file diff --git a/gameOfLife.py b/gameOfLife.py new file mode 100644 index 00000000..4127e64c --- /dev/null +++ b/gameOfLife.py @@ -0,0 +1,39 @@ +# // Time Complexity : O(m*n) +# // Space Complexity : O(1) +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : NA +# +class Solution(object): + def gameOfLife(self, board): + """ + :type board: List[List[int]] + :rtype: None Do not return anything, modify board in-place instead. + """ + res=board + m=len(board) + n=len(board[0]) + surround=[(0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1)] + + for i in range(m): + for j in range(n): + live_around=0 + for s in surround: + r=i+s[0] + c=j+s[1] + + if r=0 and c=0 and abs(board[r][c])==1: + live_around+=1 + + if board[i][j]==0 and live_around==3: + board[i][j]=2 + + if board[i][j]==1 and (live_around>3 or live_around<2): + board[i][j]*=-1 + + for i in range(m): + for j in range(n): + if board[i][j]<=0: + board[i][j]=0 + else: + board[i][j]=1 +