Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c211cc9

Browse files
Merge pull request #109 from ssi02014/ssi02014
레벨1 새로운 문제 풀이 숫자 짝꿍
2 parents 634c749 + 5ac0dfb commit c211cc9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎level-1/숫자-짝꿍&131128&.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - ssi02014
4+
5+
/**
6+
* X, Y의 길이가 굉장히 길어서 공통 숫자를 뽑아낼 때 객체를 이용해 연산 횟수 최적화
7+
* X, Y를 배열로 변환 후에 배열 메서드를 사용해도 되지만, for of문보다 효율성 떨어짐
8+
* (테스트 케이스 11 ~ 15 100ms~200ms 차이)
9+
*/
10+
function solution(X, Y) {
11+
const commons = [];
12+
const obj = {};
13+
14+
for (const el of X) {
15+
obj[el] = (obj[el] || 0) + 1;
16+
}
17+
18+
for (const el of Y) {
19+
if (obj[el]) {
20+
commons.push(el);
21+
obj[el]--;
22+
}
23+
}
24+
25+
commons.sort((a, b) => b - a);
26+
27+
if (!commons.length) return "-1";
28+
else if (commons[0] === "0") return "0";
29+
return commons.join("");
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /