In this post we will solve LeetCode 681. Next Closest Time problem using the Java programming language, the VSCode IDE on a Windows computer. Please note that the computer platform and the IDE should make no difference. One of the simplest ways to solve the problem is to use the online IDE provided by LeetCode.
Given a time represented in the format "HH:MM",
form the next closest time by reusing the current digits.
There is no limit on how many times a digit can be reused.
You may assume the given input string is always valid.
For example, "01:34", "12:09" are all valid.
"1:34", "12:9" are all invalid.
* time.length == 5
* time is a valid time in the form "HH:MM".
o 0 <= HH < 24
o 0 <= MM < 60
We are given a string with a time expressed in 24-hour format. By reusing the digits as many times as needed, we need to form the next closest time. Continue reading “LeetCode 681. Next Closest Time in Java”
In this post we will solve LeetCode 84. Largest Rectangle in Histogram problem using the Java programming language and the VSCode IDE on a Windows computer.
Given an array of integers heights representing the histogram's bar height where the width of each bar is 1,
return the area of the largest rectangle in the histogram.
o 1 <= heights.length <= 10^5
o 0 <= heights[i] <= 10^4
* Monotonic Stack
We are given an array representing a histogram. The width of each bar holding a value is 1 unit. We are asked to return the area of the largest rectangle in the histogram. Continue reading “LeetCode 84. Largest Rectangle in Histogram in Java”
In this post I will solve LeetCode 1804. Implement Trie II (Prefix Tree) problem using Java and the VSCode IDE on a Windows computer.
In a previous post we discussed and implemented a solution for LeetCode 208. Implement Trie (Prefix Tree) in Java which is similar yet different to this one.
Please note that I carried away experimenting with the current problem as I did with the previous one. Due to this I implemented two solutions. After the first one was accepted I tried improving performance and readability. That created a second solution. Due to my approach I ended up with a large amount of code which I will post and comment on. For the first implementation I will go light on comments. Continue reading “LeetCode 1804. Implement Trie II (Prefix Tree) in Java”
In this post I will solve the LeetCode 208 Implement Trie (Prefix Tree) problem using the Java programming language and the VSCode IDE on a Windows computer. After the code was accepted by LeetCode I decided to add a couple features. The first one is to provide a method to access one of the main uses for a Trie (https://en.wikipedia.org/wiki/Trie). The second, which is based on the first one, is used to display, in order, the words inserted into the Trie.
A trie (pronounced as "try") or prefix tree is a tree data structure
used to efficiently store and retrieve keys in a dataset of strings.
There are various applications of this data structure,
such as autocomplete and spellchecker.
Implement the Trie class:
Initializes the trie object.
* void insert(String word)
Inserts the string word into the trie.
* boolean search(String word)
Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
* boolean startsWith(String prefix)
Returns true if there is a previously inserted string word that has the prefix prefix,
and false otherwise.
o 1 <= word.length, prefix.length <= 2000
o word and prefix consist only of lowercase English letters.
o At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.
o Hash Table
The requirements are very well described. We need to implement the class Trie which must include a specific set of methods. In addition we need to specify the class members. Continue reading “LeetCode 208. Implement Trie (Prefix Tree) in Java”
In this post we will solve LeetCode 760. Find Anagram Mappings problem using the Java programming language.
You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1.
Both arrays may contain duplicates.
Return an index mapping array mapping from nums1 to nums2 where
mapping[i] = j means the ith element in nums1 appears in nums2 at index j.
If there are multiple answers, return any of them.
An array a is an anagram of an array b means b is made by randomizing the order of the elements in a.
o 1 <= nums1.length <= 100
o nums2.length == nums1.length
o 0 <= nums1[i], nums2[i] <= 10^5
o nums2 is an anagram of nums1.
o Hash Table
We are provided with two int holding two anagrams. We need to write a function that returns an index mapping array mapping from nums1 to nums2 where mapping[i] = j means the ith element in nums1 appears in nums2 at index j. Continue reading “LeetCode 760. Find Anagram Mappings in Java”
In this post we will solve LeetCode 1518. Water Bottles problem using the Java programming language.
There are numBottles water bottles that are initially full of water.
You can exchange numExchange empty water bottles from the market with one full water bottle.
The operation of drinking a full water bottle turns it into an empty bottle.
Given the two integers numBottles and numExchange,
return the maximum number of water bottles you can drink.
o 1 <= numBottles <= 100
o 2 <= numExchange <= 100
Before you start solving a problem associated with a website make sure you read the current description directly from the site in order to get the current description of the problem. On occasions descriptions may be updated providing additional information that may help you come up with a better solution faster. Continue reading “LeetCode 1518. Water Bottles in Java”
In this post I will be solving LeetCode 160 Intersection of Two Linked Lists using the Java programming language.
Given the heads of two singly linked-lists headA and headB,
return the node at which the two lists intersect.
If the two linked lists have no intersection at all, return null.
The inputs to the judge are given as follows (your program is not given these inputs):
o intersectVal - The value of the node where the intersection occurs.
This is 0 if there is no intersected node.
o listA - The first linked list.
o listB - The second linked list.
o skipA - The number of nodes to skip ahead in listA (starting from the head)
to get to the intersected node.
o skipB - The number of nodes to skip ahead in listB (starting from the head)
to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads,
headA and headB to your program.
If you correctly return the intersected node,
then your solution will be accepted.
o The number of nodes of listA is in the m.
o The number of nodes of listB is in the n.
o 1 <= m, n <= 3 * 104
o 1 <= Node.val <= 105
o 0 <= skipA < m
o 0 <= skipB < n
o intersectVal is 0 if listA and listB do not intersect.
o intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
* Hash Table
o Linked List
* Two Pointers
* Could you write a solution that runs in O(m + n) time and use only O(1) memory?
If interested I suggest you get the current description from LeetCode before you start coding. Continue reading “LeetCode 160 Intersection of Two Linked Lists in Java”
In this post we will solve LeetCode 171Excel Sheet Column Number problem using the Java programming language.
If interested in this problem, I suggest you go to the LeetCode website and read the description of the problem in case something has changed. If you are still interested please read on.
Given a string columnTitle that represents the column title as appear in an Excel sheet,
return its corresponding column number.
A -> 1
B -> 2
C -> 3
Z -> 26
AA -> 27
AB -> 28
o 1 <= columnTitle.length <= 7
o columnTitle consists only of uppercase English letters.
o columnTitle is in the range ["A", "FXSHRXW"].
The problem is relatively simple. We are given a string of uppercase characters each of which represents a digit in base 26. We need to compute the integer value associated with the string. Continue reading “LeetCode 171. Excel Sheet Column Number in Java”
In this post we will be solving the LeetCode 242 Valid Anagram problem using the Java programming language.
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
o 1 <= s.length, t.length <= 5 * 10^4
o s and t consist of lowercase English letters.
o Hash Table
We are given two strings. We need to determine if one string is the anagram of the other. Continue reading “LeetCode 242 Valid Anagram in Java”
In this post we will attempt to solve LeetCode 169. Majority Element problem using Java.
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than floor(n / 2) times.
You may assume that the majority element always exists in the array.
o n == nums.length
o 1 <= n <= 5 * 10^4
o -2^31 <= nums[i] <= 2^31 - 1
o Hash Table
o Divide and Conquer
We are given an int array and we must return the majority element. There are several variations of this problem. I believe we have solved a couple in this post. Continue reading “LeetCode 169 Majority Element in Java”