Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce3b1d0

Browse files
Initial commit
1 parent cbfdf81 commit ce3b1d0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def make_squares(arr):
2+
n = len(arr)
3+
squares = [0 for x in range(n)]
4+
highestSquareIdx = n - 1
5+
left, right = 0, n - 1
6+
while left <= right:
7+
leftSquare = arr[left] * arr[left]
8+
rightSquare = arr[right] * arr[right]
9+
if leftSquare > rightSquare:
10+
squares[highestSquareIdx] = leftSquare
11+
left += 1
12+
else:
13+
squares[highestSquareIdx] = rightSquare
14+
right -= 1
15+
highestSquareIdx -= 1
16+
17+
return squares
18+
19+
20+
def main():
21+
22+
print("Squares: " + str(make_squares([-2, -1, 0, 2, 3])))
23+
print("Squares: " + str(make_squares([-3, -1, 0, 1, 2])))
24+
25+
26+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /