lek-tin:

Remove Duplicates From Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1 Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length.

by lek tin in "algorithm" access_time 2-min read

3sum Closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

by lek tin in "algorithm" access_time 1-min read

3sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution # time: o(n^2) # space: o(1) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: results = [] if not nums: return results nums.

by lek tin in "algorithm" access_time 2-min read

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1 nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2 nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.

by lek tin in "algorithm" access_time 3-min read

Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head. Example Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note Given n will always be valid. Follow-up Could you do this in one pass? Solution # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.

by lek tin in "algorithm" access_time 1-min read

Search in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to search. If found in the array return true, otherwise return false. Example 1: Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true Example 2: Input: nums = [2,5,6,0,0,1,2], target = 3 Output: false Follow-up This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.

by lek tin in "algorithm" access_time 2-min read

When You New a Class in JavaScript?

Here is an example of instantiating a class using the new operator: class Person { constructor(name, age) { this.name = name; this.age = age; } } const asian = new Person('Alice', 25); console.log(asian); As you can see from below, asian is not a class, it is instead, an object: [object Object] { age: 25, name: "Alice" } The values of asian are set by the constructor of the Person class.

by lek tin in "javascript" access_time 1-min read

Happy Number

Write an algorithm to determine if a number is “happy”. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

by lek tin in "algorithm" access_time 1-min read