File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ 1. ๋ฌธ์ ์ดํด
3+ N์ด ์ฃผ์ด์ง๋ฉด 1๋ถํฐ N๊น์ง์ ์ซ์๋ฅผ ์ด์ฉํด์ N๊น์ง ๋๋ฌ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์(+1,+2๋ง ๊ฐ๋ฅ)๋ฅผ ๋ฐํํด์ผ ํ๋ ๋ฌธ์ ์. ์ค๋ณต์ ํ์ฉํ๋ ์์ด์ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
4+
5+ 2. naive algorithm ๋์ถ
6+ ํผ๋ณด๋์น ์์ด์.
7+ N=1์ผ๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ 1
8+ N=2์ผ๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ 2
9+ N=3์ผ๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ 3 (n-2๋ฒ์งธ์ n-1๋ฒ์งธ์ ํฉ)
10+
11+ 3. ์๊ฐ๋ณต์ก๋ ๋ถ์
12+
13+ O(N)
14+ 4. ์ฝ๋์์ฑ
15+ */
16+ class sangyyypark {
17+ public int climbStairs (int n ) {
18+ if (n ==1 ) return 1 ;
19+ if (n ==2 ) return 2 ;
20+ int [] dp = new int [n +1 ];
21+ dp [0 ] = 0 ;
22+ dp [1 ] = 1 ;
23+ dp [2 ] = 2 ;
24+ for (int i = 3 ; i <= n ; i ++) {
25+ dp [i ] = dp [i -2 ] + dp [i -1 ];
26+ }
27+ return dp [n ];
28+ }
29+ }
30+
Original file line number Diff line number Diff line change 1+ /**
2+ 1. ๋ฌธ์ ์ดํด
3+ answer[i] ์๋ nums[i]๋ฅผ ์ ์ธํ ๋๋จธ์ง ์๋ค์ ๋ชจ๋ ๊ณฑ์
ํ์๋์ ๊ฒฐ๊ณผ๊ฐ์ด ๋ค์ด๊ฐ๋ ๋ฐฐ์ด์ ๋ฐํํ๋ ๋ฌธ์
4+
5+ 2. naive algorithm๋์ถ
6+
7+ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ answer[i]์ ๊ฐ์ ๋ฃ์๋ nums๋ฐฐ์ด์ ํ์ํด์ nums[i]๋ฅผ ์ ์ธํ ์๋ฅผ ๊ณฑ์
ํด์ ๋ฃ์ผ๋ฉด ๋์ด๋ค.
8+ ํ์ง๋ง, nums์ ๊ธธ์ด๊ฐ ๊ธธ๋ฉด ์๊ฐ๋ณต์ก๋๊ฐ O(N^2)์ด๋ค.
9+
10+ answer[i]์๋ nums[i]์ ์ผ์ชผ๊น์ง์ ๊ณฑ๊ณผ ์ค๋ฅธ์ชฝ ๊น์ง์ ๊ณฑ์ ๊ณฑํ๋ฉด ๋์ด๋ฏ๋ก
11+ answer[i]์ nums[i]์ ์ผ์ชฝ๊น์ง ํฉ์ ๋ฃ์ด๋๊ณ answer[i]์ nums[i]์ ์ค๋ฅธ์ชฝ๊ฐ์ง์ ๊ณฑ์ ๊ณฑํ๋ค.
12+
13+ 3. ์๊ฐ๋ณต์ก๋ ๋ถ์
14+
15+ 4. ์ฝ๋๊ตฌํ
16+ */
17+ class sangyyypark {
18+ public int [] productExceptSelf (int [] nums ) {
19+ int [] answer = new int [nums .length ];
20+
21+ int left = 1 ;
22+ for (int i = 0 ; i < nums .length ; i ++) {
23+ answer [i ] = left ;
24+ left = nums [i ] * left ;
25+ }
26+
27+ int right = 1 ;
28+ for (int i = nums .length - 1 ; i >= 0 ; i --) {
29+ answer [i ] = answer [i ] * right ;
30+ right = nums [i ] * right ;
31+ }
32+ return answer ;
33+ }
34+
35+ }
36+
You canโt perform that action at this time.
0 commit comments