일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 715. Range Module
- iterator
- data science
- 109. Convert Sorted List to Binary Search Tree
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- Python Code
- 운영체제
- Decorator
- 컴퓨터의 구조
- 시바견
- Convert Sorted List to Binary Search Tree
- shiba
- t1
- 파이썬
- 밴픽
- Generator
- DWG
- LeetCode
- attribute
- Python
- 프로그래머스
- Protocol
- Python Implementation
- Regular Expression
- 30. Substring with Concatenation of All Words
- Class
- 43. Multiply Strings
- kaggle
- concurrency
Archives
- Today
- Total
Scribbling
LeetCode: 135. Candy 본문
Key idea: one's number of candies is affected by all the left and right neighbors.
So we iterate the list twice, forward and backward.
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
candies = [1] * n
# forward pass
for i in range(1, n):
if ratings[i] > ratings[i-1]:
candies[i] = candies[i-1] + 1
# backward pass
for i in range(n-2, -1, -1):
if ratings[i] > ratings[i+1]:
candies[i] = max(candies[i], candies[i+1]+1)
return sum(candies)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 410. Split Array Largest Sum (0) | 2022.02.17 |
---|---|
LeetCode: 315. Count of Smaller Numbers After Self (0) | 2022.02.10 |
LeetCode: 366. Find Leaves of Binary Tree (0) | 2022.02.02 |
LeetCode: 109. Convert Sorted List to Binary Search Tree (0) | 2022.01.26 |
LeetCode: 302. Smallest Rectangle Enclosing Black Pixels (0) | 2022.01.25 |