반응형
250x250
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
- LeetCode 83번
- 브루트포스 알고리즘
- 정렬
- 연결리스트 정렬
- 사칙연산
- KMP알고리즘
- 유클리드 호제법
- 해시를 사용한 집합과 맵
- 큰 수 연산
- LeetCode Remove Duplicates from Sorted List in c
- 시뮬레이션
- LeetCode 83 c언어
- 연결리스트 중복제거
- 큐
- 실패함수
- 이분 탐색
- 정수론
- 별 찍기
- 임의 정밀도 / 큰 수 연산
- 수학
- 조합론
- 문자열제곱
- 문자열
- 재귀
- 다이나믹 프로그래밍
- 자료 구조
- 프로그래머스
- 구현
- Queue
- 스택
Archives
- Today
- Total
hahn
[LeetCode - C] 70. Climbing Stairs 본문
728x90
반응형
Climbing Stairs - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
- 1 <= n <= 45
Solution 1
뭔가 형태가 피보나치로 나올 것 같아서
피보나치로 풀었는데 풀렸다.
int climbStairs(int n)
{
int p;
int s;
int ret;
if (n == 1)
return (1);
if (n == 2)
return (2);
p = 1;
ret = 2;
n--;
while (--n)
{
s = ret;
ret = p + ret;
p = s;
}
return (ret);
}
Solution 2
이번에는 재귀로 풀었다.
시간 복잡도에서 걸리길래
rec 추가해서 진행했다.
static rec[45];
int climbStairs(int n)
{
if (n <= 1)
return (1);
else if (rec[n - 1])
return (rec[n - 1]);
else
rec[n - 1] = climbStairs(n - 1) + climbStairs(n - 2);
return (rec[n - 1]);
}
728x90
반응형
'코딩테스트 연습 > LeetCode(C - Easy)' 카테고리의 다른 글
[LeetCode - C] 83. Remove Duplicates from Sorted List (0) | 2022.04.18 |
---|---|
[LeetCode - C] 69. Sqrt(x) (0) | 2022.04.18 |
[LeetCode - C] 67. Add Binary (0) | 2022.04.18 |
[LeetCode - C] 66. Plus One (0) | 2022.04.16 |
[LeetCode - C] 58. Length of Last Word (0) | 2022.04.12 |