| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- DWG
- 컴퓨터의 구조
- t1
- 운영체제
- Substring with Concatenation of All Words
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- 파이썬
- Protocol
- Class
- shiba
- Python Implementation
- 43. Multiply Strings
- iterator
- Decorator
- attribute
- concurrency
- kaggle
- 프로그래머스
- Python
- 밴픽
- data science
- Regular Expression
- Convert Sorted List to Binary Search Tree
- Generator
- 시바견
- 715. Range Module
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 |