일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Regular Expression
- 밴픽
- 시바견
- Python Code
- 715. Range Module
- 30. Substring with Concatenation of All Words
- attribute
- 43. Multiply Strings
- 컴퓨터의 구조
- iterator
- Python
- 109. Convert Sorted List to Binary Search Tree
- shiba
- 315. Count of Smaller Numbers After Self
- t1
- Class
- DWG
- LeetCode
- kaggle
- Generator
- Python Implementation
- Convert Sorted List to Binary Search Tree
- concurrency
- Decorator
- 프로그래머스
- 운영체제
- 파이썬
- Substring with Concatenation of All Words
- Protocol
- data science
Archives
- Today
- Total
Scribbling
[Programmers] 도둑질 본문
https://school.programmers.co.kr/learn/courses/30/lessons/42897?language=cpp
1. Python
def solution(money):
def helper(money):
if not money:
return 0
if len(money) == 1:
return money[0]
s1 = money[0]
s2 = max(money[0], money[1])
ret = s2
for i in range(2, len(money)):
m = money[i]
ret = max(ret, s1 + m, s2)
s1, s2 = s2, ret
return ret
return max(helper(money[1:]), helper(money[:-1]))
2. C++
int helper(const vector<int>& money);
int solution(vector<int> money) {
return max(helper(vector<int>(money.begin(), money.end()-1)), helper(vector<int>(money.begin()+1, money.end())));
}
int helper(const vector<int>& money) {
if (money.empty()) return 0;
if (money.size() == 1) return money[0];
int s1 = money[0];
int s2 = max(money[0], money[1]);
for (int i=2; i<money.size(); i++) {
int m = money[i];
int tmp = s1;
s1 = s2;
s2 = max(tmp+m, s2);
}
return s2;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 네트워크 (0) | 2024.06.20 |
---|---|
[Programmers] 타겟 넘버 (0) | 2024.06.20 |
[Programmers] 사칙연산 (0) | 2024.06.18 |
[Programmers] 등굣길 (0) | 2024.06.17 |
[Programmers] 정수 삼각형 (0) | 2024.06.17 |