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 ...

Leetcode 1834: Single-Threaded CPU

You are given n​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the i​​​​​​th​​​​ task will be available to process at enqueueTimei and will take processingTimeito finish processing. You have a single-threaded CPU that can process at most one task at a time and will act in the following way: If the CPU is idle ...

Protected access modifier in Java

As per the official java documentation from Oracle: “The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.” What this means is: Say, a concrete class Parent in package com.coddicted has a protected method named ...

Java Streams : Convert List of Strings to Single Comma separated String

In this short post, we’d explain how to convert/ reduce a List of Strings into a single comma (,) separated String using Java-8 Streams API. here’s the code: import java.util.Arrays; import java.util.List; public class ListToString { public static String getCommaSeparatedString(List<string> input) { return input.stream() .reduce( // identity function for String concatenation "", (s1, s2) -> ...

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) ...

Maximum Product of Word Lengths

Title: Maximum Product of Word Lengths Source: leetcode.com Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example 1: Input: [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”] Output: ...

Power of Three

Title: Power of Three Source: leetcode.com Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? Java solution Java /*https://leetcode.com/problems/power-of-three/*/ public class PowerOfThree { public boolean isPowerOfThree(int n) { if(n==0) return false; while(n!=1) { //System.out.println(n); int remainder = ...

Decode Ways 2

Title: Decode Ways Source: leetcode.com A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 1234 'A' -> 1'B' -> 2...'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, ...

Verify Preorder Serialization of a Binary Tree 4

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ ...

Implement Reverse DNS Look Up Cache

Title: How to Implement Reverse DNS Look Up Cache? Source: www.geeksforgeeks.org Reverse DNS look up is using an internet IP address to find a domain name. For example, if you type 74.125.200.106 in browser, it automatically redirects to google.in. How to implement Reverse DNS Look Up cache? Following are the operations needed from cache. 1) ...