leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8247992a3f2590d0afd4cc3c153c2480a100b653
parent dde83509e906793b29a4acd1950ffa9554462083
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Thu, 27 Apr 2023 09:54:07 -0500

Completed bulb switcher using js

Diffstat:
Abulb-switcher/bulb-switcher.js | 26++++++++++++++++++++++++++
Atwo-sums/two-sum.js | 25+++++++++++++++++++++++++
2 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/bulb-switcher/bulb-switcher.js b/bulb-switcher/bulb-switcher.js @@ -0,0 +1,26 @@ +//Create the algorithm to switch bulbs from on to off. +//This is done by creating an array of length n. From there +//you need to iterate through the list and switch each value +//that is divisible by the current index then return the number of on +//switches. The time complexity of this code is O(n * sqrt(n)) +//Runtime: 1801ms Beats: 5.17% +//Memory: 121.8MB Beats: 6.90% +/** + * @param {number} n + * @return {number} + */ +var bulbSwitch = function(n) { + let bulbs = Array(n).fill(1); + for(let i = 1; i < n ; ++i){ + for(let x = i ; x < n ; x += i + 1){ + bulbs[x] = bulbs[x] * -1; + } + } + let on = 0; + for(let i = 0 ; i < bulbs.length ; ++i){ + if(bulbs[i] == 1){ + on += 1; + } + } + return on; +}; diff --git a/two-sums/two-sum.js b/two-sums/two-sum.js @@ -0,0 +1,25 @@ +//This is two sum hashtable solution using javascript. +//This algorithm has a worst case time complexity of O(n) where +//n is the number of values in the nums array. +//Time: 58ms Beats: 92.92% +//Memory: 42.8MB Beats: 39.35% + + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + const my_map = {}; + for(let i = 0 ; i < nums.length ; ++i){ + + let diff = target - nums[i]; + if(my_map[diff] == null){ + my_map[nums[i]] = i; + } + else{ + return [i , my_map[diff]]; + } + } +};