Skip to content

Commit 3e47567

Browse files
committed
30 oct
1 parent 819843f commit 3e47567

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

JavaScript/math.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var fib = function(n) {
2+
if (n <= 1){
3+
return n;
4+
}
5+
return fib(n-1)+fib(n-2);
6+
};
7+
8+
var isPalindrome = function(s) {
9+
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g,'');
10+
return cleaned === cleaned.split('').reverse().join('');
11+
};
12+
13+
var kidsWithCandies = function(candies, extraCandies) {
14+
let max = Math.max(candies);
15+
let result = []
16+
for (let candy in candies){
17+
result.push(candy+extraCandies >= max)
18+
}
19+
return result;
20+
};
21+
22+
var reverseList = function(head) {
23+
let prev = null
24+
let curr = head
25+
while (curr != null){
26+
let next = curr.next;
27+
curr.next = prev;
28+
next = prev
29+
curr = next;
30+
}
31+
};
32+
33+
var kidsWithCandies = function(candies, extraCandies) {
34+
let max = Math.max(...candies);
35+
let result = []
36+
for (let candy of candies){
37+
result.push(candy+extraCandies >= max)
38+
}
39+
return result;
40+
};
41+
42+
var reverseList = function(head) {
43+
let prev = null
44+
let curr = head
45+
while (curr !== null){
46+
let next = curr.next;
47+
curr.next = prev;
48+
prev = curr;
49+
curr = next;
50+
}
51+
return prev;
52+
};
53+
54+
var canPlaceFlowers = function(flowerbed, n) {
55+
for (let i = 0; i < flowerbed.length; i++) {
56+
// Check if the current plot is empty and the plots to the left and right are also empty or out of bounds
57+
if (flowerbed[i] === 0 &&
58+
(i === 0 || flowerbed[i - 1] === 0) &&
59+
(i === flowerbed.length - 1 || flowerbed[i + 1] === 0)) {
60+
61+
// Plant a flower
62+
flowerbed[i] = 1;
63+
n--; // Reduce the count of flowers we need to plant
64+
65+
// If we have planted enough flowers, return true
66+
if (n === 0) return true;
67+
}
68+
}
69+
return n <= 0; // If there are still flowers left to plant, return false
70+
};

Python/sudokuSolver.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def solveSudoku(self, board):
3+
def isValid(num, row, col):
4+
for i in range(9):
5+
if board[row][i] == num:
6+
return False
7+
for i in range(9):
8+
if board[i][col] == num:
9+
return False
10+
start_row, start_col = 3*(row//3), 3*(col//3)
11+
for i in range(3):
12+
for j in range(3):
13+
if board[start_row+i][start_col+j]==num:
14+
return False
15+
return True
16+
17+
def solve():
18+
for i in range(9):
19+
for j in range(9):
20+
if board[i][j] == '.':
21+
for num in '123456789':
22+
if isValid(num, i, j) :
23+
board[i][j] = num
24+
if solve():
25+
return True
26+
board[i][j] = '.'
27+
return False
28+
return True
29+
solve()

0 commit comments

Comments
 (0)