Skip to content

Commit

Permalink
feat: add alternative s2 solution (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
justapotato213 authored Feb 22, 2023
1 parent 53a45b2 commit 4376b43
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion 2023/s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,26 @@
if grid[i][j] < ans[j - i]:
ans[j - i] = grid[i][j]

print(" ".join(str(i) for i in ans))
print(" ".join(str(i) for i in ans))

# Below is an alternative solution

# CCC '23 S2 - William Shen
n = int(input())
heights = list(map(int, input().split()))
ans = [1000000000000] * (n + 5)
for i in range(n):
cur = 0
for j in range(n + 1):
if i - j < 0 or i + j >= n:
break
cur += abs(heights[i - j] - heights[i + j])
ans[2 * j + 1] = min(ans[2 * j + 1], cur)
for i in range(n):
cur = 0
for j in range(n + 1):
if i - j < 0 or i + j + 1>= n:
break
cur += abs(heights[i - j] - heights[i + j + 1])
ans[2 * j + 2] = min(ans[2 * j + 2], cur)
print(*ans[1:n + 1])

0 comments on commit 4376b43

Please sign in to comment.