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