From 9495ca325f258f0d60ea65e2e94fcc4321e05360 Mon Sep 17 00:00:00 2001 From: cassiano Date: Mon, 11 Nov 2024 14:43:36 -0300 Subject: [PATCH 01/11] fiz exercicios com array --- README.md | 4 +++ src/array/beautiful-arrangement-ii.js | 21 ++++++---------- src/array/can-place-flowers.js | 36 ++++++++++----------------- src/array/candy-crush.js | 15 ++++++----- 4 files changed, 33 insertions(+), 43 deletions(-) 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 }; From 9014362ee05e42d8a9c5e6f4ae93750ad29faa26 Mon Sep 17 00:00:00 2001 From: cassiano Date: Mon, 11 Nov 2024 14:47:05 -0300 Subject: [PATCH 02/11] mudei branch --- src/array/container-with-most-water.js | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/array/container-with-most-water.js b/src/array/container-with-most-water.js index feed533..4355888 100644 --- a/src/array/container-with-most-water.js +++ b/src/array/container-with-most-water.js @@ -48,26 +48,3 @@ * 6 x x x x x x */ -/** - * @param {number[]} height - * @return {number} - */ -const maxArea = height => { - const n = height.length; - let max = 0; - - for (let i = 0, j = n - 1; i < j; ) { - const area = Math.min(height[i], height[j]) * (j - i); - max = Math.max(max, area); - - if (height[i] < height[j]) { - i++; - } else { - j--; - } - } - - return max; -}; - -export { maxArea }; From a112a1830291cb0d7a95b800ea16f51b7dd57d07 Mon Sep 17 00:00:00 2001 From: cassiano Date: Mon, 11 Nov 2024 14:49:05 -0300 Subject: [PATCH 03/11] m=comeinei fiz mudancao --- src/array/container-with-most-water.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/array/container-with-most-water.js b/src/array/container-with-most-water.js index 4355888..d76a3fd 100644 --- a/src/array/container-with-most-water.js +++ b/src/array/container-with-most-water.js @@ -48,3 +48,27 @@ * 6 x x x x x x */ + +/** + * @param {number[]} height + * @return {number} + */ +const maxArea = height => { + const n = height.length; + let max = 0; + + for (let i = 0, j = n - 1; i < j;) { + const area = Math.min(height[i], height[j]) * (j - i); + max = Math.max(max, area); + + if (height[i] < height[j]) { + i++; + } else { + j--; + } + } + + return max; +}; + +export { maxArea }; \ No newline at end of file From 4260e614aebfc401cc293e828002f3985cf39fe2 Mon Sep 17 00:00:00 2001 From: cassiano Date: Wed, 13 Nov 2024 12:23:11 -0300 Subject: [PATCH 04/11] fiz exercicios de linked lis --- src/linked-list/add-two-numbers-ii.js | 96 ++++++++++++++++++--------- src/linked-list/add-two-numbers.js | 27 +------- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/linked-list/add-two-numbers-ii.js b/src/linked-list/add-two-numbers-ii.js index c8f2b1d..23bcff8 100644 --- a/src/linked-list/add-two-numbers-ii.js +++ b/src/linked-list/add-two-numbers-ii.js @@ -23,43 +23,77 @@ * } */ -/** - * @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(val) { + this.value = val; + this.next = 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); + append(value) { + const newNode = new Node(value); + if (!this.head) { + this.head = newNode; + return; + } + let temp = this.head; + while (temp.next) { + temp = temp.next; + } + temp.next = newNode; + } - if (list) { - node.next = list; + sum() { + let temp = this.head; + let total = 0; + while (temp) { + total += temp.value; + temp = temp.next; } - list = node; + return total; } +} + +function sumLinkedLists(l1, l2) { + const totalSum = l1.sum() + l2.sum(); + const resultList = new LinkedList(); - return list; -}; + // Convert totalSum to a string, split it into digits, and create nodes + String(totalSum) + .split('') + .map(Number) + .forEach((digit) => resultList.append(digit)); + + return resultList; +} + +// Criação das listas l1 e l2 +let l1 = new LinkedList(); +let l2 = new LinkedList(); + +const values1 = [7, 2, 4, 3]; +const values2 = [5, 6, 4]; + +values1.forEach((value) => l1.append(value)); +values2.forEach((value) => l2.append(value)); + +// Soma das listas +const result = sumLinkedLists(l1, l2); + +// Função para exibir a lista encadeada +function printList(list) { + let temp = list.head; + const result = []; + while (temp) { + result.push(temp.value); + temp = temp.next; + } + return result.join(' -> '); +} -export { addTwoNumbers }; +console.log(printList(result)); // Saída: 7 -> 8 -> 0 -> 7 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; + From e75b3c481f26231761e24ec5121a243792b226c5 Mon Sep 17 00:00:00 2001 From: cassiano Date: Fri, 29 Nov 2024 15:33:01 -0300 Subject: [PATCH 05/11] fiz exericcio de data structure --- src/linked-list/add-two-numbers-ii.js | 143 ++++++++----- ...earch-tree-to-sorted-doubly-linked-list.js | 197 +++++++++++++++--- ...nvert-sorted-list-to-binary-search-tree.js | 41 ---- .../copy-list-with-random-pointer.js | 29 --- 4 files changed, 257 insertions(+), 153 deletions(-) diff --git a/src/linked-list/add-two-numbers-ii.js b/src/linked-list/add-two-numbers-ii.js index 23bcff8..7eeef25 100644 --- a/src/linked-list/add-two-numbers-ii.js +++ b/src/linked-list/add-two-numbers-ii.js @@ -24,76 +24,109 @@ */ class Node { - constructor(val) { - this.value = val; - this.next = null; + constructor(value) { + this.value = value + this.next = null + this.tail = null } } - class LinkedList { constructor() { - this.head = null; + this.head = null } - append(value) { - const newNode = new Node(value); - if (!this.head) { - this.head = newNode; - return; + 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 } - let temp = this.head; - while (temp.next) { - temp = temp.next; + } + 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 } - temp.next = newNode; } - - sum() { - let temp = this.head; - let total = 0; + 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) { - total += temp.value; - temp = temp.next; + saida += String(temp.value) + temp = temp.next } - return total; + return console.log(saida) } } +console.clear() +let node1 = new LinkedList() +let node2 = new LinkedList() +let collection = [] -function sumLinkedLists(l1, l2) { - const totalSum = l1.sum() + l2.sum(); - const resultList = new LinkedList(); - - // Convert totalSum to a string, split it into digits, and create nodes - String(totalSum) - .split('') - .map(Number) - .forEach((digit) => resultList.append(digit)); +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 resultList; } +node1.print() +node2.print() +let conj = new LinkedList() +conj.twoNodes(node1, node2) +// node1.removeFromEnd() +node1.print() -// Criação das listas l1 e l2 -let l1 = new LinkedList(); -let l2 = new LinkedList(); - -const values1 = [7, 2, 4, 3]; -const values2 = [5, 6, 4]; - -values1.forEach((value) => l1.append(value)); -values2.forEach((value) => l2.append(value)); - -// Soma das listas -const result = sumLinkedLists(l1, l2); - -// Função para exibir a lista encadeada -function printList(list) { - let temp = list.head; - const result = []; - while (temp) { - result.push(temp.value); - temp = temp.next; - } - return result.join(' -> '); -} -console.log(printList(result)); // Saída: 7 -> 8 -> 0 -> 7 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..0c06f2d 100644 --- a/src/linked-list/copy-list-with-random-pointer.js +++ b/src/linked-list/copy-list-with-random-pointer.js @@ -15,33 +15,4 @@ * } */ -/** - * @param {RandomListNode} head - * @return {RandomListNode} - */ -const copyRandomList = head => { - if (!head) { - return null; - } - - const map = new Map(); - - // Step 1. Copy all the nodes - let p = head; - while (p) { - map.set(p, new RandomListNode(p.label)); - p = p.next; - } - - // 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; - } - - return map.get(head); -}; -export { copyRandomList }; From c11838df3133a07cb7bc0ef3942eb4293b5d887c Mon Sep 17 00:00:00 2001 From: cassiano Date: Fri, 29 Nov 2024 16:45:22 -0300 Subject: [PATCH 06/11] fiz exericicos de linked list e binary tree --- .../copy-list-with-random-pointer.js | 178 ++++++++++++++++++ .../insert-into-a-cyclic-sorted-list.js | 58 +----- src/linked-list/insertion-sort-list.js | 30 --- 3 files changed, 179 insertions(+), 87 deletions(-) diff --git a/src/linked-list/copy-list-with-random-pointer.js b/src/linked-list/copy-list-with-random-pointer.js index 0c06f2d..17ede7f 100644 --- a/src/linked-list/copy-list-with-random-pointer.js +++ b/src/linked-list/copy-list-with-random-pointer.js @@ -15,4 +15,182 @@ * } */ +console.clear() +class Node { + constructor(value) { + this.value = value + this.next = null + } +} +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 + } + } + + 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 + } + + print() { + return this.stack.print() + } + +} +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..9f56c95 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,4 @@ * } */ -import ListNode from 'common/list-node'; - -/** - * @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; - } - } - - // 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 }; +console.clear() diff --git a/src/linked-list/insertion-sort-list.js b/src/linked-list/insertion-sort-list.js index 6787227..aa90ea1 100644 --- a/src/linked-list/insertion-sort-list.js +++ b/src/linked-list/insertion-sort-list.js @@ -29,33 +29,3 @@ * 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; - } - - // insert curr between p and p.next - curr.next = p.next; - p.next = curr; - curr = next; - } - - return dummy.next; -}; - -export { insertionSortList }; From a0291c41e20635fd61594c5415717a52b3bf102e Mon Sep 17 00:00:00 2001 From: cassiano Date: Fri, 29 Nov 2024 16:46:13 -0300 Subject: [PATCH 07/11] s --- src/linked-list/insert-into-a-cyclic-sorted-list.js | 2 ++ 1 file changed, 2 insertions(+) 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 9f56c95..8f625bd 100644 --- a/src/linked-list/insert-into-a-cyclic-sorted-list.js +++ b/src/linked-list/insert-into-a-cyclic-sorted-list.js @@ -21,3 +21,5 @@ */ console.clear() + + From 4a10de038511366f7125784e761dd45709b31c1d Mon Sep 17 00:00:00 2001 From: cassiano Date: Wed, 18 Dec 2024 15:14:08 -0300 Subject: [PATCH 08/11] fiz alguns exericios --- .../insert-into-a-cyclic-sorted-list.js | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) 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 8f625bd..ab32439 100644 --- a/src/linked-list/insert-into-a-cyclic-sorted-list.js +++ b/src/linked-list/insert-into-a-cyclic-sorted-list.js @@ -22,4 +22,67 @@ console.clear() +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 + } + +} From eb6e6cde21f3a3999510eadb37f697f5788a0685 Mon Sep 17 00:00:00 2001 From: cassi35 Date: Tue, 7 Jan 2025 16:49:34 -0300 Subject: [PATCH 09/11] fix o sort --- src/linked-list/add-two-numbers-ii.js | 1 - src/linked-list/insertion-sort-list.js | 126 ++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/linked-list/add-two-numbers-ii.js b/src/linked-list/add-two-numbers-ii.js index 7eeef25..7e98d62 100644 --- a/src/linked-list/add-two-numbers-ii.js +++ b/src/linked-list/add-two-numbers-ii.js @@ -129,4 +129,3 @@ conj.twoNodes(node1, node2) // node1.removeFromEnd() node1.print() - diff --git a/src/linked-list/insertion-sort-list.js b/src/linked-list/insertion-sort-list.js index aa90ea1..d8e6fba 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,3 +29,127 @@ * this.next = null; * } */ +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() { + let length = this.size + let current_node = this.head + for (let i = 0; i < length; i++) { + let key = current_node + + } + } + 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() From f1f21156e307f60c5a7d0f9ae15e13d4c15f53bd Mon Sep 17 00:00:00 2001 From: cassi35 Date: Tue, 28 Jan 2025 12:00:39 -0300 Subject: [PATCH 10/11] fiz algo --- src/linked-list/insertion-sort-list.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/linked-list/insertion-sort-list.js b/src/linked-list/insertion-sort-list.js index d8e6fba..054b68b 100644 --- a/src/linked-list/insertion-sort-list.js +++ b/src/linked-list/insertion-sort-list.js @@ -130,12 +130,19 @@ class LinkedList { } } insertion_sort() { - let length = this.size - let current_node = this.head - for (let i = 0; i < length; i++) { - let key = current_node - + 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){ + } print() { let root = this.head From 5620334ce648070ddfea8ada799321a37524d60f Mon Sep 17 00:00:00 2001 From: cassi35 Date: Thu, 30 Jan 2025 11:03:03 -0300 Subject: [PATCH 11/11] fiz coisas --- src/matrix/brick-wall.js | 19 ------------------- 1 file changed, 19 deletions(-) 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 };