diff --git a/README.md b/README.md index 0a3d627..3d86b96 100644 --- a/README.md +++ b/README.md @@ -604,3 +604,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*** +# **complemento** +- parei no array + diff --git a/src/array/beautiful-arrangement-ii.js b/src/array/beautiful-arrangement-ii.js index 52706d1..a197189 100644 --- a/src/array/beautiful-arrangement-ii.js +++ b/src/array/beautiful-arrangement-ii.js @@ -28,24 +28,17 @@ * The n and k are in the range 1 <= k < n <= 10^4. */ -/** - * @param {number} n - * @param {number} k - * @return {number[]} - */ -const constructArray = (n, k) => { - const ans = Array(n); - - let c = 0; +console.clear() +let constructArr = (n, k) => { + let ans = Array(n) + let c = 0 for (let v = 1; v < n - k; v++) { - ans[c++] = v; + ans[c + 1] = v } - for (let i = 0; i <= k; i++) { ans[c++] = i % 2 === 0 ? n - k + Math.floor(i / 2) : n - Math.floor(i / 2); } return ans; -}; - -export { constructArray }; +} +console.log(constructArr(3, 2)) \ No newline at end of file diff --git a/src/array/can-place-flowers.js b/src/array/can-place-flowers.js index d4b5bfa..198c4fd 100644 --- a/src/array/can-place-flowers.js +++ b/src/array/can-place-flowers.js @@ -21,29 +21,19 @@ * - n is a non-negative integer which won't exceed the input array size. */ -/** - * @param {number[]} flowerbed - * @param {number} n - * @return {boolean} - */ -const canPlaceFlowers = (flowerbed, n) => { - let count = 0; - - for (let i = 0; i < flowerbed.length && count < n; i++) { - if (flowerbed[i] === 0) { - // Get next and prev flower bed slot values. - // If i lies at the ends the next and prev are considered as 0. - const next = i === flowerbed.length - 1 ? 0 : flowerbed[i + 1]; - const prev = i === 0 ? 0 : flowerbed[i - 1]; - - if (next === 0 && prev === 0) { - flowerbed[i] = 1; - count++; +console.clear() +let can_place_flowers = (flowes, n) => { + let count = 0 + for (let i = 0; i < flowes.length - 1 && count < n; i++) { + if (flowes[i] == 0) { + let next = i == flowes.length - 1 ? 0 : flowes[i + 1] + let prev = i == 0 ? 0 : flowes[i - 1] + if (next == 0 && prev == 0) { + flowes[i] = 1 + count++ } } } - - return count === n; -}; - -export { canPlaceFlowers }; + return count == n +} +console.log(can_place_flowers([1, 0, 0, 0, 1, 1], 2)) \ No newline at end of file diff --git a/src/array/candy-crush.js b/src/array/candy-crush.js index ae5c390..626dcb4 100644 --- a/src/array/candy-crush.js +++ b/src/array/candy-crush.js @@ -26,11 +26,6 @@ * The length of board[i] will be in the range [3, 50]. * Each board[i][j] will initially start as an integer in the range [1, 2000]. */ - -/** - * @param {number[][]} board - * @return {number[][]} - */ const candyCrush = board => { const m = board.length; const n = board[0].length; @@ -74,5 +69,13 @@ const candyCrush = board => { return shouldContinue ? candyCrush(board) : board; }; +let board = [ + [1, 1, 1, 2, 2], + [5, 1, 3, 2, 2], + [1, 2, 2, 3, 3], + [4, 4, 4, 1, 1] +]; + +let candy = candyCrush(board); +console.log(candy); -export { candyCrush }; diff --git a/src/array/container-with-most-water.js b/src/array/container-with-most-water.js index feed533..d76a3fd 100644 --- a/src/array/container-with-most-water.js +++ b/src/array/container-with-most-water.js @@ -48,6 +48,7 @@ * 6 x x x x x x */ + /** * @param {number[]} height * @return {number} @@ -56,7 +57,7 @@ const maxArea = height => { const n = height.length; let max = 0; - for (let i = 0, j = n - 1; i < j; ) { + for (let i = 0, j = n - 1; i < j;) { const area = Math.min(height[i], height[j]) * (j - i); max = Math.max(max, area); @@ -70,4 +71,4 @@ const maxArea = height => { return max; }; -export { maxArea }; +export { maxArea }; \ No newline at end of file diff --git a/src/linked-list/add-two-numbers-ii.js b/src/linked-list/add-two-numbers-ii.js index c8f2b1d..7e98d62 100644 --- a/src/linked-list/add-two-numbers-ii.js +++ b/src/linked-list/add-two-numbers-ii.js @@ -23,43 +23,109 @@ * } */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = (l1, l2) => { - // Let's use two stacks to help - const s1 = []; - const s2 = []; - - while (l1) { - s1.push(l1.val); - l1 = l1.next; +class Node { + constructor(value) { + this.value = value + this.next = null + this.tail = null } - - while (l2) { - s2.push(l2.val); - l2 = l2.next; +} +class LinkedList { + constructor() { + this.head = null } - - // Perform the addition - let carry = 0; - let list = null; - while (s1.length > 0 || s2.length > 0 || carry > 0) { - const v1 = s1.length > 0 ? s1.pop() : 0; - const v2 = s2.length > 0 ? s2.pop() : 0; - const node = new ListNode((v1 + v2 + carry) % 10); - - carry = Math.floor((v1 + v2 + carry) / 10); - - if (list) { - node.next = list; + append(value) { + let new_node = new Node(value) + if (this.isEmphy()) { + this.head = new_node + return + } else { + let temp = this.head + while (temp.next) { + temp = temp.next + } + temp.next = new_node + return + } + } + prepend(value) { + let new_node = new Node(value) + if (this.isEmphy()) { + this.head = new_node + return + } else { + new_node.next = this.head + this.head = new_node + return } - list = node; } + isEmphy() { + return this.head == null + } + twoNodes(node1, node2) { + let temp_node1 = node1.head + let temp_node2 = node2.head + let node1_res = '' + let node2_res = '' + while (temp_node1 || temp_node2) { + if (temp_node1) { + node1_res += String(temp_node1.value) + temp_node1 = temp_node1.next + } + if (temp_node2) { + node2_res += String(temp_node2.value) + temp_node2 = temp_node2.next + } + } + let resposta = Number(Number(node1_res) + Number(node2_res)) + return console.log(resposta) + } + removeFromFront() { + if (this.isEmphy()) { + return null + } else { + this.head = this.head.next + return + } + } + // removeFromEnd() { + // if (this.isEmphy()) { + // return null + // } else { + // let temp = this.head + // while (true) { + // if (temp.next.next == null) { + // temp.next = null + // return + // } + // } + // } + // } + print() { + let temp = this.head + let saida = '' + while (temp) { + saida += String(temp.value) + temp = temp.next + } + return console.log(saida) + } +} +console.clear() +let node1 = new LinkedList() +let node2 = new LinkedList() +let collection = [] + +while (collection.length < 3) { + collection.push(Math.floor(Math.random() * 10)) + node1.append(Math.floor(Math.random() * 10)) + node2.append(Math.floor(Math.random() * 10)) - return list; -}; +} +node1.print() +node2.print() +let conj = new LinkedList() +conj.twoNodes(node1, node2) +// node1.removeFromEnd() +node1.print() -export { addTwoNumbers }; diff --git a/src/linked-list/add-two-numbers.js b/src/linked-list/add-two-numbers.js index 88ef9a3..2a2974b 100644 --- a/src/linked-list/add-two-numbers.js +++ b/src/linked-list/add-two-numbers.js @@ -17,34 +17,9 @@ * } */ -import ListNode from 'common/list-node'; -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ const addTwoNumbers = (l1, l2) => { - const f = new ListNode(0); - let p = f; - - let c = 0; - - while (l1 || l2 || c > 0) { - const a = l1 ? l1.val : 0; - const b = l2 ? l2.val : 0; - const s = a + b + c; - c = parseInt(s / 10, 10); - - p.next = new ListNode(s % 10); - p = p.next; - - l1 = l1 ? l1.next : null; - l2 = l2 ? l2.next : null; - } - - return f.next; }; -export default addTwoNumbers; + diff --git a/src/linked-list/convert-binary-search-tree-to-sorted-doubly-linked-list.js b/src/linked-list/convert-binary-search-tree-to-sorted-doubly-linked-list.js index 25bbef0..c83f24a 100644 --- a/src/linked-list/convert-binary-search-tree-to-sorted-doubly-linked-list.js +++ b/src/linked-list/convert-binary-search-tree-to-sorted-doubly-linked-list.js @@ -11,44 +11,185 @@ // Definition for a Node. class Node { - constructor(_val, _left, _right) { - this.val = _val; - this.left = _left; - this.right = _right; + constructor(prev, value) { + this.value = value + this.prev = prev + this.next = null + } } +class DoubleLinkedList { + constructor() { + this.head = null + this.size = 0 + this.tail = null + } + append(value) { + let new_node = new Node() + if (this.ismphy()) { + new_node.value = value + new_node.prev = null + this.head = new_node + this.tail = new_node + this.size++ + } else { + let temp = this.head + while (temp.next) { + temp = temp.next + } + new_node.value = value + new_node.prev = this.tail + temp.next = new_node + this.tail = new_node + this.size++ -const treeToDoublyList = root => { - const inorder = root => { - if (!root) { - return; } + } + prepend(value) { + if (this.ismphy()) { + return null + } else { + let new_node = new Node(null, value) + new_node.next = this.head - inorder(roo.left); - - prev.right = root; - root.left = prev; - prev = root; + new_node.next.prev = new_node + this.head = new_node - inorder(root.right); - }; + this.size++ + } + } + removeFronFront() { + if (this.ismphy()) { + return null + } else { + this.head = this.head.next + this.size-- + } + } + removeFromEnd() { + if (this.ismphy()) { + return null + } else { + let temp = this.head + while (temp.next.next) { + temp = temp.next + } + this.tail = this.tail.prev + temp.next = null + this.size-- + } + } + removeFromAt(index) { + if (this.ismphy()) { + return null + } else if (index > this.size) { + return null + } else { + if (index == 1) { + return this.removeFronFront() + } else if (index == this.size) { + return this.removeFromEnd() + } else { + let temp = this.head + for (let i = 1; i < index - 1; i++) { + temp = temp.next + } + temp.next.next.prev = temp + temp.next = temp.next.next + this.size-- + } + } - if (!root) { - return null; } + print() { + if (this.ismphy()) { + return null + } else { + let temp = this.head + while (temp) { + console.log(temp.value) + temp = temp.next + } + } - // Step 1. Initialize - const dummy = new Node(0, null, null); - let prev = dummy; + } - // Step 2. Inorder traverse the BST - inorder(root); + ismphy() { + return this.head == null + } +} +console.clear() +let doblelist = new DoubleLinkedList() +doblelist.append(1) +doblelist.append(2) +doblelist.append(3) +doblelist.append(5) +doblelist.append(4) +doblelist.prepend(4) +doblelist.prepend(6) +doblelist.prepend(10) +doblelist.removeFromAt(1) +doblelist.removeFromAt(1) +doblelist.removeFromAt(1) +doblelist.print() - // Step 3. Connect head and tail - prev.right = dummy.right; - dummy.right.left = prev; +class NodeTree { + constructor(value) { + this.value = value + this.left = null + this.right = null + } +} +class BinarySearchTree { + constructor() { + this.root = null + } + ismphy() { + return this.root == null + } + insert(value) { + let newNode = new NodeTree(value) + if (this.ismphy()) { + this.root = newNode + } else { + this.insertNode(this.root, newNode) + } + } + insertNode(root, newNode) { + if (newNode.value < root.value) { + if (root.left == null) { + root.left = newNode + } else { + this.insertNode(root.left, newNode) + } + } else { + if (root.right == null) { + root.right = newNode - return dummy.right; -}; + } else { + this.insertNode(root.right, newNode) + } + } + } + search(root, target) { + if (!root) { + return false + } else { + if (root.value == target) { + return true + } else { + if (root.value > target) { + return this.search(root.left, target) + } else { + return this.search(root.right, target) + } + } + } -export { treeToDoublyList }; + } +} +let bst = new BinarySearchTree() +bst.insert(10) +bst.insert(23) +bst.insert(12) +console.log(bst.search(bst.root, 100)) diff --git a/src/linked-list/convert-sorted-list-to-binary-search-tree.js b/src/linked-list/convert-sorted-list-to-binary-search-tree.js index 78ed403..26cd86b 100644 --- a/src/linked-list/convert-sorted-list-to-binary-search-tree.js +++ b/src/linked-list/convert-sorted-list-to-binary-search-tree.js @@ -35,44 +35,3 @@ * } */ -/** - * @param {ListNode} head - * @return {TreeNode} - */ -const sortedListToBST = head => { - if (!head) { - return null; - } - - if (!head.next) { - return new TreeNode(head.val); - } - - // Find the previous node of middle node - const node = findMiddle(head); - const middle = node.next; - node.next = null; - - const root = new TreeNode(middle.val); - - root.left = sortedListToBST(head); - root.right = sortedListToBST(middle.next); - - return root; -}; - -const findMiddle = head => { - let prev = null; - let slow = head; - let fast = head; - - while (fast && fast.next) { - prev = slow; - slow = slow.next; - fast = fast.next.next; - } - - return prev; -}; - -export { sortedListToBST }; diff --git a/src/linked-list/copy-list-with-random-pointer.js b/src/linked-list/copy-list-with-random-pointer.js index 7d6937d..17ede7f 100644 --- a/src/linked-list/copy-list-with-random-pointer.js +++ b/src/linked-list/copy-list-with-random-pointer.js @@ -15,33 +15,182 @@ * } */ -/** - * @param {RandomListNode} head - * @return {RandomListNode} - */ -const copyRandomList = head => { - if (!head) { - return null; - } - - const map = new Map(); +console.clear() +class Node { + constructor(value) { + this.value = value + this.next = null + } - // Step 1. Copy all the nodes - let p = head; - while (p) { - map.set(p, new RandomListNode(p.label)); - p = p.next; - } +} +class LinkedList { + constructor() { + this.head = null + this.tail = null + this.size = 0 + } + isemphy() { + return this.head == null + } + append(value) { + let new_node = new Node(value) + if (this.isemphy()) { + this.head = new_node + this.tail = new_node + this.size++ + return + } else { + let temp = this.head + while (temp.next) { + temp = temp.next + }; + temp.next = new_node + this.tail = new_node + this.size++ + return + } + } - // Step 2. Copy the next and random pointers - p = head; - while (p) { - if (p.next) map.get(p).next = map.get(p.next); - if (p.random) map.get(p).random = map.get(p.random); - p = p.next; - } + prepend(value) { + let new_node = new Node(value) + if (this.isemphy()) { + this.head = new_node + this.size++ + this.tail = new_node + return + } else { + new_node.next = this.head + this.head = new_node + this.size++ + return + } + } + removeFromFront() { + if (this.isemphy()) { + return null + } else { + this.head = this.head.next + this.size-- + } + } + removeFromEnd() { + if (this.isemphy()) { + return null + } else { + let temp = this.head + while (temp.next) { + temp = temp.next + } + temp.next = null + this.tail = temp + this.head = temp + this.size-- + } + } + print() { + if (this.isemphy()) { + return null + } else { + let sequence = '' + let temp = this.head + while (temp) { + sequence += String(temp.value) + temp = temp.next + } + return console.log(sequence) + } + } + search(value) { + if (this.isemphy()) { + return null + } else { + let temp = this.head + let index = 0 + while (temp) { + if (temp.value == value) { + return index + } + temp = temp.next + index += 1 + } + return -1 + } + } + copyList(curr) { + if (this.isemphy()) return null; + if (curr > this.size) return undefined; + let index = 0 + let temp = this.head + while (index < curr) temp = temp.next; + let sequencia = '' + while (temp) { + sequencia += String(temp.value) + temp = temp.next + } + return sequencia + } +} +class Stack { + constructor(capacity) { + this.stack = new LinkedList() + this.capacity = capacity + } + isfull() { + return stack.stack.size >= this.capacity + } + push(value) { + if (this.isfull()) { + return undefined + } else { + return this.stack.append(value) + } + } + pop() { + return this.stack.removeFromEnd() + } + isemphy() { + return this.stack.isEmphy() + } + top() { + return this.stack.tail + } + clear() { + return this.stack.head = null + } - return map.get(head); -}; + print() { + return this.stack.print() + } -export { copyRandomList }; +} +class Queue { + constructor() { + this.queue = new LinkedList() + } + enqueue(value) { + return this.queue.append(value) + } + dequeue() { + return this.queue.removeFromFront() + } + peek() { + return this.queue.head.value + } + isemphy() { + return this.queue.isEmphy() + } + print() { + return this.queue.print() + } +} +let queue = new Queue() +queue.enqueue(1) +queue.enqueue(2) +queue.enqueue(3) +queue.print() +let stack = new Stack(4) +stack.push(1) +stack.push(2) +stack.push(3) +stack.pop() +stack.print() diff --git a/src/linked-list/insert-into-a-cyclic-sorted-list.js b/src/linked-list/insert-into-a-cyclic-sorted-list.js index dac80e1..ab32439 100644 --- a/src/linked-list/insert-into-a-cyclic-sorted-list.js +++ b/src/linked-list/insert-into-a-cyclic-sorted-list.js @@ -20,60 +20,69 @@ * } */ -import ListNode from 'common/list-node'; +console.clear() -/** - * @param {ListNode} start - * @param {number} x - */ -const insert = (start, x) => { - // if start is null, create a node pointing to itself and return - if (start === null) { - const node = new ListNode(x, null); - node.next = node; - return node; - } - - // is start is NOT null, try to insert it into correct position - let cur = start; - while (true) { - // case 1A: has a tipping point, still climbing - if (cur.val < cur.next.val) { - if (cur.val <= x && x <= cur.next.val) { - // x in between cur and next - insertAfter(cur, x); - break; - } - // case 1B: has a tipping point, about to return back to min node - } else if (cur.val > cur.next.val) { - if (cur.val <= x || x <= cur.next.val) { - // cur is the tipping point, x is max or min val - insertAfter(cur, x); - break; - } - // case 2: NO tipping point, all flat - } else { - if (cur.next === start) { - // insert x before we traverse all nodes back to start - insertAfter(cur, x); - break; - } +class Node { + constructor(value) { + this.next = null + this.value = value + } +} +class LinkedList { + constructor() { + this.list = null + this.tail = null + } + isemphy() { + return this.list == null + } + insertEnd(value) { + if (this.isemphy()) { + this.list = new Node(value) + this.tail = this.list + this.size = 0 + } else { + let node = new Node(value) + let temp = this.list + this.size += 1 + while (temp.next) { + temp = temp.next + } + temp.next = node + this.tail = node + this.size += 1 + } + } + remove() { + if (this.isemphy()) { + return null + } else { + let temp = this.list + while (temp.next.next) { + temp = temp.next + } + temp.next = null + this.tail = temp + this.size = -1 + } + } + search(value) { + if (this.isemphy()) { + return null + } else { + let temp = this.list + while (temp) { + if (temp.value == value) { + return true + } + temp = temp.next + } + return false + } + } + peek() { + return this.list.value } - // None of the above three cases met, go to next node - cur = cur.next; - } - - return start; -}; - -/** - * Insert value x after Node node - * @param {ListNode} node - * @param {number} x - */ -const insertAfter = (node, x) => { - node.next = new ListNode(x, node.next); -}; +} -export { insert }; diff --git a/src/linked-list/insertion-sort-list.js b/src/linked-list/insertion-sort-list.js index 6787227..054b68b 100644 --- a/src/linked-list/insertion-sort-list.js +++ b/src/linked-list/insertion-sort-list.js @@ -1,3 +1,4 @@ +console.clear() /** * Insertion Sort List * @@ -21,7 +22,6 @@ * Input: -1->5->3->4->0 * Output: -1->0->3->4->5 */ - /** * Definition for singly-linked list. * function ListNode(val) { @@ -29,33 +29,134 @@ * this.next = null; * } */ - -/** - * @param {ListNode} head - * @return {ListNode} - */ -const insertionSortList = head => { - let curr = head; - let next = null; - - // dummy is a fake head - const dummy = new ListNode(0); - - while (curr) { - next = curr.next; - - let p = dummy; - while (p.next && p.next.val < curr.val) { - p = p.next; +class Node { + constructor(value) { + this.data = value + this.next = null } +} +class LinkedList { + constructor() { + this.head = null + this.size = 0 + this.tail = null + } + isemphy() { + return this.head == null + } + prepend(value) { + let node = new Node(value) + if (this.isemphy()) { + this.head = node + this.tail = node + this.size++ + return + } else { + this.tail = node + node.next = this.head + this.head = node + this.size++ + return + } + } + append(value) { + let node = new Node(value) + if (this.isemphy()) { + this.head = node + this.tail = node + this.size++ + return + } else { + let current_node = this.head + while (current_node.next) { + current_node = current_node.next + } + current_node.next = node + this.tail = node + this.size++ + return + } + } + removeFromEnd() { + if (this.isemphy()) { + return console.log(null) + } else { + let current_node = this.head + while (current_node.next.value != this.tail.value) { + current_node = current_node.next + } + let new_tail = new Node(current_node.value) + this.tail = new_tail + current_node.next = null + this.size-- + return + } + } + removeFromFront() { + if (this.isemphy()) { + return console.log(null) + } else { + this.head = this.head.next + this.size-- + } + } + removeForPosition(position) { + if (this.isemphy() || position > this.size || position < 0) { + return console.log(false) + } else { + let current_node = this.head + let current_position = 0 + while (current_position < position - 1) { + current_node = current_node.next + current_position++ + } + let temp = current_node.next + current_node.next = temp.next + this.size-- + return + } + } + search(value) { + if (this.isemphy()) { + return console.log(null) + } else { + let current_node = this.head + while (current_node) { + if (current_node.data == value) { + return true + } + } + return false + } + } + insertion_sort() { + if(this.isemphy() || this.head.next == null){ + return + } + let sorted = null + let current = this.head + while(current != null){ + let nextNode = current.next + sorted = this.sortedInsert(sorted,current) + current = nextNode + } + } + sortedInsert(sorted,new_node){ - // insert curr between p and p.next - curr.next = p.next; - p.next = curr; - curr = next; - } - - return dummy.next; -}; - -export { insertionSortList }; + } + print() { + let root = this.head + let output = '' + while (root) { + output += ` ${root.data} ` + root = root.next + } + return console.log(output) + } +} +let list = new LinkedList() +list.append(3) +list.append(2) +list.append(1) +list.append(4) +list.print() diff --git a/src/matrix/brick-wall.js b/src/matrix/brick-wall.js index 497a252..11fbdff 100644 --- a/src/matrix/brick-wall.js +++ b/src/matrix/brick-wall.js @@ -26,23 +26,4 @@ * Output: 2 */ -/** - * @param {number[][]} wall - * @return {number} - */ -const leastBricks = wall => { - const map = {}; - let count = 0; - - for (let i = 0; i < wall.length; i++) { - for (let j = 0, sum = 0; j < wall[i].length - 1; j++) { - sum += wall[i][j]; - map[sum] = ~~map[sum] + 1; - count = Math.max(count, map[sum]); - } - } - - return wall.length - count; -}; -export { leastBricks };