Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Kaggle
- 편스토랑
- 코로나19
- 편스토랑 우승상품
- 프로그래머스 파이썬
- 데이콘
- 캐치카페
- 자연어처리
- 금융문자분석경진대회
- programmers
- 우분투
- gs25
- 백준
- dacon
- leetcode
- Docker
- PYTHON
- AI 경진대회
- Baekjoon
- github
- SW Expert Academy
- 파이썬
- ubuntu
- Git
- 맥북
- hackerrank
- Real or Not? NLP with Disaster Tweets
- 프로그래머스
- ChatGPT
- 더현대서울 맛집
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 198. House Robber (Python) 본문
728x90
반응형
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
- 0 <= nums.length <= 100
- 0 <= nums[i] <= 400
Solution
class Solution():
def rob(self, nums):
now = 0
last = 0
for i in nums:
last, now = now, max(i+last, now)
return now
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 989. Add to Array-Form of Integer (Python) (0) | 2020.07.11 |
---|---|
[HackerRank] Day 14: Scope (Python) (0) | 2020.07.10 |
[BaekJoon] 9095번 : 1, 2, 3 더하기 (Python) (0) | 2020.07.08 |
[BaekJoon] 10808번 : 알파벳 개수 (Python) (0) | 2020.07.07 |
[BaekJoon] 10809번 : 알파벳 찾기 (Python) (0) | 2020.07.06 |
Comments