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

[Zioq] Week 1 #675

Merged
merged 9 commits into from
Dec 14, 2024
Merged
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
32 changes: 32 additions & 0 deletions contains-duplicate/Zioq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
let dup_set = new Set(nums); // Initialize Set
return dup_set.size !== nums.length


/* Previous Code before the review */
// for (let num of nums) {
// dup_set.add(num) // Add value into the set (duplicated value will be ignored)
// }

// if(dup_set.size !== nums.length) {
// return true
// }
// return false
};

/*
Space Complexity - O(n) - Create a set to store elements
Time Complexity - O(n) - Traverse through the array
*/


/* Test code */
console.log(containsDuplicate([1, 2, 3, 1])); // true
console.log(containsDuplicate([1, 2, 3, 4])); // false
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true


29 changes: 29 additions & 0 deletions house-robber/Zioq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
if( nums.length < 1 ) { return nums[0]}

let prev_2 = nums[0]
let prev_1 = Math.max(nums[0], nums[1]);

for( let i= 2; i <nums.length; i++ ) {
let crr = Math.max(prev_1, nums[i]+ prev_2 );
prev_2 = prev_1;
prev_1 = crr;
}

return prev_1;
};
/*
TC: O(n)
SC: O(1)
*/


/* Test Case */
console.log(rob([1,2,3,1]));
console.log(rob([2,7,9,3,1]));


25 changes: 25 additions & 0 deletions longest-consecutive-sequence/Zioq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
if(nums.length === 0) return 0
let set = new Set();
for(const n of nums) {
set.add(n);
}
let max = 0;
for(let n of set) {
if(!set.has(n-1)) {
let count = 0;
while(set.has(n++)) {
count++;
}
max = Math.max(max, count);
}
}
return max;
};

/* Test code */
console.log(longestConsecutive([9,1,4,7,3,-1,0,5,8,-1,6])); // true
30 changes: 30 additions & 0 deletions top-k-frequent-elements/Zioq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {

const map = new Map();
let arr = []

nums.forEach( e => {
if(map.has(e)) {
let current_value = map.get(e)
map.set(e, current_value +1)
} else {
map.set(e, 1)
}
})
const store = [...map.entries()].sort((a,b) => b[1] - a[1])
for( let i = 0 ; i < k && i < store.length ; i++ ) {
arr.push(store[i][0])
}
return arr

};


/* Test code */
console.log(topKFrequent([1,1,1,2,2,3],2)); // true
console.log(topKFrequent([1,2],1)); // true
33 changes: 33 additions & 0 deletions valid-palindrome/Zioq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {

let text = s.replace(/[^0-9a-z]/gi, '');
text = text.replace(/\s/g, '').toLowerCase();

const str_arr = text.split("");
if( str_arr % 2 === 1 ) {
return false;
}

let length = str_arr.length - 1 ;
for ( const [i, value] of str_arr.entries()) {
if( value == str_arr[length -i] ) continue;
if(value != str_arr[length - i]) {
return false
}
}
return true;
};

/*
Space Complexity - O(n) - Create a array to store elements
Time Complexity - O(n) - Traverse through the array
*/

/* Test code */
console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
console.log(isPalindrome("race a car")); // false
console.log(isPalindrome(" ")); // true
Loading