Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- leetcode
- 데이터 파싱
- 오차역전파
- 컴퓨터비전
- rag parsing
- LLaVA
- multi-query
- Non-Maximum Suppression
- deep learning
- rag-fusion
- 합성곱 신경망
- anomaly detection
- Time Series
- Cross Entropy Error
- visual instruction tuning
- pdf parsing
- 활성화함수
- 손실함수
- Mean squared error
- 활성화 함수
- 이상탐지
- computer vision
- LLM
- 시계열
- nlp
- rrf
- segmentation
- 퍼셉트론
- E
- 딥러닝
Archives
- Today
- Total
굴러가는 분석가의 일상
[LeetCode] Two Sum 본문
문제
풀이
Brute Force 방식과는 달리 HashMap를 사용하게 된다면, 한번의 Iteration으로 결과 값 도출이 가능합니다.
nums = [2,1,5,3] 배열과 target = 4 값이 주어졌다고 가정해보겠습니다.
Value | Index | Difference | Hashmap |
2 | 0 | 4 - 2 = 2 | (2,0) |
1 | 1 | 4 - 1 = 3 | (1,1) |
5 | 2 | 4 - 5 = -1 | (5,2) |
처음으로 Target 값에서 각 배열의 element 차이를 구합니다. 이를 통해 도출되는 Difference 값이 중복되지 않았다면 Hashmap에 포함시켜주고, 중복 되어있다면 포함시키지 않습니다.
Class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
prevMap = {}
for i,n in enumerate(nums):
diff = target - n
if diff in prevMap:
return[prevMap[diff],i]
prevMap[n] = i
'Algorithm > Leetcode' 카테고리의 다른 글
[LeetCode] Valid Anagram (0) | 2024.02.16 |
---|---|
[LeetCode] Product of Array Except Self (0) | 2023.12.14 |
[LeetCode] SQL 50문제 (진행중) (0) | 2023.10.24 |
[LeetCode] Valid Sudoku (0) | 2023.10.12 |
[LeetCode] Contains Duplicate (0) | 2023.10.10 |