547. Number of Provinces

![https://leetcode.com/problems/number-of-provinces/] There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c. A province is a group of directly or indirectly connected cities and no other cities outside of the group. You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise....

<span title='2021-01-16 00:00:00 +0000 UTC'>January 16, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

684. Redundant Connection

![https://leetcode.com/problems/redundant-connection/] In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed. The resulting graph is given as a 2D-array of edges....

<span title='2021-01-16 00:00:00 +0000 UTC'>January 16, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

1010. Pairs of Songs With Total Durations Divisible by 60

You are given a list of songs where the ith song has a duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Example 1: Input: time = [30,20,150,100,40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120 (time[1] = 20, time[4] = 40): total duration 60 Example 2: Input: time = [60,60,60] Output: 3 Explanation: All three pairs have a total duration of 120, which is divisible by 60....

<span title='2021-01-02 00:00:00 +0000 UTC'>January 2, 2021</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” 1 2 3 4 5 Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3....

<span title='2020-11-12 00:00:00 +0000 UTC'>November 12, 2020</span>&nbsp;·&nbsp;5 min&nbsp;·&nbsp;volyx

Regions Cut By Slashes

An a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, , or blank space. These characters divide the square into contiguous regions. (Note that backslash characters are escaped, so a \ is represented as “\”.) Return the number of regions. Example 1: 1 2 3 4 5 6 7 Input: [ " /", "/ " ] Output: 2 Explanation: The 2x2 grid is as follows: Example 2:...

<span title='2020-11-08 00:00:00 +0000 UTC'>November 8, 2020</span>&nbsp;·&nbsp;7 min&nbsp;·&nbsp;volyx

Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters. Example 1: 1 2 3 Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: 1 2 3 Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: 1 2 3 4 Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3....

<span title='2020-11-07 00:00:00 +0000 UTC'>November 7, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Buddy Strings

Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at A[i] and A[j]. For example, swapping at indices 0 and 2 in “abcd” results in “cbad”. Example 1: Input: A = “ab”, B = “ba” Output: true Explanation: You can swap A[0] = ‘a’ and A[1] = ‘b’ to get “ba”, which is equal to B....

<span title='2020-10-28 00:00:00 +0000 UTC'>October 28, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: 1 2 Input: 4 Output: 2 Example 2: 1 2 Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842…, and since the decimal part is truncated, 2 is returned....

<span title='2020-10-28 00:00:00 +0000 UTC'>October 28, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (i.e. xn). Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 Example 3: Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Constraints: -100.0 < x < 100.0 -231 <= n <= 231-1 -104 <= xn <= 104...

<span title='2020-10-27 00:00:00 +0000 UTC'>October 27, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 1 2 3 4 5 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: 1 2 3 4 5 [ [3], [20,9], [15,7] ] Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 /** * Definition for a binary tree node....

<span title='2020-10-21 00:00:00 +0000 UTC'>October 21, 2020</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;volyx

Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size. It’s guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique....

<span title='2020-10-18 00:00:00 +0000 UTC'>October 18, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: 1 2 Input: [1,2,3] Output: 6 Example 2: 1 2 Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer. Solution:...

<span title='2020-10-17 00:00:00 +0000 UTC'>October 17, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Time Needed to Inform All Employees

A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID. Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it’s guaranteed that the subordination relationships have a tree structure. The head of the company wants to inform all the employees of the company of an urgent piece of news....

<span title='2020-09-16 00:00:00 +0000 UTC'>September 16, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations. Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivelent or false otherwise....

<span title='2020-09-07 00:00:00 +0000 UTC'>September 7, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 1 2 3 4 5 6 7 8 Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II....

<span title='2020-09-06 00:00:00 +0000 UTC'>September 6, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx