Given an arrayContinue reading “[Leetcode]15. 3Sum”nums
of n integers, are there elements a, b, c innums
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] ]
[Leetcode]1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].Continue reading “[Leetcode]1. Two Sum”
[Leetcode]1498. Number of Subsequences That Satisfy the Given Sum Condition
Given an array of integersContinue reading “[Leetcode]1498. Number of Subsequences That Satisfy the Given Sum Condition”nums
and an integertarget
. Return the number of non-empty subsequences ofnums
such that the sum of the minimum and maximum element on it is less or equal thantarget
. Since the answer may be too large, return it modulo 10^9 + 7. Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
[Leetcode]1497. Check If Array Pairs Are Divisible by k
Given an array of integersContinue reading “[Leetcode]1497. Check If Array Pairs Are Divisible by k”arr
of even lengthn
and an integerk
. We want to divide the array into exactlyn / 2
pairs such that the sum of each pair is divisible byk
. Return True If you can find a way to do that or False otherwise. Example 1: Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
Floyd’s Cycle Detection Algorithm – [Leetcode]142. Linked List Cycle II
The cycle detection problem is to find the cycle in a sequence, and Floyd’s cycle detection algorithm, aka Tortoise and Hare algorithm, is a two pointers algorithm to detect the cycle and the start of the cycle as well.
Continue reading “Floyd’s Cycle Detection Algorithm – [Leetcode]142. Linked List Cycle II”[Leetcode]957. Prison Cells After N Days
There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: 1. If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. 2. Otherwise, it becomes vacant. (Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.) We describe the current state of the prison in the following way:Continue reading “[Leetcode]957. Prison Cells After N Days”cells[i] == 1
if thei
-th cell is occupied, elsecells[i] == 0
. Given the initial state of the prison, return the state of the prison afterN
days (andN
such changes described above.)
[Leetcode]212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. Example: Input: board = [ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ] words =Continue reading “[Leetcode]212. Word Search II”["oath","pea","eat","rain"]
Output:["eat","oath"]
Intro to Trie (Prefix Tree) – FEAT. Leetcode 208. Implement Trie
A trie, or a prefix tree, refers to an ordered data structure that is usually used to store strings (Wikipedia). The picture on the above is an example where a trie stores [ape,apple,are,art]
. Now let’s take a closer look at it and it’s not hard to find that: