diff --git a/_config.yml b/_config.yml index 277f1f2..c419263 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1 @@ -theme: jekyll-theme-cayman +theme: jekyll-theme-cayman \ No newline at end of file diff --git a/add_digits.js b/add_digits.js deleted file mode 100644 index 0cd1181..0000000 --- a/add_digits.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @param {number} num - * @return {number} - */ -var addDigits = function(num) { - let arr = []; - - let total = num; - - while(total > 9) { - arr = total.toString().split(""); - total = 0; - - for(let i = 0; i < arr.length; i++) { - total += +arr[i]; - } - } - - return total; -}; diff --git a/add_two_integers.js b/add_two_integers.js deleted file mode 100644 index 0c4a0e7..0000000 --- a/add_two_integers.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @param {number} num1 - * @param {number} num2 - * @return {number} - */ -var sum = function(num1, num2) { - return num1 + num2; -}; diff --git a/add_two_promises.js b/add_two_promises.js deleted file mode 100644 index 2b2606f..0000000 --- a/add_two_promises.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @param {Promise} promise1 - * @param {Promise} promise2 - * @return {Promise} - */ -var addTwoPromises = async function(promise1, promise2) { - return Promise.all([promise1, promise2]).then(result => { - return result.reduce((a, b) => a + b, 0) - }) -}; - -/** - * addTwoPromises(Promise.resolve(2), Promise.resolve(2)) - * .then(console.log); // 4 - */ diff --git a/array_from_permutation.js b/array_from_permutation.js deleted file mode 100644 index 93a0b8c..0000000 --- a/array_from_permutation.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var buildArray = function(nums) { - let arr = []; - - for(let i = 0; i < nums.length; i++) { - arr.push(nums[nums[i]]) - } - - return arr; -}; diff --git a/article_views_1.sql b/article_views_1.sql deleted file mode 100644 index 7f56262..0000000 --- a/article_views_1.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -SELECT DISTINCT author_id as 'id' FROM Views WHERE author_id = viewer_id ORDER BY author_id; diff --git a/big_countries.sql b/big_countries.sql deleted file mode 100644 index 4dbef37..0000000 --- a/big_countries.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -SELECT name, population, area FROM World WHERE (area > 3000000 || population > 25000000) diff --git a/binary_search.js b/binary_search.js deleted file mode 100644 index 6b8bd80..0000000 --- a/binary_search.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -var search = function(nums, target) { - let temp = nums; - - while(temp.length !== 1) { - let mid = temp[Math.floor(temp.length / 2)]; - let index = Math.floor(temp.length / 2); - - if(target > mid) { - temp = temp.slice(index); - } else if(target < mid) { - temp = temp.slice(0, index); - } else { - return nums.indexOf(mid); - } - } - if(target === nums[0]) return 0; - - return -1; -}; diff --git a/binary_tree_inorder_traversal.js b/binary_tree_inorder_traversal.js deleted file mode 100644 index a921639..0000000 --- a/binary_tree_inorder_traversal.js +++ /dev/null @@ -1,17 +0,0 @@ -var inorderTraversal = function(root) { - let number = []; - let stack = []; - - while(root || stack.length) { - if(root) { - stack.push(root); - root = root.left; - } else { - root = stack.pop(); - number.push(root.val); - root = root.right; - } - } - - return number; -}; diff --git a/combine_tables.sql b/combine_tables.sql deleted file mode 100644 index 4f45343..0000000 --- a/combine_tables.sql +++ /dev/null @@ -1,5 +0,0 @@ -# Write your MySQL query statement below -SELECT firstName, lastName, city, state -FROM Person -LEFT JOIN Address -ON Person.personID = Address.personID; diff --git a/concatenation_of_array.js b/concatenation_of_array.js deleted file mode 100644 index e8b21d0..0000000 --- a/concatenation_of_array.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var getConcatenation = function(nums) { - return nums.concat(nums); -}; diff --git a/convert_temperature.js b/convert_temperature.js deleted file mode 100644 index 706e609..0000000 --- a/convert_temperature.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {number} celsius - * @return {number[]} - */ -var convertTemperature = function(celsius) { - return [celsius + 273.15, celsius * 1.8 + 32] -}; diff --git a/count_primes.js b/count_primes.js deleted file mode 100644 index 0180c5e..0000000 --- a/count_primes.js +++ /dev/null @@ -1,20 +0,0 @@ -// /** -// * @param {number} n -// * @return {number} -// */ - -var countPrimes = function(n) { - if (n == 0 || n == 1) return 0 - - let count = 0 - for (let i = 2; i < n; i++) if(isPrime(i)) count++ - return count -}; - -const isPrime = n => { - if (n == 2) return true - for (let i = 2; i <= Math.sqrt(n); i++) - if (n % i == 0) return false - - return true -} diff --git a/decompress_run_length_encoded_list.js b/decompress_run_length_encoded_list.js deleted file mode 100644 index f922137..0000000 --- a/decompress_run_length_encoded_list.js +++ /dev/null @@ -1,21 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var decompressRLElist = function(nums) { - let decompressed = [], freq = [], val = []; - - for(let i = 0; i < nums.length; i++) { - if(i % 2 == 0) - freq.push(nums[i]); - else - val.push(nums[i]); - } - - for(let i = 0; i < freq.length; i++) { - for(let j = 0; j < freq[i]; j++) - decompressed.push(val[i]); - } - - return decompressed; -}; diff --git a/final_value.js b/final_value.js deleted file mode 100644 index e852da6..0000000 --- a/final_value.js +++ /dev/null @@ -1,9 +0,0 @@ -/** - * @param {string[]} operations - * @return {number} - */ -var finalValueAfterOperations = function(operations) { - return (operations.reduce( - (accumulator, value) => value.includes("++") ? ++accumulator : --accumulator, 0)) - -}; diff --git a/find_customer_referee.sql b/find_customer_referee.sql deleted file mode 100644 index 0bcf85a..0000000 --- a/find_customer_referee.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -SELECT name FROM Customer WHERE referee_id != '2' or referee_id is null diff --git a/find_the_difference.js b/find_the_difference.js deleted file mode 100644 index 99508e4..0000000 --- a/find_the_difference.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @param {string} s - * @param {string} t - * @return {character} - */ -var findTheDifference = function(s, t) { - let x = s.split("").sort().join(""); - let y = t.split("").sort().join(""); - - for(let i = 0; i < y.length; i++) - if(y[i] !== x[i]) return y[i]; -}; diff --git a/find_the_peaks.js b/find_the_peaks.js deleted file mode 100644 index a58fa8a..0000000 --- a/find_the_peaks.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @param {number[]} mountain - * @return {number[]} - */ -var findPeaks = function(mountain) { - let peaks = []; - - if(mountain.length <= 2) return peaks; - - for(let i = 1; i < mountain.length - 1; i++) { - if(mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) - peaks.push(i); - } - - return peaks; -}; diff --git a/greatest_number_of_candies.js b/greatest_number_of_candies.js deleted file mode 100644 index ffd736d..0000000 --- a/greatest_number_of_candies.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @param {number[]} candies - * @param {number} extraCandies - * @return {boolean[]} - */ -var kidsWithCandies = function(candies, extraCandies) { - let result = []; - - for(let i = 0; i < candies.length; i++) { - if((candies[i] + extraCandies) >= Math.max(...candies)) - result.push(true); - else - result.push(false); - } - - return result; -}; diff --git a/README.md b/index.md similarity index 94% rename from README.md rename to index.md index 616c29d..a61def3 100644 --- a/README.md +++ b/index.md @@ -1,6 +1,4 @@ -# LeetCode Solved Problems -# My LeetCode Solutions -### Go to https://thecodersingh.github.io/Leetcode-Javascript-Solutions/ +# LeetCode Solutions | # | Title | Description | Solution | | --- | --- | --- | --- | @@ -15,7 +13,6 @@ | 65 | Valid Number | Validate if a given string can be interpreted as a decimal number. | [Solution](valid_number.js) | | 66 | Plus One | Increment the large integer by one and return the resulting array of digits. | [Solution](plus_one.js) | | 94 | Binary Tree Inorder Traversal | Given the root of a binary tree, return the inorder traversal of its nodes' values. | [Solution](binary_tree_inorder_traversal.js) | -| 100| Same Tree | Given the roots of two binary trees p and q, write a function to check if they are the same or not. | [Solution](same_tree.js) | | 104 | Maximum Depth of Binary Tree | Given the root of a binary tree, return its maximum depth. | [Solution](maximum_depth_binary_tree.js) | | 125 | Valid Palindrome | Given a string s, return true if it is a palindrome, or false otherwise. | [Solution](valid_palindrome.js) | | 136 | Single Number | Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. | [Solution](single_number.js) | @@ -48,7 +45,6 @@ | 1313 | Decompress Run-Length Encoded List | We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list. | [Solution](decompress_run_length_encoded_list.js) | | 1342 | Number of Steps to Reduce a Number to Zero | Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. | [Solution](reduce_to_zero.js) | | 1365 | How Many Numbers Are Smaller Than the Current Number | Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i]. | [Solution](numbers_smaller_than_current.js) | -| 1046 | Last Stone Weight | Return the weight of the last remaining stone. If there are no stones left, return 0. | [Solution](last_stone_weight.js) | | 1431 | Kids With the Greatest Number of Candies| For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies. | [Solution](greatest_number_of_candies.js) | | 1470 | Shuffle the Array | Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn]. | [Solution](shuffle_array.js) | | 1476 | Subrectangle Queries | Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods. | [Solution](subrectangle_queries.js) | @@ -57,16 +53,16 @@ | 1512 | Number of Good Pairs | Given an array of integers nums. A pair (i,j) is called good if nums[i] == nums[j] and i < j. Return the number of good pairs. | [Solution](number_good_pairs.js) | | 1528 | Shuffle String | Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. Return the shuffled string. | [Solution](shuffle_string.js) | | 1603 | Design Parking System | Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size. | [Solution](parking_system.js) | -| 1672 | Richest Customer Wealth | You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has. A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth. | [Solution](richest_customer_wealth.js) | +| 1672 | Richest Customer Wealth | You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has. A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth. | [Solution](richest_customer_wealth.js) | | | 1683 | Invalid Tweets | Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15. | [Solution](invalid_tweets.sql) | | 1757 | Recyclable and Low Fat Products | Write a solution to find the ids of products that are both low fat and recyclable. | [Solution](recyclable_low_fat.sql) | -| 1920 | Build Array from Permutation | Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it. | [Solution](array_from_permutation.js) | +| 1920 | Build Array from Permutation | Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it. | [Solution](array_from_permutation.js) | 1929 | Concatenation of Array | Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays. Return the array ans. | [Solution](concatenation_of_array.js) | | 2011 | Final Value of Variable After Performing Operations | Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations. | [Solution](final_value.js) | | 2114 | Maximum Number of Words Found in Sentences | Return the maximum number of words that appear in a single sentence. | [Solution](maximum_words.js) | | 2235 | Add Two Integers | Given two integers num1 and num2, return the sum of the two integers. | [Solution](add_two_integers.js) | -| 2413 | Smallest Even Multiple | Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n. | [Solution](smallest_even_multiple.js) | -| 2469 | Convert the Temperature | You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit]. | [Solution](convert_temperature.js) | +| 2413 | Smallest Even Multiple | Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n. | [Solution](smallest_even_multiple.js) +| 2469 | Convert the Temperature | You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = (kelvin, fahrenheit). | [Solution](convert_temperature.js) | | 2703 | Return Length of Arguments Passed | Write a function argumentsLength that returns the count of arguments passed to it. | [Solution](length_of_arguments.js) | | 2723 | Add Two Promises | Given two promises, return a new promise. The returned promise should resolve with the sum of the two numbers. | [Solution](add_two_promises.js) | | 2769 | Find the Maximum Achievable Number | Return the maximum possible achievable number. | [Solution](maximum_achievable.js) | diff --git a/intersection_of_arrays.js b/intersection_of_arrays.js deleted file mode 100644 index 9bd19eb..0000000 --- a/intersection_of_arrays.js +++ /dev/null @@ -1,9 +0,0 @@ -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number[]} - */ -var intersection = function(nums1, nums2) { - let result = nums1.filter((num) => nums2.includes(num)); - return [...new Set(result)]; -}; diff --git a/invalid_tweets.sql b/invalid_tweets.sql deleted file mode 100644 index 2239a33..0000000 --- a/invalid_tweets.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -SELECT tweet_id from Tweets WHERE LENGTH(content) > 15 diff --git a/ip_defanging.js b/ip_defanging.js deleted file mode 100644 index 9a74b75..0000000 --- a/ip_defanging.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @param {string} address - * @return {string} - */ -var defangIPaddr = function(address) { - let defangedIpAddr = ""; - for(let i = 0; i < address.length; i++) { - if(address[i] !== ".") - defangedIpAddr = defangedIpAddr.concat(address[i]); - else - defangedIpAddr = defangedIpAddr.concat("[.]"); - } - return defangedIpAddr; -}; diff --git a/jewels_and_stones.js b/jewels_and_stones.js deleted file mode 100644 index e4e92a4..0000000 --- a/jewels_and_stones.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @param {string} J - * @param {string} S - * @return {number} - */ -var numJewelsInStones = function(J, S) { - let stones = 0; - for(let i = 0; i < J.length; i++) { - for(let j = 0; j < S.length; j++) { - if(J[i] === S[j]) - stones++; - } - } - - return stones; -}; diff --git a/last_stone_weight.js b/last_stone_weight.js deleted file mode 100644 index fc0468f..0000000 --- a/last_stone_weight.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @param {number[]} stones - * @return {number} - */ -var lastStoneWeight = function(stones) { - const stonesQueue = new MaxPriorityQueue(); - - let length = stones.length; - - for(let i = 0; i < length; i++) { - stonesQueue.enqueue(stones[i]); - } - - while(stonesQueue.size() > 1) { - let y = stonesQueue.dequeue().element; - let x = stonesQueue.dequeue().element; - - if(y !== x) { - y = y - x; - stonesQueue.enqueue(y); - } else { - stonesQueue.enqueue(0); - } - } - - return stonesQueue.dequeue().element; -}; diff --git a/length_of_arguments.js b/length_of_arguments.js deleted file mode 100644 index 9e1620f..0000000 --- a/length_of_arguments.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * @param {...(null|boolean|number|string|Array|Object)} args - * @return {number} - */ -var argumentsLength = function(...args) { - return args.length; -}; - -/** - * argumentsLength(1, 2, 3); // 3 - */ diff --git a/linked_list_cycle.js b/linked_list_cycle.js deleted file mode 100644 index 3c0e3db..0000000 --- a/linked_list_cycle.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ - -/** - * @param {ListNode} head - * @return {boolean} - */ -var hasCycle = function(head) { - let temp = new Set(); - - while(head !== null) { - if (temp.has(head)) return true; - - temp.add(head); - head = head.next; - } - - return false; -}; diff --git a/longest_common_prefix.js b/longest_common_prefix.js deleted file mode 100644 index c02662a..0000000 --- a/longest_common_prefix.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @param {string[]} strs - * @return {string} - */ -var longestCommonPrefix = function(strs) { - if(strs.length === 0) return ""; - - let prefix = strs[0]; - - for(let i = 1; i < strs.length; i++) { - while(!strs[i].startsWith(prefix)) { - prefix = prefix.slice(0, -1); - } - } - - return prefix; -}; diff --git a/majority_element.js b/majority_element.js deleted file mode 100644 index 645a0bf..0000000 --- a/majority_element.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @param {number[]} nums - * @return {number} - */ -var majorityElement = function(nums) { - let count = 0; - let max = nums.length/2; - - nums.sort(); - - for(let i = 0; i < nums.length; i++) { - count++; - if(count > max) return nums[i]; - for(let j = i + 1; j < nums.length; j++) { - if(nums[j] === nums[i]) { - count++; - if(count > max) return nums[i]; - } - else - break; - } - - count = 0; - } -}; diff --git a/maximum_achievable.js b/maximum_achievable.js deleted file mode 100644 index 20f6d34..0000000 --- a/maximum_achievable.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @param {number} num - * @param {number} t - * @return {number} - */ -var theMaximumAchievableX = function(num, t) { - return num + t * 2; -}; diff --git a/maximum_depth_binary_tree.js b/maximum_depth_binary_tree.js deleted file mode 100644 index 47c02c4..0000000 --- a/maximum_depth_binary_tree.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -var maxDepth = function(root) { - return getDepth(root); -}; - -var getDepth = function(root) { - if(root === null) - return 0; - - let left = getDepth(root.left); - let right = getDepth(root.right); - - if(left > right) - return left + 1; - else - return right + 1; -} diff --git a/maximum_words.js b/maximum_words.js deleted file mode 100644 index b14638e..0000000 --- a/maximum_words.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {string[]} sentences - * @return {number} - */ -var mostWordsFound = function(sentences) { - return Math.max(...sentences.map((sentence, i) => (sentences[i].split(" ").length))) -}; diff --git a/move_zeroes.js b/move_zeroes.js deleted file mode 100644 index 1d35e5c..0000000 --- a/move_zeroes.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @param {number[]} nums - * @return {void} Do not return anything, modify nums in-place instead. - */ -var moveZeroes = function(nums) { - let zeroes = nums.filter((num) => num === 0); - let i = 0; - - while(i < nums.length) { - if(nums[i] === 0) nums.splice(i, 1); - else i++; - } - - nums.splice(nums.length, 0, ...zeroes); -}; diff --git a/non_decreasing_array.js b/non_decreasing_array.js deleted file mode 100644 index b8c60c0..0000000 --- a/non_decreasing_array.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * @param {number[]} nums - * @return {boolean} - */ -var checkPossibility = function(nums) { - for(let i = 0; i < nums.length; i++) { - if(i !== nums.length - 1) { - if(nums[i] > nums[i + 1]) { - if(nums[i + 1] < nums[i - 1]) - nums[i + 1] = nums[i]; - else - nums[i] = nums[i + 1]; - break; - - if(i !== 0) { - if(nums[i - 1] < nums[i]) { - return false; - } - } - } - } - } - - for(let i = 0; i < nums.length; i++) { - if(nums[i] > nums[i + 1]) { - return false; - } - } - - return true; -}; diff --git a/not_boring_movies.sql b/not_boring_movies.sql deleted file mode 100644 index 32fbe6e..0000000 --- a/not_boring_movies.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -SELECT id, movie, description, rating FROM cinema WHERE (id % 2 != 0) AND description != "boring" ORDER BY rating DESC diff --git a/number_disappeared_in_array.js b/number_disappeared_in_array.js deleted file mode 100644 index 807345b..0000000 --- a/number_disappeared_in_array.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var findDisappearedNumbers = function(nums) { - let result = []; - let origLength = nums.length; - let unique = new Set(nums); - - for(let i = 1; i <= origLength; i++) { - if(!unique.has(i)) { - result.push(i); - } - } - - return result; -}; diff --git a/number_good_pairs.js b/number_good_pairs.js deleted file mode 100644 index b0400c7..0000000 --- a/number_good_pairs.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @param {number[]} nums - * @return {number} - */ -var numIdenticalPairs = function(nums) { - let count = 0; - - for(let i = 0; i < nums.length; i++) { - for(let j = i + 1; j < nums.length; j++) { - if(nums[i] === nums[j]) - count++; - } - } - - return count; -}; diff --git a/number_of_recent_calls.js b/number_of_recent_calls.js deleted file mode 100644 index e80697b..0000000 --- a/number_of_recent_calls.js +++ /dev/null @@ -1,28 +0,0 @@ - -var RecentCounter = function() { - this.requests = []; -}; - -/** - * @param {number} t - * @return {number} - */ -RecentCounter.prototype.ping = function(t) { - this.requests.push(t); - - for(let i = 0; i < this.requests.length; i++) { - if(this.requests[i] < (t - 3000)) { - this.requests.splice(i, 1); - i--; - } - - } - - return this.requests.length; -}; - -/** - * Your RecentCounter object will be instantiated and called as such: - * var obj = new RecentCounter() - * var param_1 = obj.ping(t) - */ diff --git a/numbers_even_number_of_digits.js b/numbers_even_number_of_digits.js deleted file mode 100644 index 5e8ec26..0000000 --- a/numbers_even_number_of_digits.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {number[]} nums - * @return {number} - */ -var findNumbers = function(nums) { - let digits = 0, count = 0; - - for(let i = 0; i < nums.length; i++) { - for(let j = 0; j < nums[i].toString().length; j++) - digits++; - - if(digits % 2 == 0) - count++; - - digits = 0; - } - - return count; -}; diff --git a/numbers_smaller_than_current.js b/numbers_smaller_than_current.js deleted file mode 100644 index 2fb111c..0000000 --- a/numbers_smaller_than_current.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var smallerNumbersThanCurrent = function(nums) { - let count = []; - - for(let i = 0; i < nums.length; i++) - count.push(0); - - for(let i = 0; i < nums.length; i++) { - for(let j = 0; j < nums.length; j++) { - if(nums[j] < nums[i]) - count[i]++; - } - } - - return count; -}; diff --git a/palindrome.js b/palindrome.js deleted file mode 100644 index 272a961..0000000 --- a/palindrome.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @param {number} x - * @return {boolean} - */ -var isPalindrome = function(x) { - let second = 0; - let i = 1; - let len = 0; - let z = x; - - if (x < 0) - return false; - - while(true) { - let temp = Math.pow(10, i); - - if(x / temp < 1) { - len = i - 1; - break; - } - - i++; - } - - if (len === 0) - return true; - - // i = 1; - - let j = 0; - let temp = z; - while(j <= len) { - temp = z % 10; - console.log(z) - - second += (temp * Math.pow(10, i - 1)); - console.log(second) - - z = Math.trunc(z / 10); - temp = z; - console.log(z) - console.log("---\n") - j++; - i--; - } - - return x === second; -}; diff --git a/parking_system.js b/parking_system.js deleted file mode 100644 index 0e8141b..0000000 --- a/parking_system.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * @param {number} big - * @param {number} medium - * @param {number} small - */ -var ParkingSystem = function(big, medium, small) { - this.parkingSlots = [big, medium, small]; -}; - -/** - * @param {number} carType - * @return {boolean} - */ -ParkingSystem.prototype.addCar = function(carType) { - return this.parkingSlots[carType - 1]-- > 0; -}; - -/** - * Your ParkingSystem object will be instantiated and called as such: - * var obj = new ParkingSystem(big, medium, small) - * var param_1 = obj.addCar(carType) - */ diff --git a/plus_one.js b/plus_one.js deleted file mode 100644 index 4ccdd7a..0000000 --- a/plus_one.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {number[]} digits - * @return {number[]} - */ -var plusOne = function(digits) { - return (BigInt(digits.join("")) + BigInt(1)).toString().split(""); -}; diff --git a/recyclable_low_fat.sql b/recyclable_low_fat.sql deleted file mode 100644 index 925e068..0000000 --- a/recyclable_low_fat.sql +++ /dev/null @@ -1,2 +0,0 @@ -# Write your MySQL query statement below -select product_id from Products where low_fats="Y" and recyclable="y" diff --git a/reduce_to_zero.js b/reduce_to_zero.js deleted file mode 100644 index 0c2a40f..0000000 --- a/reduce_to_zero.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @param {number} num - * @return {number} - */ -var numberOfSteps = function(num) { - let steps = 0; - while(num !== 0) { - if(num % 2 === 0) { - num /= 2; - steps++; - } else { - num--; - steps++; - } - } - - return steps; -}; diff --git a/reverse_integer.js b/reverse_integer.js deleted file mode 100644 index 9519f31..0000000 --- a/reverse_integer.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * @param {number} x - * @return {number} - */ -var reverse = function(x) { - let isNeg; - let min = Math.pow(-2, 31); - let max = Math.pow(2, 31) - 1; - - if(x < 0) - isNeg = 1; - - x = x.toString(); - - for(let i = x.length-1; i >= 0; i--) { - x = x.concat(x[i]); - } - - x = x.substring(x.length/2, x.length); - - x = parseInt(x); - - if(x < min || x > max) - return 0; - - if(isNeg) - x = -x; - - return x; -}; diff --git a/reverse_string.js b/reverse_string.js deleted file mode 100644 index 41f9812..0000000 --- a/reverse_string.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * @param {character[]} s - * @return {void} Do not return anything, modify s in-place instead. - */ -var reverseString = function(s) { - let lngth = s.length; - let x = lngth/2; - for(let i = 0; i < x; i++) { - let temp = s[i]; - s[i] = s[lngth-1-i]; - s[lngth-1-i] = temp; - } -}; diff --git a/richest_customer_wealth.js b/richest_customer_wealth.js deleted file mode 100644 index b4bfd0b..0000000 --- a/richest_customer_wealth.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @param {number[][]} accounts - * @return {number} - */ -var maximumWealth = function(accounts) { - let maxWealth = 0; - - for (i = 0; i < accounts.length; i++) { - let wealth = accounts[i].reduce((wealth, bank) => wealth + bank); - - if (wealth > maxWealth) { - maxWealth = wealth; - } - } - - return maxWealth; -}; diff --git a/roman_to_integer.js b/roman_to_integer.js deleted file mode 100644 index 1758e79..0000000 --- a/roman_to_integer.js +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @param {string} s - * @return {number} - */ -var romanToInt = function(s) { - let sum = 0; - - let i = 0; - - while(i < s.length) { - switch(s[i]) { - case "I": - if(s[i + 1] === "V" || (s[i + 1] === "X")) - sum -= 1; - else - sum += 1; - break; - case "V": - sum += 5; - break; - case "X": - if(s[i + 1] === "L" || s[i + 1] === "C") - sum -= 10; - else - sum += 10; - break; - case "L": - sum += 50; - break; - case "C": - if(s[i + 1] === "D" || s[i + 1] === "M") - sum -= 100; - else - sum += 100; - break; - case "D": - sum += 500; - break; - case "M": - sum += 1000; - break; - } - i++; - } - - return sum; -}; diff --git a/same_tree.js b/same_tree.js deleted file mode 100644 index 1c44133..0000000 --- a/same_tree.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} p - * @param {TreeNode} q - * @return {boolean} - */ -var isSameTree = function(p, q) { - if(p === null && q === null) return true; - - if(p === null || q === null) return false; - - if(p.val !== q.val) return false; - - return isSameTree(p?.left, q?.left) && isSameTree(p?.right, q?.right); -}; diff --git a/shuffle_array.js b/shuffle_array.js deleted file mode 100644 index f32772f..0000000 --- a/shuffle_array.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * @param {number[]} nums - * @param {number} n - * @return {number[]} - */ -var shuffle = function(nums, n) { - let number = []; - - for (let i = 0; i < (2*n); i++) { - if(i % 2 === 0) { - number[i] = nums[i/2]; - } else { - number[i] = nums[n]; - n++; - - if(n === nums.length) - return number; - } - } - - return number; -}; diff --git a/shuffle_string.js b/shuffle_string.js deleted file mode 100644 index 97dc45f..0000000 --- a/shuffle_string.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @param {string} s - * @param {number[]} indices - * @return {string} - */ -var restoreString = function(s, indices) { - let newStr = []; - - indices.forEach((index, i) => { - newStr[index] = s[i]; - }); - - return newStr.join(""); -}; diff --git a/single_number.js b/single_number.js deleted file mode 100644 index 54ec68c..0000000 --- a/single_number.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @param {number[]} nums - * @return {number} - */ -var singleNumber = function(nums) { - return nums.reduce((a, b) => a ^ b) -}; diff --git a/smallest_even_multiple.js b/smallest_even_multiple.js deleted file mode 100644 index 200ed95..0000000 --- a/smallest_even_multiple.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @param {number} n - * @return {number} - */ -var smallestEvenMultiple = function(n) { - if(n % 2 === 0) - return n; - - return n * 2; -}; diff --git a/sort_array_parity.js b/sort_array_parity.js deleted file mode 100644 index d007245..0000000 --- a/sort_array_parity.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @param {number[]} A - * @return {number[]} - */ - -var sortArrayByParityII = function(A) { - let evens = [], odds = []; - - sortArray(A, evens, odds); - - if(A.includes(-1) || A.includes(-2)) - sortArray(A, evens, odds); - - return A; -}; - -let sortArray = (A, evens, odds) => { - for(let i = 0; i < A.length; i++) { - if(i % 2 === 0) { - if(A[i] % 2 !== 0) { - odds.push(A[i]); - - if(evens.length !== 0) { - A[i] = evens[evens.length - 1]; - evens.pop(); - } else - A[i] = -2; - } else { - if(A[i] === -2) { - A[i] = evens[evens.length - 1]; - evens.pop(); - } - } - } else { - if(A[i] % 2 === 0) { - evens.push(A[i]); - if(odds.length !== 0) { - A[i] = odds[odds.length - 1]; - odds.pop(); - } else - A[i] = -1; - } else { - if(A[i] === -1) { - A[i] = odds[odds.length - 1]; - odds.pop(); - } - } - } - } -} diff --git a/string_to_integer.js b/string_to_integer.js deleted file mode 100644 index 820b3ae..0000000 --- a/string_to_integer.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * @param {string} str - * @return {number} - */ - -let isNegative; -var myAtoi = function(str) { - isNegative = 0; - str = str.trim(); - - if(str[0] === "-" || str[0] === "+" || !isNaN(str[0])) { - if(str[0] === "+") { - return checkNumber(str, 1); - } else if(!isNaN(str[0])) { - return checkNumber(str, 0); - } else { - if(str[0] === "-") { - isNegative = 1; - str = str.substring(1, str.length); - - return checkNumber(str, 0); - } - } - } else { - return 0; - } -}; - -let checkNumber = (currentString, currentIndex) => { - let newNumber = ""; - let INT_MIN = Math.pow(-2, 31); - let INT_MAX = Math.pow(2, 31) - 1; - - for(let i = 0; i < currentString.length; currentIndex++, i++) { - if(isNaN(currentString.charAt(currentIndex))) - break; - - if(currentString.charAt(currentIndex) === " ") - break; - - newNumber += currentString.charAt(currentIndex); - } - - if(isNegative) - newNumber *= -1; - - if(newNumber < INT_MIN) - return INT_MIN; - else if(newNumber > INT_MAX) - return INT_MAX; - - return newNumber; -} diff --git a/subrectangle_queries.js b/subrectangle_queries.js deleted file mode 100644 index be0455e..0000000 --- a/subrectangle_queries.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @param {number[][]} rectangle - */ -var SubrectangleQueries = function(rectangle) { - this.rectCoords = rectangle; -}; - -/** - * @param {number} row1 - * @param {number} col1 - * @param {number} row2 - * @param {number} col2 - * @param {number} newValue - * @return {void} - */ -SubrectangleQueries.prototype.updateSubrectangle = function(row1, col1, row2, col2, newValue) { - for(let i = row1; i <= row2; i++) { - for(let j = col1; j <= col2; j++) { - this.rectCoords[i][j] = newValue; - } - } -}; - -/** - * @param {number} row - * @param {number} col - * @return {number} - */ -SubrectangleQueries.prototype.getValue = function(row, col) { - return this.rectCoords[row][col]; -}; - -/** - * Your SubrectangleQueries object will be instantiated and called as such: - * var obj = new SubrectangleQueries(rectangle) - * obj.updateSubrectangle(row1,col1,row2,col2,newValue) - * var param_2 = obj.getValue(row,col) - */ diff --git a/subtract_product_and_sum_integer.js b/subtract_product_and_sum_integer.js deleted file mode 100644 index 24fb002..0000000 --- a/subtract_product_and_sum_integer.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @param {number} n - * @return {number} - */ -var subtractProductAndSum = function(n) { - let numArr = [], prod = 1, sum = 0; - - let createNumArray = (x) => { - numArr.push(parseInt(x)); - } - - n.toString().split("").map(createNumArray); - - for(let i = 0; i < numArr.length; i++) { - prod *= numArr[i]; - sum += numArr[i]; - } - - return (prod-sum); -}; diff --git a/sum_1d_array.js b/sum_1d_array.js deleted file mode 100644 index deb4bf0..0000000 --- a/sum_1d_array.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -var runningSum = function(nums) { - let result = []; - - result[0] = nums[0]; - - for(let i = 1; i < nums.length; i++) - result.push(nums[i] + result[i-1]); - - return result; -}; diff --git a/to_lower_case.js b/to_lower_case.js deleted file mode 100644 index 08202e6..0000000 --- a/to_lower_case.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {string} str - * @return {string} - */ -var toLowerCase = function(str) { - let result = ""; - - for(let i = 0; i < str.length; i++) { - if(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) { - let code = str.charCodeAt(i); - let char = String.fromCharCode(code + 32); - - result += char; - } else - result += str[i]; - } - - return result; -}; diff --git a/two_sum_ii.js b/two_sum_ii.js deleted file mode 100644 index d7b5b69..0000000 --- a/two_sum_ii.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @param {number[]} numbers - * @param {number} target - * @return {number[]} - */ -var twoSum = function(numbers, target) { - let left = 0; - let right = numbers.length - 1; - - while(left <= right) { - let sum = numbers[left] + numbers[right]; - if (sum === target) { - return [left + 1, right + 1]; - } else if (sum < target) { - left++; - } else { - right--; - } - } -}; diff --git a/twosum.js b/twosum.js deleted file mode 100644 index a16c3f1..0000000 --- a/twosum.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number[]} - */ -var twoSum = function(nums, target) { - for(let i = 0; i < nums.length; i++) { - for(let j = i; j < nums.length; j++) { - if(nums[i] + nums[j] == target) { - if(i != j) { - return [i, j]; - } - } - } - } -}; diff --git a/unique_occurrences.js b/unique_occurrences.js deleted file mode 100644 index 0d15c0b..0000000 --- a/unique_occurrences.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @param {number[]} arr - * @return {boolean} - */ -var uniqueOccurrences = function(arr) { - let occ = [], uniq = []; - - arr = arr.sort(); - uniq = arr.filter((item, index) => !arr.includes(item, index + 1)); - - for(let i = 0 ; i < uniq.length; i++) { - let count = arr.filter(item => item === uniq[i]).length; - occ[i] = count; - } - - occ = occ.sort(); - - for(let i = 0; i < occ.length; i++) { - if(occ[i] === occ[i + 1]) - return false; - } - - return true; -}; diff --git a/valid_anagram.js b/valid_anagram.js deleted file mode 100644 index 21d8533..0000000 --- a/valid_anagram.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * @param {string} s - * @param {string} t - * @return {boolean} - */ -var isAnagram = function(s, t) { - if(JSON.stringify(Array.from(s).sort()) === JSON.stringify(Array.from(t).sort())) - return true; - else - return false; -}; diff --git a/valid_number.js b/valid_number.js deleted file mode 100644 index 3d00351..0000000 --- a/valid_number.js +++ /dev/null @@ -1,104 +0,0 @@ -/** - * @param {string} s - * @return {boolean} - */ -let hasADot = 0; -let hasAnE = 0; - -var isNumber = function(s) { - hasADot = 0; - hasAnE = 0; - s = s.trim(); - return validateForFirstCharacter(s); -}; - -let validateForFirstCharacter = (s) => { - if(validateFirstCharacter(s[0])) { - if(!isNaN(s[0])) - return validateForNumber(s, 0); - else if(s[0] === "+" || s[0] === "-") - return validateForSign(s, 0); - else if(s[0] === ".") - return validateForDot(s, 0); - else - return false; - } else - return false; -} - - -let validateFirstCharacter = (fc) => { - return (!isNaN(fc) || fc === "+" || fc === "-" || fc === "." || fc === " "); -} - -let validateForNumber = (currentString, currentIndex) => { - let nextCharacter = currentString[currentIndex + 1]; - - if(nextCharacter === undefined) - return true; - else if(nextCharacter === " ") - return false; - else if(nextCharacter === ".") - return validateForDot(currentString, currentIndex + 1); - else if(nextCharacter === "e") - return validateForE(currentString, currentIndex + 1); - else if(!isNaN(nextCharacter)) - return validateForNumber(currentString, currentIndex + 1); - else - return false; -} - -let validateForSign = (currentString, currentIndex) => { - let nextCharacter = currentString[currentIndex + 1]; - - if(nextCharacter === "." && !isNaN(currentString[currentIndex + 2])) - return validateForDot(currentString, currentIndex + 1); - else if(!isNaN(nextCharacter) && nextCharacter !== " ") - return validateForNumber(currentString, currentIndex + 1); - else - return false; -} - -let validateForDot = (currentString, currentIndex) => { - let nextCharacter = currentString[currentIndex + 1]; - - if(hasADot) - return false; - - hasADot = 1; - - if(hasAnE) - return false; - - if(nextCharacter === undefined) { - if(currentIndex === 0) - return false; - else - return true; - } else if(!isNaN(nextCharacter) && nextCharacter !== " ") - return validateForNumber(currentString, currentIndex + 1); - else if(nextCharacter === "e") { - if(currentString[currentIndex - 1] === undefined) - return false; - - return validateForE(currentString, currentIndex + 1); - } - else - return false; -} - -let validateForE = (currentString, currentIndex) => { - let nextCharacter = currentString[currentIndex + 1]; - - if(hasAnE) - return false; - - hasAnE = 1; - - if(nextCharacter === "+" || nextCharacter === "-") { - return validateForSign(currentString, currentIndex + 1); - } else if(!isNaN(nextCharacter)) { - return validateForNumber(currentString, currentIndex + 1); - } else - return false; -} diff --git a/valid_palindrome.js b/valid_palindrome.js deleted file mode 100644 index 67b32ce..0000000 --- a/valid_palindrome.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {string} s - * @return {boolean} - */ -var isPalindrome = function(s) { - s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase(); - - let left = 0; - let right = s.length - 1; - - while(left <= right) { - if (s[left] === s[right]) { - left++; - right--; - } else return false; - } - - return true; -}; diff --git a/valid_parentheses.js b/valid_parentheses.js deleted file mode 100644 index b1c8da5..0000000 --- a/valid_parentheses.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * @param {string} s - * @return {boolean} - */ -var isValid = function(s) { - const brackets = { - "(": ")", - "{": "}", - "[": "]" - }; - - let test = []; - - if(s.length % 2 !== 0) return false; - - if(s[0] === ")" || s[0] === "]" || s[0] === "}") return false; - - if(s[s.length - 1] === "(" || s[s.length - 1] === "[" || s[s.length - 1] === "{") return false; - - for(let i = 0; i < s.length; i++) { - if(s[i] === "(" || s[i] === "[" || s[i] === "{") test.push(s[i]); - else if(brackets[test.pop()] !== s[i]) return false; - } - - return test.length === 0; -}; diff --git a/words_containing_char.js b/words_containing_char.js deleted file mode 100644 index bb75536..0000000 --- a/words_containing_char.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @param {string[]} words - * @param {character} x - * @return {number[]} - */ -var findWordsContaining = function(words, x) { - let indices = []; - - for(let i = 0; i < words.length; i++) { - if(words[i].includes(x)) - indices.push(i); - } - - return indices; -}; diff --git a/xor_array.js b/xor_array.js deleted file mode 100644 index aed0a82..0000000 --- a/xor_array.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * @param {number} n - * @param {number} start - * @return {number} - */ -var xorOperation = function(n, start) { - let total = 0; - - for(let i = 0; i < n; i++) - total ^= start + 2 * i; - - return total; -};