File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 11// ๋น์ทํ ๋ฌธ์ ๋ฅผ ํผ ์ ์ด ์์ด์ ์ฝ๊ฒ ํด๊ฒฐ
22// https://www.acmicpc.net/problem/2294
3+ // O(N * amount) ์๊ฐ๋ณต์ก๋๊ฐ ๋ฐฐ์ด ํฌ๊ธฐ์ amount์ ์ข
์๋๋ค.
4+ // dp[N]๋ง ์ฌ์ฉํ๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(N)
35class Solution {
46 public int coinChange (int [] coins , int amount ) {
57 int [] dp = new int [amount + 1 ];
Original file line number Diff line number Diff line change 1+ // O(N^2) ์ด ๋์ฌ ์๋ฐ์ ์๋ ๋ฌธ์ . ์ด๋ฐ ๋ฌธ์ ์ ํน์ง์ N์ ํฌ๊ธฐ๊ฐ ์๋ค.
2+ // ์ด๋ฒ๋ฌธ์ ๋ N์ ํฌ๊ธฐ๊ฐ 1000์ผ๋ก ์ฃผ์ด์ก์๋, ์ด์ฐจ์ for๋ฌธ์ด ํ์ฉ๋๋ค๋๊ฑธ ๊ฐ์ ์ ์ผ๋ก ์์์ฑ ์ ์๋ค.
3+ // ์ด์ฐจ์ ๋ฐฐ์ด ์ด๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(N^2)
4+ class Solution {
5+ public int countSubstrings (String s ) {
6+ int n = s .length ();
7+ boolean [][] dp = new boolean [n ][n ];
8+ int count = 0 ;
9+
10+ for (int i = 0 ; i < n ; i ++) {
11+ dp [i ][i ] = true ;
12+ count ++;
13+ }
14+
15+ for (int len = 2 ; len <= n ; len ++) {
16+ for (int i = 0 ; i <= n - len ; i ++) { // ์์ ์์น
17+ int j = i + len - 1 ; // ๋ ์์น
18+
19+ // ์ ๋ ๋ฌธ์๊ฐ ๊ฐ๊ณ , ๋ด๋ถ๊ฐ ํ๋ฌธ์ด๊ฑฐ๋ ๊ธธ์ด๊ฐ 2์ธ ๊ฒฝ์ฐ
20+ if (s .charAt (i ) == s .charAt (j )) {
21+ if (len == 2 || dp [i + 1 ][j - 1 ]) {
22+ dp [i ][j ] = true ;
23+ count ++;
24+ }
25+ }
26+ }
27+ }
28+
29+ return count ;
30+ }
31+ }
You canโt perform that action at this time.
0 commit comments