Skip to content

Commit ec1875b

Browse files
authored
Merge pull request #425 from JEONGHWANMIN/main
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week4 ๋ฌธ์ œํ’€์ด
2 parents 5120c30 + 012e8d0 commit ec1875b

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
if (nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let maxSequenceLength = 0;
10+
11+
for (const num of numSet) {
12+
if (!numSet.has(num - 1)) {
13+
let currentNum = num;
14+
let currentLength = 1;
15+
16+
while (numSet.has(currentNum + 1)) {
17+
currentNum++;
18+
currentLength++;
19+
}
20+
21+
maxSequenceLength = Math.max(maxSequenceLength, currentLength);
22+
}
23+
}
24+
25+
return maxSequenceLength;
26+
};
27+
28+
console.log(longestConsecutive([100, 4, 200, 1, 3, 2]));
29+
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]));

โ€Žmissing-number/hwanmini.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n log n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var missingNumber = function(nums) {
9+
nums.sort((a,b) => a - b);
10+
11+
for (let i = 0 ; i <= nums.length; i++) {
12+
if (i !== nums[i]) return i
13+
}
14+
15+
};
16+
17+
console.log(missingNumber([3, 0, 1]))
18+
console.log(missingNumber([0, 1]))
19+
console.log(missingNumber([9,6,4,2,3,5,7,0,1]))

โ€Žvalid-palindrome/hwanmini.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @return {boolean}
7+
*/
8+
var isPalindrome = function(s) {
9+
const strs = s.replace(/[^a-z0-9]/gi, '').toLowerCase();
10+
11+
let leftIdx = 0;
12+
let rightIdx = strs.length - 1
13+
14+
while (leftIdx <= rightIdx) {
15+
if (strs[leftIdx] !== strs[rightIdx]) return false
16+
17+
leftIdx++
18+
rightIdx--
19+
}
20+
21+
22+
return true
23+
};
24+
25+
const s = "A man, a plan, a canal: Panama"
26+
27+
28+
console.log(isPalindrome(s))
29+

โ€Žword-search/hwanmini.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
// m์€ board์˜ ํ–‰ ์ˆ˜, n์€ board์˜ ์—ด ์ˆ˜, 4(์ƒํ•˜์ขŒ์šฐ), l(word)
3+
4+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(m * n * 4L)
5+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(m * n)
6+
7+
const d_row = [1, -1, 0, 0]
8+
const d_col = [0, 0, -1, 1]
9+
10+
11+
const isMoveBoard = (new_row, new_col, board) => {
12+
return new_row >= 0 && new_row < board.length && new_col >= 0 && new_col < board[0].length
13+
}
14+
15+
16+
/**
17+
* @param {character[][]} board
18+
* @param {string} word
19+
* @return {boolean}
20+
*/
21+
var exist = function(board, word) {
22+
let result = false
23+
24+
const visited = Array.from({length: board.length} , () => Array.from({length: board[0].length}).fill(false))
25+
26+
const dfs = (strs, row, col, count) => {
27+
28+
if (strs[count] !== word[count]) return
29+
if (strs.length > word.length) return
30+
31+
if (strs === word) {
32+
result = true
33+
return
34+
}
35+
36+
for (let i = 0 ; i < d_row.length; i++) {
37+
const new_row = row + d_row[i]
38+
const new_col = col + d_col[i]
39+
40+
if (isMoveBoard(new_row, new_col, board) && visited[new_row][new_col] !== true){
41+
visited[new_row][new_col] = true
42+
dfs(strs+board[new_row][new_col], new_row, new_col, count+1)
43+
visited[new_row][new_col] = false
44+
}
45+
}
46+
47+
48+
49+
}
50+
51+
52+
for (let row = 0 ; row < board.length ; row++) {
53+
for (let col = 0 ; col < board[0].length ; col++) {
54+
visited[row][col] = true
55+
dfs(board[row][col], row, col , 0)
56+
visited[row][col] = false
57+
}
58+
}
59+
60+
61+
return result
62+
};
63+
64+
console.log(exist([["A","B","C","E"],
65+
["S","F","C","S"],
66+
["A","D","E","E"]], "ABCCED"))
67+
console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE"))
68+
console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB"))
69+

0 commit comments

Comments
ย (0)