Leetcode 1167: Minimum Cost to Connect Sticks

You have some number of sticks with positive integer lengths. These lengths are given as an array sticks, where sticks[i] is the length of the ith stick. You can connect any two sticks of lengths x and y into one stick by paying a cost of x + y. You must connect all the sticks until there is only one stick remaining. Return the minimum cost of connecting ...

Increasing Order Search Tree

Title: Increasing Order Search Tree Source: leetcode.com Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child (Increasing order search tree). Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] 5 / \ 3 6 ...

Find all Anagrams in a String 2

Title: Custom Sort String Source: leetcode.com Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Example ...

Custom Sort String

Title: Custom Sort String Source: leetcode.com S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before ...

Monotonic Array

Title: Monotonic Array Source: leetcode.com An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if the ...

Binary Watch Problem

Title: Binary Watch Source: leetcode.com A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right. For example, the above binary watch reads “3:25”. Given a ...

Leaf-Similar trees

Title: Leaf-Similar TreesSource: leetcode.com Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence ...

Shortest Distance to a Character

Title: Shortest Distance to a CharacterSource: leetcode.com Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = “loveleetcode”, C = ‘e’ Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note: 1. ...

Positions of Large Groups

Title: Positions of Large Groups Source: leetcode.com In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”. Call a group large if it has 3 or more characters. We would like the ...

N-ary Tree PreOrder Traversal 2

Title: N-ary Tree Preorder Traversal Source: leetcode.com Given an n-ary tree, return the preorder traversal of its nodes’ values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2,4]. Note: Recursive solution is trivial, could you do it iteratively? You could find another explanation/ problem related to Preorder Traversal here. Java solution (Recursive) ...