Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adicionando mais atividades #351

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 7 additions & 14 deletions src/array/beautiful-arrangement-ii.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
36 changes: 13 additions & 23 deletions src/array/can-place-flowers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
15 changes: 9 additions & 6 deletions src/array/candy-crush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
5 changes: 3 additions & 2 deletions src/array/container-with-most-water.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* 6 x x x x x x
*/


/**
* @param {number[]} height
* @return {number}
Expand All @@ -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);

Expand All @@ -70,4 +71,4 @@ const maxArea = height => {
return max;
};

export { maxArea };
export { maxArea };
134 changes: 100 additions & 34 deletions src/linked-list/add-two-numbers-ii.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
27 changes: 1 addition & 26 deletions src/linked-list/add-two-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Loading