File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/missing-number/
3+ *
4+ * ๋ฌธ์ ์ค๋ช
:
5+ * - 0๋ถํฐ n๊น์ง์ ์ซ์ ์ค ํ๋๊ฐ ๋น ์ง ๊ธธ์ด n์ ๋ฐฐ์ด nums๊ฐ ์ฃผ์ด์ง๋๋ค.
6+ * - ์ด ๋, ๋๋ฝ๋ ์ซ์ ํ๋๋ฅผ ์ฐพ์ ๋ฐํํ์ธ์.
7+ *
8+ * ์กฐ๊ฑด:
9+ * - nums.length == n
10+ * - nums์ ์์๋ ๊ณ ์ ํ๋ฉฐ [0, n] ๋ฒ์์ ์ ์๋ฅผ ํฌํจํฉ๋๋ค.
11+ *
12+ * ํ์ด ์์ด๋์ด:
13+ * - 0๋ถํฐ n๊น์ง์ ์ดํฉ์ ๊ณต์์ผ๋ก ๊ณ์ฐํ ํ, ์ค์ ๋ฐฐ์ด์ ์ดํฉ์ ๋บ๋๋ค.
14+ * 0๋ถํฐ n๊น์ง์ ํฉ ๊ณต์:
15+ * 0 + 1 + 2 + ... + n = n(n + 1)/2
16+ * - ๋น ์ง ์ซ์ = ๊ธฐ๋ ์ดํฉ - ์ค์ ์ดํฉ
17+ *
18+ * TC: O(n)
19+ * - ๋ฐฐ์ด ์ํ๋ก ์ค์ ํฉ์ ๊ตฌํ๋ ๋ฐ O(n)
20+ *
21+ * SC: O(1)
22+ * - ์ถ๊ฐ ๊ณต๊ฐ ์์ด ์์ ๋ณ์๋ง ์ฌ์ฉ
23+ */
24+
25+ function missingNumber ( nums : number [ ] ) : number {
26+ const expectedSum = Math . floor ( ( nums . length * ( nums . length + 1 ) ) / 2 ) ;
27+ const actualSum = nums . reduce ( ( acc , cur ) => acc + cur ) ;
28+ return expectedSum - actualSum ;
29+ }
You canโt perform that action at this time.
0 commit comments