From 2d5cba6bb75fe7ecf4c9bd0d18b8364479cf8df6 Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Sat, 4 Jan 2025 20:08:04 +0900 Subject: [PATCH] add: solution for week 4 --- merge-two-sorted-lists/higeuni.js | 34 ++++++++++++++++++++++ missing-number/higeuni.js | 15 ++++++++++ word-search/higeuni.js | 47 +++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 merge-two-sorted-lists/higeuni.js create mode 100644 missing-number/higeuni.js create mode 100644 word-search/higeuni.js diff --git a/merge-two-sorted-lists/higeuni.js b/merge-two-sorted-lists/higeuni.js new file mode 100644 index 000000000..545a240f6 --- /dev/null +++ b/merge-two-sorted-lists/higeuni.js @@ -0,0 +1,34 @@ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + * + * complexity + * time: O(n + m) + * space: O(1) + */ + +var mergeTwoLists = function(list1, list2) { + let dummy = new ListNode(); + let current = dummy; + + while (list1 !== null && list2 !== null) { + if (list1.val < list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + + if (list1 !== null) { + current.next = list1; + } else if (list2 !== null) { + current.next = list2; + } + + return dummy.next; +}; + diff --git a/missing-number/higeuni.js b/missing-number/higeuni.js new file mode 100644 index 000000000..2a25ac51a --- /dev/null +++ b/missing-number/higeuni.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + * + * complexity + * time: O(n) + * space: O(1) + */ + +var missingNumber = function(nums) { + const sumOfNums = nums.reduce((acc, curr) => acc + curr, 0); + const sumOfTotalNumbers = (nums.length * (nums.length + 1)) / 2; + return sumOfTotalNumbers - sumOfNums; +}; + diff --git a/word-search/higeuni.js b/word-search/higeuni.js new file mode 100644 index 000000000..4e6fe19a2 --- /dev/null +++ b/word-search/higeuni.js @@ -0,0 +1,47 @@ +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + * + * complexity + * time: O(n * m * 4^l) + * space: O(n * m) + */ + +var exist = function(board, word) { + word = word.split(''); + const dx = [-1, 1, 0, 0]; + const dy = [0, 0, 1, -1]; + const visited = Array.from({length: board.length}, () => Array(board[0].length).fill(false)); + + const dfs = (length, x, y) => { + if(word.length === length) { + return true; + } + for(let i = 0; i < 4; ++i) { + const nx = x + dx[i]; + const ny = y + dy[i]; + if(0 <= nx && nx < board[0].length && 0 <= ny && ny < board.length){ + if(board[ny][nx] === word[length] && !visited[ny][nx]) { + visited[ny][nx] = true; + if(dfs(length + 1, nx, ny)) return true; + visited[ny][nx] = false; + } + } + } + return false; + } + + for(let i = 0; i < board.length; ++i){ + for(let j = 0; j < board[0].length; ++j){ + if(board[i][j] === word[0]) { + visited[i][j] = true; + if(dfs(1, j, i)) return true; + visited[i][j] = false; + } + } + } + + return false; +}; +