Skip to content

Commit f9ab807

Browse files
Merge pull request #1713 from CodingTestStudy2/이진희
[이진희] Day29
2 parents ced0fe0 + 0025b8e commit f9ab807

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
3+
1. 아이디어 : DP를 활용한다. 가장 많이 선을 그을 수 있는 경우를 구한다.
4+
각 단계의 경우는 이전단계에 영향을 받으므로 DP를 활용한다.
5+
6+
2. 시간복잡도 : ON*M) (nums1.length = N, nums2.length = M)
7+
8+
3. 자료구조/알고리즘 : DP
9+
10+
*/
11+
12+
class Solution {
13+
public int maxUncrossedLines(int[] nums1, int[] nums2) {
14+
int[][] dp = new int[nums1.length+1][nums2.length+1];
15+
16+
for(int i=1; i<=nums1.length; i++) {
17+
for(int j=1; j<=nums2.length; j++) {
18+
// 연결 가능할 때
19+
if(nums1[i-1] == nums2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
20+
// 연결할 수 없을 때
21+
else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
22+
}
23+
}
24+
25+
return dp[nums1.length][nums2.length];
26+
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
1. 아이디어 : 최대 10억의 숫자를 각 자릿수 순서대로 배열 저장, 각 원소를 분리한뒤, 자릿수(2,3,4..)에 맞게 곱한 뒤 배열로 저장하면 된다.
4+
5+
2. 시간복잡도 : O(9+9) => O(1) (최대 수의 길이)
6+
7+
3. 자료구조/알고리즘 : 구현
8+
9+
*/
10+
11+
class Solution {
12+
public int[] decimalRepresentation(int n) {
13+
StringBuilder sb = new StringBuilder();
14+
15+
while(n>0) {
16+
sb.append(n%10);
17+
n/=10;
18+
}
19+
20+
int[] ans = new int[sb.length()];
21+
int multi = (int)Math.pow(10,sb.length()-1);
22+
int idx = 0;
23+
int cnt = sb.length();
24+
25+
for(int i=ans.length-1; i>=0; i--) {
26+
if(sb.charAt(i)-'0' == 0) cnt--;
27+
else ans[idx++] = (sb.charAt(i)-'0')*multi;
28+
multi/=10;
29+
}
30+
31+
return Arrays.copyOf(ans, cnt);
32+
}
33+
}

0 commit comments

Comments
 (0)