Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
Array-1

Problem 1

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in O(n).

Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
'''

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]: # type: ignore
if nums == None or len(nums) == 0:
return []
n = len(nums)
rp = 1
result = [1 for i in range(n)]
#result[0] = 1
#left prod
for i in range(1, n):
rp = rp * nums[i-1]
result[i] = rp

#right prod, multiply right prod with value in arr
rp = 1

for i in range(n-2, -1, -1):
rp = rp * nums[i+1]
result[i] = result[i] * rp

return result
58 changes: 58 additions & 0 deletions 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'''
Problem 2

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.

Example:

Input:

[

[ 1, 2, 3 ],

[ 4, 5, 6 ],

[ 7, 8, 9 ]

]

Output: [1,2,4,7,5,3,6,8,9]
'''

class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # type: ignore
if mat == None or len(mat) == 0:
return []
m = len(mat)
n = len(mat[0])
result = [0 for i in range(m*n)]
index = 0 #on result arr
r = 0
c = 0
flag = 1
while index < m*n:
result[index] = mat[r][c]
index = index+1
if flag == 1:
if c == n-1:
r = r+1
flag = 0
elif r == 0:
c = c+1
flag = 0
else:
r = r-1
c = c+1
else:
if r == m-1:
c = c+1
flag = 1
elif c == 0:
r = r+1
flag = 1
else:
r = r+1
c = c-1
return result

68 changes: 68 additions & 0 deletions 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'''
Problem 3

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:

[

[ 1, 2, 3 ],

[ 4, 5, 6 ],

[ 7, 8, 9 ]

] Output: [1,2,3,6,9,8,7,4,5] Example 2:

Input:

[

[1, 2, 3, 4],

[5, 6, 7, 8],

[9,10,11,12]

] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
'''

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: # type: ignore
m = len(matrix)
n = len(matrix[0])

result = []
top = 0
bottom = m-1
left = 0
right = n-1

while top <= bottom and left <= right:
#top row
if top <= bottom and left <= right:
for j in range(left, right+1):
result.append(matrix[top][j])
top = top+1

#right col
if top <= bottom and left <= right:
for i in range(top, bottom+1):
result.append(matrix[i][right])
right = right -1

#bottom row
if top <= bottom and left <= right:
for j in range(right, left-1, -1):
result.append(matrix[bottom][j])
bottom = bottom -1

#left col
if top <= bottom and left <= right:
for i in range(bottom, top-1, -1):
result.append(matrix[i][left])
left = left + 1
return result
6 changes: 0 additions & 6 deletions Sample.java

This file was deleted.