Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1 | Two Sum | Easy
- Return indices of 2 numbers from array adding to given sum.
- console.log(twoSum([2, 7, 11, 15],9)); // [ 0, 1 ]
- */
- // A-I
- let twoSum = (arr, sum) => {
- const seen = {};
- for (let i = 0; i < arr.length; i++) {
- const number = arr[i];
- const diff = sum - arr[i];
- if (seen[diff] !== undefined) {
- return [seen[diff], i];
- }
- seen[number] = i;
- }
- };
- // console.log(twoSum([2,8,3,7],9)); // [ 0, 3 ]
- // A-II
- let twoSum2 = function(arr, sum) {
- const seen = new Set();
- for (let i = 0; i < arr.length; i++) {
- const number = arr[i];
- const diff = sum - arr[i];
- if (seen.has(diff)) {
- return [arr.indexOf(diff), i]
- }
- seen.add(number);
- }
- };
- // console.log(twoSum2([2,8,3,7],9)); // [ 0, 3 ]
- /*
- 2 | Add Two Numbers | Medium
- Given two linked lists of integers with digits stored in reverse order, Add two numbers and return the sum as a linked list.
- You may assume the two numbers do not contain any leading zero, except the number 0 itself.
- Input: l1 = [2,4,3], l2 = [5,6,4]
- Output: [7,0,8] // 342 + 465 = 807
- */
- // A-I
- /** @param {ListNode} l1 | @param {ListNode} l2 | @return {ListNode} */
- var addTwoNumbers = function(l1, l2) {
- let carry = 0;
- let digitSum = 0;
- let head = new ListNode();
- let now = head;
- while (l1 || l2) {
- digitSum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
- carry = digitSum > 9 ? 1 : 0;
- now.next = new ListNode(digitSum % 10);
- now = now.next;
- l1 = l1 ? l1.next : null;
- l2 = l2 ? l2.next : null;
- }
- if (carry) now.next = new ListNode(carry);
- return head.next;
- };
- // Time complexity : O(max(m,n)). Space complexity : O(max(m,n)).
- // console.log(listToArray(addTwoNumbers(arrToList([2,4,3]), arrToList([5,6,4])))); // [ 7, 0, 8 ]
- // listToArray(addTwoNumbers(arrToList([9,9,9,9,9,9,9]), arrToList([9,9,9,9]))); // [8,9,9,9,0,0,0,1]
- // Helper functions
- function ListNode(val, next) {
- this.val = (val ? val : 0)
- this.next = (next ? next : null);
- }
- function listToArray(list) {
- let arr = []; // Initialise an array to return
- while (list) {
- arr = [...arr, list.val];
- list = list.next;
- };
- return arr;
- }
- function arrToList (arr) {
- let list = null;
- for(i=0;i<arr.length;i++){
- list = {val: arr[i], next: arrToList(arr.slice(i+1,arr.length))}
- }
- return list;
- }
- /*
- 3 | Longest Substring Without Repeating Characters | Medium
- Return length of Longest Substring
- Input: s = "abcabcbb" Substring = "abc", "bca", "cab" so Output: 3
- Input: s = "pwwkew" Substring = "wke", "kew" so Output: 3. Substring != pwke, as substrings are "pw" and "wke", resetting when "w" repeats.
- */
- // A-I
- /** @param {string} s| @return {number} */
- let subStrL = function(str) {
- let map = {};
- let max = 0;
- let start = 0;
- for (let position = 0; position < str.length; position++) {
- const charc = str[position];
- if (map[charc] !== undefined) {
- start = Math.max(start, map[charc] + 1); // start from next charcter after duplicate charc
- }
- map[charc] = position;
- max = Math.max(max, position - start + 1); // current length position - start + 1
- }
- return max;
- };
- // Time complexity : O(n). Space complexity : O(1).
- // console.log(subStrL("pwwkew")); // 3
- 8 | String to Integer (atoi) | Medium
- Implement function which converts a string to a 32-bit signed integer capped in range [-231, 231 - 1].
- Ignore leading whitespaces and disregard characters other than +, - and numbers.
- Input: s = "42" Output: 42 | Input: s = " -42" Output: -42 | Input: s = "4193 with words" Output: 4193
- Input: s = "words and 987" Output: 0 | Input: s = "-91283472332" Output: -2147483648
- // A-I
- /** @param {string} str | @return {number} */
- let myAtoi = function(str) {
- let i = 0;
- let sign = 1;
- let res = 0;
- // str = str.trim();
- let len = str.length;
- let INT_MAX = Math.pow(2, 31) - 1;
- let INT_MIN = - Math.pow(2, 31); // 2147483648
- while (str[i] === ' ') i++; // str.trim();
- if (str[i] === '+' || str[i] === '-') {
- sign = str[i] === '+' ? 1 : -1;
- i++;
- }
- while (str[i] >= '0' && str[i] <= '9') {
- res = (res * 10) + (str[i] - 0);
- if (sign === 1 && res > INT_MAX) return INT_MAX;
- if (sign === -1 && res > INT_MIN) return INT_MIN;
- i++;
- }
- return res * sign;
- };
- // Time complexity : O(n). Space complexity : O(1).
- console.log(myAtoi("-2147483649")); // -2147483648
- /*
- 11 | Container with most water |
- Return max amount of water container can store, given an array of numbers which represent heights.
- */
- var maxArea = function(height) {
- var max = 0;
- var l = 0; // left
- var r = height.length - 1; // right
- while (l < r) {
- max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
- if (height[l] < height[r]) {
- l++;
- } else {
- r--;
- }
- }
- return max;
- };
- // Time complexity : O(n) | Space complexity : O(1).
- // console.log(maxArea([1,8,6,2,5,4,8,3,7])); // 49
- // console.log(maxArea([1,1])); // 1
- /** 12 2 @param {number} num | @return {string} */
- var intToRoman1 = function(num) {
- var str = [
- ['I', 'V'], // 1s: 1, 5
- ['X', 'L'], // 10s: 10, 50
- ['C', 'D'], // 100s: 100, 500
- ['M'] // 1000s: 1000
- ];
- var res = '';
- var i = 0;
- var digit = 0;
- for ( let i = 0 ; num > 0; i++) {
- digit = num % 10;
- if (digit === 9) {
- res = str[i][0] + str[i + 1][0] + res;
- } else if (digit >= 5) { // 6, 7, 8
- res = str[i][1] + str[i][0].repeat(digit - 5) + res;
- } else if (digit === 4) {
- res = str[i][0] + str[i][1] + res;
- } else { // 0, 1, 2, 3
- res = str[i][0].repeat(digit) + res;
- }
- num = Math.floor(num / 10);
- // i++;
- }
- return res;
- };
- // Time complexity : O(log(n)) | Space complexity : O(1).
- // console.log(intToRoman1(402)); // CDII
- // console.log(intToRoman1(3000)); // MMM
- // console.log(intToRoman1(93)); // XCIII
- // console.log(intToRoman1(48)); // XLVIII
- // console.log(intToRoman1(4)); // IV
- // console.log(intToRoman1(900)); // CMXCIX
- // console.log('Hi'.repeat(2)) // HiHi
- // console.log('Hi'.repeat(1)) // Hi
- // console.log('Hi'.repeat(0)) //
- /** 12 @param {number} num | @return {string} */
- var intToRoman2 = function(num) {
- var map = [
- [1, "I"],
- [4, "IV"],
- [5, "V"],
- [9, "IX"],
- [10, "X"],
- [40, "XL"],
- [50, "L"],
- [90, "XC"],
- [100, "C"],
- [400, "CD"],
- [500, "D"],
- [900, "CM"],
- [1000, "M"]
- ];
- var res = '';
- var i = 12;
- var tmp = 0;
- while (num > 0) {
- res += map[i][1].repeat(Math.floor(num / map[i][0]));
- num %= map[i][0];
- console.log(map[i][0]);
- i--;
- }
- return res;
- };
- //Time complexity : O(log(n)) | Space complexity : O(1).
- // console.log(intToRoman2(402)); // CDII
- // console.log(intToRoman2(3000)); // MMM
- // console.log(intToRoman2(93)); // XCIII
- // console.log(intToRoman2(4)); // IV
- 189. WIP
- let nums = [1,2,3,4,5,6,7], k = 3;
- const numsLength = nums.length;
- let newArr = [];
- let mapArr = nums.map((element, index) => {
- if(index < (numsLength - k)){
- newArr[(numsLength - 1) - (numsLength - k)] = element;
- }else{
- newArr[(index -1) - (numsLength - k)] = element;
- }
- });
- console.log(numsLength, newArr);
- =========================================================
- 1281 |Subtract the Product and Sum of Digits of an Integer | Easy
- Given an integer number n, return the difference between the product of its digits and the sum of its digits.
- var subtractProductAndSum = function(n) {
- // Initialize the sum and the product with neutral values
- var sum = 0;
- var product = 1;
- while (n > 0) {
- var digit = n % 10;
- sum += digit;
- product *= digit;
- n = Math.floor(n / 10);
- }
- return product - sum;
- };
- const firstTen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- // console.log(firstTen.some((element) => {
- // return element % 2 === 0 }
- // )); // true
- const even = (element) => element % 2 === 0;
- // console.log(firstTen.some(even)); // true
- const fibonacciElements = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
- function fibonacci(num) {
- if(num < 2) {
- return num;
- }
- else {
- return fibonacci(num-1) + fibonacci(num - 2);
- }
- }
- // console.log('fibonacci', fibonacci(3)); // 2
- // for(let i = 0; i < 5; i++) {
- // console.log(fibonacci(i));
- // }
- /*
- 0
- 1
- 1
- 2
- 3
- */
- // Pagination
- const pagination = () => {
- const paginationReference = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- let paginatedData = [];
- let pageSize = 2;
- const dataPages = Math.ceil(paginationReference.length/ pageSize);
- for(let i = 0; i < 10; i += pageSize) {
- paginatedData.push([...paginationReference.slice(i, i+ pageSize)]);
- }
- }
- let strForNumber = "234";
- // console.log(strForNumber[1]); // 3
- // console.log(strForNumber[2]); // 4
- // console.log("234"[2]) // 4
- // console.log(5 * 10); // 50
- // console.log((strForNumber[2] - 0)); // 4
- // console.log((5 * 10) + (strForNumber[2] - 0)); // 54
- // console.log(typeof((5 * 10) + (strForNumber[2] - 0))); // number
- let numbersToSort = [4, 2, 5, 1, 3];
- numbersToSort.sort((a, b) => a - b);
- // console.log('sorted numbers', numbersToSort); // sorted numbers [ 1, 2, 3, 4, 5 ]
- let valueToFilter = 3
- let arrToFilter = [1, 2, 3, 4, 5, 3]
- arrFltered1 = arrToFilter.filter(item => item !== valueToFilter)
- arrFiltered2 = arrToFilter.filter(item => {return item !== valueToFilter})
- // console.log('after filter', arrFltered1); // after filter [ 1, 2, 4, 5 ]
- // console.log('after filter', arrFiltered2); // after filter [ 1, 2, 4, 5 ]
- const animals = ['pigs', 'goats', 'sheep'];
- // console.log(animals.push('chickens', 'cats', 'dogs')); // 6
- // console.log(animals.pop()); // dogs
- [aRest, bRest] = [10, 20];
- // console.log(aRest);// 10
- // console.log(bRest);// 20
- const [aResting, bResting, ...resting] = [10, 20, 30, 40, 50];
- // console.log(resting);// [30,40,50]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement