Skip to content
Merged
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
40 changes: 40 additions & 0 deletions leetcode3/변지협/3912. Valid Elements in an Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

'''
1. 아이디어 :
왼쪽 오른쪽 최대 구하고 비교
2. 시간복잡도 :
O(n^2)
3. 자료구조/알고리즘 :
'''
class Solution:
def findValidElements(self, nums: list[int]) -> list[int]:
left = [0] * len(nums)
right = [0] * len(nums)

_max = 0
for i in range(len(nums)):
_max = max(nums[i], _max)
left[i] = _max

_max = 0
for i in range(len(nums)-1, -1, -1):
_max = max(nums[i], _max)
right[i] = _max

print(left, right)

ans = []
for i in range(len(nums)-1):
if i == 0:
ans.append(nums[i])
continue

# if i != 0 and i == len(nums) - 1:
# ans.append(nums[i])

if max(left[:i]) < nums[i] or max(right[i+1:]) < nums[i]:
ans.append(nums[i])

# if len(nums) != 1:
ans.append(nums[len(nums)-1])
return ans