일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 컴퓨터의 구조
- 시바견
- data science
- Python Code
- attribute
- Class
- 43. Multiply Strings
- t1
- Regular Expression
- 30. Substring with Concatenation of All Words
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- iterator
- Python
- 운영체제
- 715. Range Module
- DWG
- Generator
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- Decorator
- 파이썬
- Python Implementation
- kaggle
- 315. Count of Smaller Numbers After Self
- concurrency
- Protocol
- 프로그래머스
- LeetCode
- shiba
Archives
- Today
- Total
Scribbling
[Programmers] 정수 삼각형 본문
https://school.programmers.co.kr/learn/courses/30/lessons/43105?language=java
1. Python
def solution(triangle):
ret = 0
m = len(triangle)
for i in range(1, m):
for j in range(len(triangle[i])):
left = triangle[i-1][j-1] if j != 0 else 0
right = triangle[i-1][j] if j != len(triangle[i]) - 1 else 0
triangle[i][j] = max(left, right) + triangle[i][j]
ret = max(ret, triangle[i][j])
return ret
2. C++
int solution(vector<vector<int>> triangle) {
int ret = 0;
int m = triangle.size();
for (int i=1; i<m; i++) {
for (int j=0; j<triangle[i].size(); j++) {
int left = j != 0 ? triangle[i-1][j-1] : 0;
int right = j != triangle[i].size() - 1 ? triangle[i-1][j] : 0;
triangle[i][j] = max(left, right) + triangle[i][j];
ret = max(ret, triangle[i][j]);
}
}
return ret;
}
3. Java
import static java.lang.Math.max;
class Solution {
public int solution(int[][] triangle) {
int ret = 0;
int m = triangle.length;
for (int i=0; i<m; i++) {
for (int j=0; j<triangle[i].length; j++) {
int left = j != 0 ? triangle[i-1][j-1] : 0;
int right = j != triangle[i].length - 1 ? triangle[i-1][j] : 0;
triangle[i][j] = max(left, right) + triangle[i][j];
ret = max(ret, triangle[i][j]);
}
}
return ret;
}
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 사칙연산 (0) | 2024.06.18 |
---|---|
[Programmers] 등굣길 (0) | 2024.06.17 |
[Programmers] N으로 표현 (1) | 2024.06.13 |
Design HashMap -- Python Implementation (0) | 2023.10.05 |
LeetCode: 173. Binary Search Tree Iterator (0) | 2023.09.30 |