//GitHub https://github.com/vellimole0621
vellimole0621 - Overview
キム・ヒョンギュ . vellimole0621 has 6 repositories available. Follow their code on GitHub.
github.com
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
:
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
After doing so, return the array.
=> 주어진 배열을, 각 요소의 오른쪽 기준으로 가장 큰 값으로 하고, 마지막 인덱스에는 -1으로 하는 배열로 바꾸어라.
내 풀이
:
최종 답을 담을 배열과 중간 값 , 요소 변수 생성 => 요소가 하나인 경우 -1 배열에 추가 => 아닌 경우 끝 값인 경우 -1, 끝 전 값인 경우 끝값 추가, 중간 변수 보다 인덱스가 전인 경우 그 인덱스 값을 추가하고 아닌 경우 반복문으로 비교해서 추가시킨다.
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
temp = []
temp_num = 0
temp_idx = -1
if len(arr) == 1:
temp.append(-1)
else:
for i in range(0,len(arr)):
if i == len(arr) - 1:
temp.append(-1)
elif (i + 1 == len(arr) -1):
temp.append(arr[len(arr) - 1])
else:
if temp_idx > i:
temp.append(arr[temp_idx])
else:
for j in range(i+1, len(arr)):
if arr[j] > temp_num:
temp_num = arr[j]
temp_idx = j
temp.append(arr[temp_idx])
temp_num = 0
return temp
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Move Zeroes (0) | 2023.09.19 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Remove Duplicates from Sorted Array (0) | 2023.09.18 |
[파이썬 Python/알고리즘] LeetCode - Check If N and Its Double Exist (0) | 2023.09.11 |
[파이썬 Python/알고리즘] LeetCode - Remove Element (0) | 2023.09.07 |
[파이썬 Python/알고리즘] LeetCode - Merge Sorted Array (0) | 2023.09.05 |