forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsungjinwi.cpp
More file actions
32 lines (25 loc) ยท 951 Bytes
/
Copy pathsungjinwi.cpp
File metadata and controls
32 lines (25 loc) ยท 951 Bytes
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
32
/*
ํ์ด :
๋ง์ง๋ง ์ธ๋ฑ์ค๋ฅผ ์์์ ์ผ๋ก ํ๋ ๋ณ์ lastReachable ์ ์ธ
๋ง์ง๋ง ์ธ๋ฑ์ค์ ๋๋ฌํ ์ ์๋ ๊ฐ์ฅ ์์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅ
์์์ ๋ด๋ ค์ค๋ฉด์ lastReachable ๋์ด์ ์ ํํด์ ๋ฟ์ ์ ์๋ ๊ฐ์ฅ ์์ ๋์ด๋ฅผ ์๋ก์ด lastReachable๋ก ์
๋ฐ์ดํธ
๋ฐ๋ณต๋ฌธ์ด ๋๋๊ณ lastReachable == 0์ ๋ฆฌํดํ๋ค
nums์ ๊ธธ์ด : N
TC : O(N)
for๋ฌธ ๋ฐ๋ณต 1ํ
SC : O(1)
๋ณ์ lastReachable ์ธ์ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์์
*/
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
int lastReachable = nums.size() - 1;
for (int i = nums.size() - 2; i >= 0; i--) {
if (i + nums[i] >= lastReachable)
lastReachable = i;
}
return lastReachable == 0;
}
};