Skip to content

Commit 7869476

Browse files
authored
Merge pull request #2287 from smosco/main
[smosco] WEEK 11 solutions
2 parents 1ae487f + 125dcfd commit 7869476

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

โ€Žmissing-number/smosco.jsโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Missing Number
3+
* 0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋น ์ง„ ๋ฐฐ์—ด์—์„œ ๋ˆ„๋ฝ๋œ ์ˆซ์ž ์ฐพ๊ธฐ
4+
*/
5+
6+
// ํ’€์ด 1: ํ•ฉ ๊ณต์‹ ์ด์šฉ
7+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
8+
// 0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์—์„œ ๋ฐฐ์—ด ์›์†Œ๋“ค์˜ ํ•ฉ์„ ๋นผ๋ฉด ๋น ์ง„ ์ˆซ์ž๊ฐ€ ๋‚˜์˜จ๋‹ค
9+
const missingNumber = (nums) => {
10+
const n = nums.length;
11+
const expectedSum = (n * (n + 1)) / 2; // ๋“ฑ์ฐจ์ˆ˜์—ด ํ•ฉ ๊ณต์‹
12+
const actualSum = nums.reduce((sum, num) => sum + num, 0);
13+
return expectedSum - actualSum;
14+
};
15+
16+
// ํ’€์ด 2: Set ์‚ฌ์šฉ
17+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
18+
// Set์— ๋„ฃ๊ณ  0๋ถ€ํ„ฐ n๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ์—†๋Š” ์ˆซ์ž ์ฐพ๊ธฐ
19+
const missingNumberSet = (nums) => {
20+
const numSet = new Set(nums);
21+
22+
for (let i = 0; i <= nums.length; i++) {
23+
if (!numSet.has(i)) {
24+
return i;
25+
}
26+
}
27+
};

0 commit comments

Comments
ย (0)