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 | 29 | 30 |
| 31 |
Tags
- code
- DS
- adaptive remeshing
- coding
- 코테
- 대학생
- CS
- 컴퓨터공학
- Database
- sort
- meshgraphnet
- 티스토리챌린지
- Mesh
- 개발자
- GNN
- Leet Code
- SQL
- db
- 데베
- 데이터베이스
- 컴퓨터사이언스
- 데이터
- 자료구조
- LeetCode
- Python
- 코딩테스트
- CNN
- 오블완
- Data_Structure
- mysql
Archives
- Today
- Total
sy1214ei 님의 블로그
[Leet Code] 594. Longest Harmonious Subsequence - Python 본문
class Solution:
def findLHS(self, nums: List[int]) -> int:
# 1. 빈도수를 저장하는 딕셔너리 만들기
# 2. for문으로 nums를 돌면서 각 숫자의 빈도수 저장
# 3. for num in freq 돌면서
# num+1 값이 존재한다면
# freq[num] + freq[num+1] 값이 최대인 값 max_lang에 저장
freq = {}
for num in nums: # save the frequence
if num in freq:
freq[num] += 1
else:
freq[num] = 1
max_lang = 0
for num in freq:
if num+1 in freq:
max_lang = max(max_lang, freq[num]+freq[num+1])
return max_lang
# Time Complexity : O(n)
# Space Complextiy : O(n)'[Coding]' 카테고리의 다른 글
| [Leet Code] 747. Largest Number At Least Twice of Others - Python (0) | 2024.11.25 |
|---|---|
| [Leet Code] 628. Maximum Product of Three Numbers - Python (0) | 2024.11.25 |
| [Leet Code] 561. Array Partition (0) | 2024.11.25 |
| [Leet Code] 414. Third Maximum Number (1) | 2024.11.25 |
| [Leet Code] 350. Intersection of Two Arrays II (0) | 2024.11.25 |