Skip to content

Commit 369a555

Browse files
committed
sadie100: 2 solutions
1 parent 3e32801 commit 369a555

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

โ€Ž3sum/sadie100.tsโ€Ž

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ๋’ค ์—˜๋ฆฌ๋จผํŠธ ํ•˜๋‚˜๋ฅผ ๊ณ ์ •ํ•˜๊ณ , ํ•ด๋‹น ์—˜๋ฆฌ๋จผํŠธ๋ณด๋‹ค ํฐ ๋ฒ”์œ„์—์„œ ํˆฌ ํฌ์ธํ„ฐ ์ˆœํšŒ๋กœ ํ•ฉ์ด -{์—˜๋ฆฌ๋จผํŠธ๊ฐ’} ์ด ๋˜๋Š” ์Œ์„ ์ฐพ๋Š” ๋กœ์ง์„ ๋ชจ๋“  ๋ฐฐ์—ด ์—˜๋ฆฌ๋จผํŠธ์— ์ˆœํšŒํ•˜๋ฉฐ ์ ์šฉํ•œ๋‹ค
3+
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N^2)
5+
=> ์—˜๋ฆฌ๋จผํŠธ ๊ณ ์ • ์ˆœํšŒ n * ํˆฌ ํฌ์ธํ„ฐ ์ˆœํšŒ n
6+
7+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N)
8+
=> ์ •๋ ฌ๋œ ๋ฐฐ์—ด
9+
*/
10+
11+
function threeSum(nums: number[]): number[][] {
12+
const sortedNums = nums.sort((a, b) => a - b)
13+
const result = []
14+
let beforeIVal
15+
16+
for (let i = 0; i < nums.length; i++) {
17+
const idxVal = sortedNums[i]
18+
if (beforeIVal !== undefined && beforeIVal === idxVal) {
19+
continue
20+
}
21+
beforeIVal = idxVal
22+
const shouldBe = -idxVal
23+
let start = i + 1
24+
let end = nums.length - 1
25+
26+
while (start < end) {
27+
const startVal = sortedNums[start]
28+
const endVal = sortedNums[end]
29+
const value = startVal + endVal
30+
if (value === shouldBe) {
31+
result.push([idxVal, startVal, endVal])
32+
start += 1
33+
end -= 1
34+
while (startVal === sortedNums[start]) {
35+
start += 1
36+
}
37+
while (endVal === sortedNums[end]) {
38+
end -= 1
39+
}
40+
} else if (value > shouldBe) {
41+
end -= 1
42+
} else {
43+
start += 1
44+
}
45+
}
46+
}
47+
48+
return result
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
/*
16+
ํ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฃจํŠธ์—์„œ ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด์„œ ๋…ธ๋“œ, max๊ฐ’(left๋กœ ๋น ์งˆ ๋•Œ ํ˜„์žฌ ๋…ธ๋“œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ), min๊ฐ’(right๋กœ ๋น ์งˆ ๋•Œ ํ˜„์žฌ ๋…ธ๋“œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ )์„ ์ „๋‹ฌํ•˜๊ณ ,
17+
ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉด์„œ min max ํ•œ๋„๋ฅผ ๊ฒ€์‚ฌ, ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
18+
19+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N) (N์€ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜)
20+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N) (queue ์‚ฌ์šฉ)
21+
*/
22+
23+
function isValidBST(root: TreeNode | null): boolean {
24+
if (root === null) return true
25+
const queue: [TreeNode, number | null, number | null][] = [[root, null, null]]
26+
let idx = 0
27+
28+
while (idx < queue.length) {
29+
const [node, min, max] = queue[idx]
30+
31+
if (min !== null && node.val <= min) return false
32+
if (max !== null && node.val >= max) return false
33+
34+
if (node.left) {
35+
queue.push([node.left, min, node.val])
36+
}
37+
if (node.right) {
38+
queue.push([node.right, node.val, max])
39+
}
40+
idx += 1
41+
}
42+
43+
return true
44+
}

0 commit comments

Comments
ย (0)