LeetCode 760. Find Anagram Mappings 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.

Constraints:

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.

Related Topics:

o Array
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”

LeetCode 169 Majority Element 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.

Constraints:

o n == nums.length
o 1 <= n <= 5 * 10^4
o -2^31 <= nums[i] <= 2^31 - 1

Related Topics:

o Array
o Hash Table
o Divide and Conquer
o Sorting
o Counting

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”

LeetCode 575. Distribute Candies in Java

In this post we will solve the LeetCode 575. Distribute Candies problem.

This problem is not too difficult to get to an answer. Not sure if it was network traffic, but the different attempts, which were accepted, required a different approach. Once you get a first pass accepted, I would suggest that before you leave the LeetCode website, take a look at the implementations that perform best. I was going to implement some of those approaches but got late and had to move on with other tasks.

Alice has n candies, where the ith candy is of type candyType[i]. 
Alice noticed that she started to gain weight, so she visited a doctor.

The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). 
Alice likes her candies very much, 
and she wants to eat the maximum number of different types of candies while still following the doctor's advice.

Given the integer array candyType of length n, 
return the maximum number of different types of candies she can eat if she only eats n / 2 of them.

Constraints:

o n == candyType.length
o 2 <= n <= 10^4
o n is even.
o -10^5 <= candyType[i] <= 10^5

In this problem Alice is given different candies. As we will see in the examples, a type of candy may appear once or more times. Continue reading “LeetCode 575. Distribute Candies in Java”

LeetCode 229. Majority Element II in Java

In this post we will solve the LeetCode 229. Majority Element II problem using two approaches.

Given an integer array of size n, find all elements that appear more than floor(n/3)times.

Constraints:

o 1 <= nums.length <= 5 * 10^4
o -10^9 <= nums[i] <= 10^9

We are given an array of integer values. We need to find all elements whose values are larger than floor(n / 3), where `n` is the number of elements in the input array. Continue reading “LeetCode 229. Majority Element II in Java”

Stone Wall

Good day! Hope all is well in your neck of the woods. My wife left lunch in the oven and ran to the dentist office. She left the oven on. Not sure when I should shut it down. I think I will do it right now before the food gets burned. When she gets back we can turn on the oven back if needed.

OK, the oven is off. It is 01:00 PM. I am starving. Hope my wife returns shortly so we can have lunch. Continue reading “Stone Wall”

Best Time to Buy and Sell Stock II

It is a relatively nice day in the Twin Cities of Minneapolis and St. Paul.

This weekend my wife and I will go to visit our younger son. His house is about three hours by car. It does not make sense to fly. It will take longer than driving when you consider the overhead at the airports.

I continue giving it a try to the process to solve dynamic problems using some simple steps as described in the paper Dynamic programming is simple by omgitspavel. Each time I give it a try I add additional guidance.

The main problem that I see with the approach is that one still has to read, understand and explore other methods before diving into dynamic programming. The problem we will be solving in this post can be resolved faster and better not using dynamic programming. Continue reading “Best Time to Buy and Sell Stock II”

Contains Duplicate

Good day! It is Thursday morning and it is a typical gloomy and cold winter day in the Twin Cities of Minneapolis and St. Paul. The good thing is that is Thursday. One more day to go to and we can start enjoying the weekend. For most of us the weekend will be 2-days long. It is different from the past two weekends in which most people enjoyed 3 or 4 days off work.

Due to COVID-19 my wife and I just leave home for grocery shopping and healthcare appoints when needed. The good thing is that vaccination has started in the USA and hopefully in a few more months most of us will be vaccinated and can start getting back to normal. We all will see what happens. Continue reading “Contains Duplicate”

Revenue Milestones in Java

Good day ladies and gentlemen. Today is Wednesday January 06, 2021. The claims of electoral fraud in the 2020 presidential elections in the USA continue to be brought up. One way or the other January 20 is about two weeks away. If the claims do not pan out then on that day we will have a new president be sworn in. We will be able to read news without political opinions.

Earlier today my wife fixed a delicious chicken dish with a white sauce with lemon and capers. She served it with rice, baked potatoes and beats. For desert we had chocolate ice cream followed by a triple espresso. That marked the middle of my day. In the afternoon I finished two additional 2-hour blocks. Continue reading “Revenue Milestones in Java”

Pair Sums in Java

!!! UPDATE UPDATE UPDATE !!! An issue was found in the implementation of the function of interest. The issue has been addressed. The code in the GitHub repository has been updated. Thanks to Mario Rincon Nigro.

Good day to all of you. I believe this is my first post for 2021. That said hope you had a nice holiday season. It seems that COVID-19 vaccinations have been started. Hopefully we will be able to put politics aside and move forward quickly vaccinating all people who are interested in getting the shots. My wife will be checking with our doctor to figure out if we can get on a schedule to get it done in the near future. We know of several people in our families and friends that have already received the first shot. It seems that for them so far so good. Will let you know how it goes after the second one.

We are already in January and the United States presidential inauguration is approaching quickly. That said; the winner of the election is still undetermined. Apparently there has been a lot of fraud committed by one of the political parties. If this is case democracy is out the door in the USA. Hopefully all will be resolved in a friendly manner and the people (if any) that committed fraud will be made accountable. Continue reading “Pair Sums in Java”