-
Notifications
You must be signed in to change notification settings - Fork 10
/
hw3.py
209 lines (189 loc) · 4.29 KB
/
hw3.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
##################################
# #
# Homework 3 #
# Released: September 26, 2017 #
# Due: October 10, 2017 #
# #
##################################
# Matrix Transpose
#
# Description:
# Given an m x n matrix A, return A^T, that is,
# return the transpose of the matrix A.
#
# Example(s):
#
# Example 1:
# Input:
# A = [[1]]
# Output:
# [[1]]
#
# Example 2:
# Input:
# A = [[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]
# Output:
# [[1, 4, 7],
# [2, 5, 8],
# [3, 6, 9]]
#
def matrix_transpose(A):
pass
# Max Element in 2-D Array
#
# Description:
# Given a 2-d array grid of integers,
# determine the maximum number in grid.
#
# Example(s):
# Example 1:
# Input:
# grid = [[4, 2],
# [3, -1]]
# Output:
# 4
#
# Example 2:
# Input:
# grid = [[-300, -200],
# [-300, -100]]
# Output:
# -100
#
def max_2d_array(grid):
pass
# Binary Search
#
# Description:
# Given a sorted (increasing) array with distinct integers and a
# target integer, determine the index of target in the given array.
# If target is not in the array, return None. Try to use the fact
# that the array is sorted to optimize your algorithm.
#
# Example(s):
#
# Example 1:
# Input:
# arr = [1, 2, 3, 4, 5]
# target = 3
# Output:
# 2
#
# Example 2:
# Input:
# arr = [1, 2, 3, 4, 5]
# target = 0
# Output:
# None
#
def binary_search(arr, target):
pass
# Sorted Matrix Search
#
# Description:
# Given a square 2d array of integers and a target integer
# return the coordinates of the target integer as a tuple
# in the form (row, col) if the element exists in the matrix,
# or None if the element does not exists. The 2d array
# is guaranteed to be sorted ascending row-wise,
# and the zeroth element of each row is strictly larger than
# the last element of the previous row.
#
# Example(s):
# Example 1:
# Input:
# arr = [[1 , 2 , 3],
# [8 , 11, 16],
# [23, 24, 25]]
# target = 8
# Output:
# (1, 0)
#
# Example 2:
# Input:
# arr = [[1 , 2 , 3],
# [8 , 11, 16],
# [23, 24, 25]]
# target = 20
# Output:
# None
#
def search_2d_array(arr, target):
pass
# Maximum Sum Subarray
#
# Description:
# Given an array of integers, determine the maximum sum of
# a continuous subarray of the given array.
#
# Examples(s):
# Example 1:
# Input:
# arr = [1, 2, 3, 4, 5]
# Ouput:
# 15
#
# Example 2:
# Input:
# arr = [1, -3, 4, 1, -2, 3]
# Output:
# 6
#
def max_sum_subarray(arr):
pass
# Maximum Sum Sub-Rectangle
#
# Description:
# Given a 2-d array of integers, determine the
# maximum sub-rectangle sum.
#
# Example(s):
# Example 1:
# Input:
# grid = [[1, 2],
# [3, 4]]
# Output:
# 10
#
# Example 2:
# Input:
# grid = [[1, 2],
# [-3, 0]]
# Ouput:
# 3
#
# Example 3:
# Input:
# grid = [[ 1, -2, 0],
# [-1, 3, 0],
# [ 3, -1, -9]]
# Output:
# 4
#
def max_sum_subrectangle(grid):
pass
# Maximum Number of Times an Array can be Flattened
#
# Description:
# Given an array of integers and arrays,
# return the maximum number of times arr can be flattened.
# For example, if arr = [1, 2, [3, 4], 5],
# then arr could be flattened once.
#
# Example(s):
# Example 1:
# Input:
# arr = [1, 2, [3, 4], 5]
# Output:
# 1
#
# Example 2:
# Input:
# arr = [1, 2, 3, 4, 5]
# Output:
# 0
#
def max_array_flatten(arr):
pass