Skip to content

Commit

Permalink
Merge branch 'main' into patch-55
Browse files Browse the repository at this point in the history
  • Loading branch information
aadil42 authored Jul 3, 2023
2 parents c259074 + 6e9bbd9 commit a963809
Show file tree
Hide file tree
Showing 47 changed files with 1,129 additions and 120 deletions.
68 changes: 34 additions & 34 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions go/0912-sort-an-array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func sortArray(nums []int) []int {
sort.Ints(nums)
return nums
}
11 changes: 11 additions & 0 deletions go/1929-concatenation-of-array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func getConcatenation(nums []int) []int {
n := len(nums)
ans := make([]int, 2*n) //lets make an array of int type and 2*n size
i := 0
for i < n { //implementing for loop for the given condition
ans[i] = nums[i]
ans[i+n] = nums[i]
i++
}
return ans
}
1 change: 0 additions & 1 deletion java/0091-decode-ways.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
class Solution {

public int numDecodings(String s) {
int[] dp = new int[s.length() + 1];
int twoBack = 1; // empty string
int oneBack = s.charAt(0) == '0' ? 0 : 1;
int current = oneBack;
Expand Down
47 changes: 23 additions & 24 deletions javascript/0022-generate-parentheses.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* https://leetcode.com/problems/generate-parentheses
* DFS
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
* Time O(2^N) | Space O(2^N)
* https://leetcode.com/problems/generate-parentheses
* @param {number} n
* @return {string[]}
*/
var generateParenthesis = (n) => dfs(n);/* Time O(2^N) | Space O(2^N) */
var generateParenthesis = (n) => dfs(n);

const dfs = (n, combos = [], open = 0, close = 0, path = []) => {
const isBaseCase = path.length === (n * 2);
const isBaseCase = (path.length === (n * 2));
if (isBaseCase) {
combos.push(path.join(''));/* Space O(N + N) */

Expand All @@ -25,35 +26,36 @@ const dfs = (n, combos = [], open = 0, close = 0, path = []) => {
}

const backTrackOpen = (n, combos, open, close, path) => {
path.push('('); /* | Space O(N) */
path.push('(');/* Space O(N) */
dfs(n, combos, (open + 1), close, path);/* Time O(2^N) | Space O(2^N) */
path.pop();
}

const backTrackClose = (n, combos, open, close, path) => {
path.push(')'); /* | Space O(N) */
path.push(')');/* Space O(N) */
dfs(n, combos, open, (close + 1), path);/* Time O(2^N) | Space O(2^N) */
path.pop();
}

/**
* https://leetcode.com/problems/generate-parentheses
* BFS
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
* Time O(2^N) | Space O(2^N)
* https://leetcode.com/problems/generate-parentheses
* @param {number} n
* @return {string[]}
*/
var generateParenthesis = (n) => bfs(n);/* Time O(2^N) | Space O(2^N) */
var generateParenthesis = (n) => bfs(n);

const bfs = (n, queue, combos = []) => {
const bfs = (n, combos = []) => {
const queue = new Queue([ ['', 0, 0] ]);

while (!queue.isEmpty()) {/* Time O(2^N) */
const [ str, open, close ] = queue.dequeue();

const isBaseCase = (open === n) && (close === n);
const isBaseCase = ((open === n) && (close === n));
if (isBaseCase) {
combos.push(str); /* Space O(N) */
combos.push(str); /* Space O(N) */

continue;
}
Expand All @@ -69,31 +71,28 @@ const bfs = (n, queue, combos = []) => {
}

/**
* https://leetcode.com/problems/generate-parentheses
* DFS
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
* Time O(2^N) | Space O(2^N)
* https://leetcode.com/problems/generate-parentheses
* @param {number} n
* @return {string[]}
*/
var generateParenthesis = (n, combos = []) => {
const isBaseCase = n === 0;
const isBaseCase = (n === 0);
if (isBaseCase) {
combos.push(''); /* | Space O(N) */
combos.push('');

return combos;
return combos
}

return closureNumber(n, combos);/* Time O(2^N) | Space O(2^N) */
}

const closureNumber = (n, combos) => {
for (let c = 0; c < n; c++) {/* Time O(N) */
for (const left of generateParenthesis(c)) { /* Time O(2^N) | Space O(2^N) */
for (const right of generateParenthesis(((n - 1) - c))) {/* Time O(2^N) | Space O(2^N) */
combos.push(`(${left})${right}`); /* | Space O(N) */
for (let c = 0; (c < n); c++) {
for (const left of generateParenthesis(c)) {
for (const right of generateParenthesis(((n - 1) - c))) {
combos.push(`(${left})${right}`);
}
}
}

return combos
}
}
10 changes: 5 additions & 5 deletions javascript/0036-valid-sudoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var searchGrid = (board, boxes, cols, rows) => {
const isEmpty = char === '.';
if (isEmpty) continue;

const hasMoved = boxes[index][char] || cols[col][char] || rows[row][char];
const hasMoved = boxes[index][(char - 1)] || cols[col][(char - 1)] || rows[row][(char - 1)];
if (hasMoved) return false;

rows[row][char] = true; /* Space O(ROWS * COLS)*/
cols[col][char] = true; /* Space O(ROWS * COLS)*/
boxes[index][char] = true; /* Space O(ROWS * COLS)*/
rows[row][(char - 1)] = true; /* Space O(ROWS * COLS)*/
cols[col][(char - 1)] = true; /* Space O(ROWS * COLS)*/
boxes[index][(char - 1)] = true; /* Space O(ROWS * COLS)*/
}
}

Expand Down Expand Up @@ -84,4 +84,4 @@ var searchGrid = (board, boxes, rows, cols) => {
}

return true;
}
}
97 changes: 49 additions & 48 deletions javascript/0080-remove-duplicates-from-sorted-array-ii.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
/**
* Linear
* https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
* Time O(n) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
* https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
*
* Time O(n) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let current = nums[0];
let sameElCount = 0;
let current = nums[0];
let sameElCount = 0;

for(let i = 0; i < nums.length; i++) {
if(current === nums[i]) {
sameElCount++;
}
if(current !== nums[i]) {
current = nums[i];
sameElCount = 1;
}
if(sameElCount > 2) {
nums.splice(i,1);
i--;
}
for(let i = 0; i < nums.length; i++) {
if(current === nums[i]) {
sameElCount++;
}
if(current !== nums[i]) {
current = nums[i];
sameElCount = 1;
}
if(sameElCount > 2) {
nums.splice(i,1);
i--;
}
}
};


/**
* Two pointer
* Time O(n^2) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
* Two pointer
* Time O(n^2) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates2 = function(nums) {

let current = nums[0];
let sameElCount = 0;
let current = nums[0];
let sameElCount = 0;


for(let i = 0; i < nums.length; i++) {
if(current === nums[i]) {
sameElCount++;
}
if(current !== nums[i]) {
current = nums[i];
sameElCount = 1;
}
if(sameElCount > 2) {
let left = i;
let right = i+1;
let count = 1;
while(nums[left] === nums[right]) {
count++;
right++;
}
nums.splice(left, count);
i--;
}
for(let i = 0; i < nums.length; i++) {
if(current === nums[i]) {
sameElCount++;
}
if(current !== nums[i]) {
current = nums[i];
sameElCount = 1;
}
return nums.length;
};
if(sameElCount > 2) {
let left = i;
let right = i+1;
let count = 1;
while(nums[left] === nums[right]) {
count++;
right++;
}
nums.splice(left, count);
i--;
}
}
return nums.length;
};
37 changes: 37 additions & 0 deletions javascript/0116-populating-next-right-pointers-in-each-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* BFS
* Time O(n) | Space O(1)
*
* https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
* // Definition for a Node.
* function Node(val, left, right, next) {
* this.val = val === undefined ? null : val;
* this.left = left === undefined ? null : left;
* this.right = right === undefined ? null : right;
* this.next = next === undefined ? null : next;
* };
*/
/**
* @param {Node} root
* @return {Node}
*/

var connect = function(root) {

let currentNode = root;
let nextLevelNode = root && root.left;

while(currentNode && nextLevelNode) {
currentNode.left.next = currentNode.right;
if(currentNode.next) {
currentNode.right.next = currentNode.next.left;
}
currentNode = currentNode.next;
if(!currentNode) {
currentNode = nextLevelNode;
nextLevelNode = currentNode.left;
}
}

return root;
};
30 changes: 30 additions & 0 deletions javascript/0125-valid-palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,33 @@ var isPalindrome = function(s) {
}
return true;
};

/**
* 2 Pointer | Midde Convergence | No RegEx | No Copying
* Time O(N) | Space O(1)
* https://leetcode.com/problems/valid-palindrome/
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
const isAlphaNumeric = c => (c.toLowerCase() >= 'a' && c.toLowerCase() <= 'z') || c >= '0' && c <= '9'

let left = 0;
let right = s.length - 1;
let skipLeft, skipRight, endsEqual = false;

while (left < right) {
skipLeft = !isAlphaNumeric(s.charAt(left))
if (skipLeft) { left++; continue; }

skipRight = !isAlphaNumeric(s.charAt(right))
if (skipRight) { right--; continue; }

endsEqual = s.charAt(left).toLowerCase() === s.charAt(right).toLowerCase()
if (!endsEqual) return false

left++
right--
}
return true
};
4 changes: 2 additions & 2 deletions javascript/0143-reorder-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
var reorderList = function(head) {
const mid = getMid(head); /* Time O(N) */
const reveredFromMid = reverse(mid);/* Time O(N) */
const reversedFromMid = reverse(mid);/* Time O(N) */

reorder(head, reveredFromMid); /* Time O(N) */
reorder(head, reversedFromMid); /* Time O(N) */
};

const getMid = (head) => {
Expand Down
22 changes: 22 additions & 0 deletions javascript/0162-find-peak-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Time O(log(N)) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findPeakElement = function(nums) {
let [l, r] = [0, nums.length - 1];
let mid = null;
while (l <= r){
mid = (l + r) >> 1;
if (mid < nums.length - 1 && nums[mid] < nums[mid+1]){
l = mid + 1;
}
else if (mid > 0 && nums[mid] < nums[mid-1]) {
r = mid - 1;
}
else {
break;
}
}
return mid;
};
21 changes: 21 additions & 0 deletions javascript/0187-repeated-dna-sequences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string} s
* @return {string[]}
*/
var findRepeatedDnaSequences = function (s) {
const seen = new Set();
const res = new Set();
const arr = Array.from(s);

for (let l = 0; l < arr.length - 9; l++) {
const sequence = s.slice(l, l + 10);

if (seen.has(sequence)) {
res.add(sequence);
} else {
seen.add(sequence);
}
}

return Array.from(res);
};
Loading

0 comments on commit a963809

Please sign in to comment.