[Leetcode] 1. Two Sum
◎ 자료구조와 알고리즘/Leetcode2024. 5. 8. 15:24[Leetcode] 1. Two Sum

문제 링크 : https://leetcode.com/problems/two-sum/description/🖥️ 시작하며브루트 포스 방법으로 접근할 수 있다.이중 for문 으로 순차적으로 모든 배열을 탐색하며 결괏값을 찾으면 된다. 조건에 You may assume that each input would have ***exactly* one solution**, 라고 명시했으므로 쉬운 문제라 할 수 있다.⚙️ Pythonclass Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i + 1, len(nums)): ..

[Leetcode] 2816. Double a Number Represented as a Linked List
◎ 자료구조와 알고리즘/Leetcode2024. 5. 8. 02:17[Leetcode] 2816. Double a Number Represented as a Linked List

[Leetcode] 2816.문제 링크 : https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/?envType=daily-question&envId=2024-05-07🖥️ 시작하며간단한 연결 리스트 문제다.받은 연결리스트에서 숫자를 문자열로 하나하나 저장문자열을 숫자로 바꾼 후, 2배결과물을 다시 연결 리스트로 저장class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = nextclass Solution: def doubleIt(self, head: Optional[ListNode]) -> Optio..

[Leetcode] 42. Trapping Rain Water
◎ 자료구조와 알고리즘/Leetcode2024. 5. 8. 00:43[Leetcode] 42. Trapping Rain Water

문제 링크 : https://leetcode.com/problems/trapping-rain-water/description/🖥️ 시작하며처음 접근이 어려웠던 문제였다.우선 height 배열에 검은색 블럭의 높이가 주어지고, 비가 왔을 시 웅덩이가 생기는 칸이 몇 칸인지 답을 도출해야 하는 문제다.아마 직관적으로는 왼쪽 벽과 오른쪽 벽 중 낮은 벽의 높이로 바로 접근할 수 있지 않나 싶다.그래서 본인은 왼쪽 벽의 최대 높이를 저장하는 배열, 오른쪽 벽의 최대 높이를 저장하는 배열을 생성하는 쪽으로 가닥을 잡았다.아래 그림을 중심으로 파헤쳐보자.결국 이를 식으로 풀어내자면**왼쪽과 오른쪽 벽의 최솟값 - 검은색 블록 높이** 라고 할 수 있다.Pythonclass Solution: def trap(..

image