Design a phone directory that initially has maxNumbers empty slots that can store numbers. The directory should store numbers, check if a certain slot is empty or not, and empty a given slot. Implement the PhoneDirectory class: PhoneDirectory(int maxNumbers) Initializes the phone directory with the number of available slots maxNumbers.int get() Provides a number that ...

You are given an integer length and array updates where updates[i] = [startIdxi, endIdxi, inci]. You have an array “arr” of length with all zeros, and you have some operation to apply on arr. In the ith operation, you should increment all the elements arr[startIdxi], arr[startIdxi + 1], …, arr[endIdxi] by inci. Return arr after ...

A confusing number is a number that, when rotated 180 degrees, becomes a different number with each digit valid. We can rotate the digits of a number by 180 degrees to form new digits. When 0, 1, 6, 8, and 9 are rotated 180 degrees, they become 0, 1, 9, 8, and 6, respectively.When 2, ...

Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. https://leetcode.com/problems/word-pattern/ class Solution { public boolean wordPattern(String pattern, String s) { String[] sArr = s.split(" "); Map<Character, String> map = new HashMap<>(); char[] pArr = pattern.toCharArray(); //if the length of ...

A row-sorted binary matrix means that all elements are 0 or 1 and each row of the matrix is sorted in non-decreasing order.Given a row-sorted binary matrix binaryMatrix, return the index (0-indexed) of the leftmost column with a 1 in it. If such an index does not exist, return -1.You can’t access the Binary Matrix ...

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order. The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]). https://leetcode.com/problems/all-paths-from-source-to-target/description/ class Solution { public List<List<Integer>> allPathsSourceTarget(int[][] ...

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

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

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

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