Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 파이썬
- two pointers
- SQL
- simulation
- Data Structure
- dynamic programming
- Math
- Stack
- 코테
- 구현
- database
- hash table
- Number Theory
- array
- 코딩테스트
- Counting
- implement
- geometry
- Tree
- Class
- java
- Method
- Matrix
- string
- bit manipulation
- sorting
- Binary Tree
- greedy
- 자바
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1646. Get Maximum in Generated Array 본문
1. Input
1) int n
2. Output
1) 길이가 n+1인 배열을 다음과 같은 규칙으로 만든 후, 배열의 요소 중 가장 큰 값을 반환
- nums[0] = 0
- nums[1] = 1
- nums[2*i] = nums[i] (2 <= 2*i <= n)
- nums[2*i+1] = nums[i] + nums[i+1] (2 <= 2*i+1 <= n)
3. Constraint
1) 0 <= n <= 100
4. Example
Input: n = 7 -> Output: 3
설명:
- nums[0] = 0
- nums[1] = 1
- nums[(1 * 2) = 2] = nums[1] = 1
- nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
- nums[(2 * 2) = 4] = nums[2] = 1
- nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
- nums[(3 * 2) = 6] = nums[3] = 2
- nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
5. Code
1) 첫 코드(2023/04/15)
int[] nums = new int[n+1];
int max = nums[0];
if(n>=1){
nums[1] = 1; max = nums[1];
for(int i=2 ; i<nums.length ; i++){
if(i%2==0) nums[i] = nums[i/2];
else nums[i] = nums[i/2] + nums[i/2+1];
if(nums[i]>max) max = nums[i];
}
}
return max;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1694. Reformat Phone Number (0) | 2023.04.16 |
---|---|
[LeetCode/Easy] 1652. Defuse the Bomb (0) | 2023.04.15 |
[LeetCode/Easy] 1588. Sum of All Odd Length Subarrays (0) | 2023.04.13 |
[LeetCode/Easy] 1598. Crawler Log Folder (0) | 2023.04.13 |
[LeetCode/Easy] 1592. Rearrange Spaces Between Words (0) | 2023.04.13 |