Skip to content

Commit df48a41

Browse files
committed
add: solve DaleStudy#272 with ts
1 parent 107cd10 commit df48a41

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ณ„์‚ฐ
4+
* @param {number[]} nums
5+
* @returns {number} - ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด
6+
*
7+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O( n log(n) )
8+
* - n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด
9+
* - ๊ฐ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— log(n)์ด ์†Œ์š”๋จ
10+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
11+
* - ๋ฐฐ์—ด์— ์ตœ๋Œ€ n๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅ
12+
*/
13+
function lengthOfLIS(nums: number[]): number {
14+
// ์ˆ˜์—ด์„ ์ €์žฅํ•  ๋ฐฐ์—ด
15+
const sequnces: number[] = [];
16+
17+
for (let num of nums) {
18+
// ์ด๋ถ„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ num์ด ๋“ค์–ด๊ฐˆ ์œ„์น˜๋ฆ„ ์ฐพ์Œ
19+
let left = 0;
20+
let right = sequnces.length;
21+
22+
while (left < right) {
23+
const mid = Math.floor((left + right) / 2);
24+
if (sequnces[mid] < num) {
25+
left = mid + 1;
26+
} else {
27+
right = mid;
28+
}
29+
}
30+
31+
// ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ๋Œ€์ฒด
32+
if (left < sequnces.length) {
33+
sequnces[left] = num;
34+
} else {
35+
sequnces.push(num)
36+
}
37+
}
38+
39+
return sequnces.length;
40+
}
41+

0 commit comments

Comments
ย (0)