본문 바로가기

리트코드

(10)
[동적계획법] 릿코드 Medium 120 'Triangle' (Python) https://leetcode.com/problems/triangle/ Triangle - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 삼각형의 꼭대기에서 바닥까지의 합을 구하는 것이므로, 아래 칸으로 이동할 때 윗 칸의 숫자를 더해준다. 삼각형의 왼쪽 변의 숫자는 항상 왼쪽 숫자를 더하고, 오른쪽 변의 숫자는 항상 오른쪽 숫자를 더한다. 삼각형의 가운데 숫자들은 자신의 위의 숫자 2개에서 작은 값을 더하면 된다. 숫자를 다 더한 바닥의 숫자들 중 최솟값을 반..
[BFS] 릿코드 Medium 695 'Max Area of Island' (Python) https://leetcode.com/problems/max-area-of-island/ Max Area of Island - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 상하좌우로 1이 연결된 면적 구하기 최대 면적 구하기 class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: tmp = 0 answer = 0 dx = [-1,0,1,0] dy = [0,-1,0,1] for x in ..
[기타] 릿코드 Easy 557 'Reverse Words in a String III' (Python) https://leetcode.com/problems/reverse-words-in-a-string-iii/ Reverse Words in a String III - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열의 띄어쓰기를 기준으로 각 단어들이 뒤집으면 된다. class Solution: def reverseWords(self, s: str) -> str: answer = '' for i in s.split(): answer += (i[::-1] + '..
[정렬] 릿코드 Easy 167 'Two Sum II - Input Array Is Sorted' (Python) https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Two Sum II - Input Array Is Sorted - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 효율적인 알고리즘을 위해 bisect 라이브러리를 사용 양수가 포함될 때와 음수(0 포함)만 있을 때 나눠서 코딩 from bisect import bisect_right, bisect_left class Solution: def twoS..
[정렬] 릿코드 Easy 283 'Move Zeroes' (Python) https://leetcode.com/problems/move-zeroes/ Move Zeroes - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 파이썬 배열의 내장 메소드를 사용 class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ for i in range(nums.count(0)): nu..
[정렬] 릿코드 Medium 189 'Rotate Array' (Python) https://leetcode.com/problems/rotate-array/ Rotate Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com k번만큼 제일 오른쪽 값을 왼쪽으로 옮기는걸 반복하면 됨 class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ for i in range..
[정렬] 릿코드 Easy 977 'Squares of a Sorted Array' (Python) https://leetcode.com/problems/squares-of-a-sorted-array/ Squares of a Sorted Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 리스트의 각 값에 연산을 해야 하므로 벡터연산이 가능한 numpy를 import 함 import numpy as np class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: answer = np...
[이분탐색] 릿코드 Easy 35 'Search Insert Position' (Python) https://leetcode.com/problems/search-insert-position/ Search Insert Position - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 정렬된 배열에 값을 넣으면 그 값에 대한 인덱스를 찾아주는 파이썬 라이브러리 사용 from bisect import bisect_left class Solution: def searchInsert(self, nums: List[int], target: int) -> int..

LIST