Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions two-sum/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 배열 두 수의 합이 target 과 같은 값
* 알고리즘 복잡도:
* - 시간복잡도: O(n^2)
* - 공간복잡도: O(1)
* @param nums
* @param target
*/
function twoSum(nums: number[], target: number): number[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 경우의 수를 확인하는 방법으로 푸셨군요 :) 복잡도를 더 낮출 수 있는 방법이 없을 지 생각해보는건 어떨까요?

let result: number[] = [];

for(let i = 0; i < nums.length; i++) {
for(let j = i + 1; j < nums.length; j++) {
if(nums[i] + nums[j] === target) {
result.push(i, j);
return result;
}
}
}
}
Loading