File tree 3 files changed +99
-0
lines changed
product-of-array-except-self
3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
1
+ class SolutionClimbStairs {
2
+
3
+ public int climbStairs (int n ) {
4
+ // n๋ฒ์งธ ๊ณ๋จ๊น์ง ์ค๋ฅด๋ ๋ฐฉ๋ฒ์ ์
5
+ // ์ฒซ ๋ฒ์งธ ๊ณ๋จ๊น์ง ์ค๋ฅด๋ ๋ฐฉ๋ฒ์ 1๊ฐ์ง
6
+ // ๋ ๋ฒ์งธ ๊ณ๋จ๊น์ง ์ค๋ฅด๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง
7
+ // ์ธ ๋ฒ์งธ ๊ณ๋จ๋ถํฐ๋ ์ด์ ๋ ๊ณ๋จ์ ๋ฐฉ๋ฒ์ ๋ํ์ฌ ๊ณ์ฐ
8
+ // stairs[i]๋ i๋ฒ์งธ ๊ณ๋จ๊น์ง ์ค๋ฅด๋ ๋ฐฉ๋ฒ์ ์
9
+ // ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N)
10
+ if (n == 1 ) {
11
+ return 1 ;
12
+ }
13
+
14
+ if (n == 2 ) {
15
+ return 2 ;
16
+ }
17
+
18
+ int [] stairs = new int [n +1 ];
19
+ stairs [1 ] = 1 ;
20
+ stairs [2 ] = 2 ;
21
+
22
+
23
+ for (int i =3 ; i <=n ; i ++) {
24
+ stairs [i ] = stairs [i -1 ] + stairs [i -2 ];
25
+ }
26
+
27
+ return stairs [n ];
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ class SolutionProductExceptSelf {
2
+
3
+ public int [] productExceptSelf (int [] nums ) {
4
+ // answer[i] = nums[0] * nums[1] * ... * nums[i-1] * nums[i+1] * ... * nums[n-1]
5
+ // answer[i] = left[i] * right[i]
6
+ // left[i] = nums[0] * nums[1] * ... * nums[i-1]
7
+ // right[i] = nums[i+1] * ... * nums[n-1]
8
+ // left[i] = left[i-1] * nums[i-1]
9
+ // right[i] = right[i+1] * nums[i+1]
10
+ // answer[i] = left[i] * right[i]
11
+ // ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N)
12
+ int n = nums .length ;
13
+ int [] left = new int [n ];
14
+ int [] right = new int [n ];
15
+ int [] answer = new int [n ];
16
+
17
+ left [0 ] = 1 ;
18
+ right [n - 1 ] = 1 ;
19
+
20
+ for (int i = 1 ; i < n ; i ++) {
21
+ left [i ] = left [i - 1 ] * nums [i - 1 ];
22
+ }
23
+
24
+ for (int i = n - 2 ; i >= 0 ; i --) {
25
+ right [i ] = right [i + 1 ] * nums [i + 1 ];
26
+ }
27
+
28
+ for (int i = 0 ; i < n ; i ++) {
29
+ answer [i ] = left [i ] * right [i ];
30
+ }
31
+
32
+ return answer ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .HashMap ;
3
+ import java .util .List ;
4
+
5
+ class SolutionTwoSum {
6
+ public int [] twoSum (int [] nums , int target ) {
7
+ // ๋ชจ๋ ์๋ฅผ ํด์๋งต์ ์ ์ฅํ๋ค. key = nums[i], value = i
8
+ // ํด์๋งต์ ์ํํ๋ฉฐ target - key = result๋ฅผ ๋ง์กฑํ๋ ์์ ์ฐพ์
9
+ // key ์ result ์ value๋ฅผ ์ ๋ต์ผ๋ก ๋ฐํํ๋ค
10
+ // ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(N)
11
+ // ๊ฐ๋ณ ์ธ๋ฑ์ค ๋ฆฌ์คํธ๋ฅผ ์ ์ฅํ ํด์๋งต
12
+ HashMap <Integer , List <Integer >> indicesByValue = new HashMap <>();
13
+
14
+ for (int i = 0 ; i < nums .length ; i ++) {
15
+ indicesByValue .computeIfAbsent (nums [i ], k -> new ArrayList <>()).add (i );
16
+ }
17
+
18
+ for (int key : indicesByValue .keySet ()) {
19
+ int diff = target - key ;
20
+
21
+ if (indicesByValue .containsKey (diff )) {
22
+ int index1 = indicesByValue .get (key ).get (0 );
23
+
24
+ // ๋์ผํ ๊ฐ์ ๋ํด ๋ ๊ฐ์ ๋ค๋ฅธ ์ธ๋ฑ์ค๊ฐ ์๋์ง ํ์ธ
25
+ int index2 = (key == diff && indicesByValue .get (key ).size () > 1 )
26
+ ? indicesByValue .get (key ).get (1 )
27
+ : indicesByValue .get (diff ).get (0 );
28
+
29
+ return new int []{index1 , index2 };
30
+ }
31
+ }
32
+
33
+ // ์์ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ
34
+ return null ;
35
+ }
36
+ }
You canโt perform that action at this time.
0 commit comments