Insertion Sort implements an algorithm similar to ordering a hand of cards in ascending order. The algorithm is O(n^2) execution and typically is useful when sorting a rather small number of elements.
Several years ago (November 03, 2016) I generated the post Insertion Sort in this blog. The code snippets do not look nice. It seems that the tool I was using to format source code is no longer working as expected. Continue reading “Insertion Sort – Revisited”
It has been a few weeks since my last post. I apologize for this. Will let you know after my current quest is completed. Hopefully everything will turn out as I desire.
In the meantime I have been taking a few on-line courses.
I am almost done with the course C++20 Fundamentals by Paul J. Deitel. The course was released July, 2021 and published by Pearson3. The ISBN: 013687518. This online course is made available through O’Reilly. I am a member of the Association of Computing Machinery and have access to several O’Reilly titles. Continue reading “Bubble Sort in Java”
In this post we will write a couple functions to generate Fibonacci Numbers using the C++ programming language and the Visual Studio 2022 IDE, on a Windows 11 computer.
The motivation came from the on-line course C++20 Fundamentals by Paul J. Deitel, included in the O’Reilly Learning Platform offered by the Association for Computing Machinery.
C++20 for Programmers
by Paul Deitel and Harvey Deitel
Release Date: March 2022
Publisher: Pearson Education
Length: 1008 Pages
I have preordered the new book associated with the course on Amazon. Hopefully it should arrive next month. Continue reading “Fibonacci Numbers in C++”
In this post we will tackle the LeetCode 307. Range Sum Query – Mutable problem using the Java programming language and the VSCode IDE on a Windows computer. Unless you have a good reason (i.e., keep test code and solution on the same source code) my suggestion is to solve the problem using the online IDE provided by LeetCode.
Given an integer array nums, handle multiple queries of the following types:
o Update the value of an element in nums.
o Calculate the sum of the elements of nums between indices left and right inclusive
where left <= right.
Implement the NumArray class:
o NumArray(int nums)
Initializes the object with the integer array nums.
o void update(int index, int val)
Updates the value of nums[index] to be val.
o int sumRange(int left, int right)
Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
o 1 <= nums.length <= 3 * 10^4
o -100 <= nums[i] <= 100
o 0 <= index < nums.length
o -100 <= val <= 100
o 0 <= left <= right < nums.length
o At most 3 * 10^4 calls will be made to update and sumRange.
o Binary Indexed Tree
* Segment Tree
We are given an int and asked to perform two operations as part of a class. Continue reading “LeetCode 307. Range Sum Query – Mutable in Java”
In this post I will not be solving a problem, instead I will be talking about something I learned by reading chapter 7 Design a Unique ID Generator in Distributed Systems in the book System Design Interview by Alex Wu.
The idea of a unique ID is a requirement in most distributed systems. In my case, some years ago I was developing a storage server and needed to name the objects of different types and sizes a client might wish to store and of course, at some later point in time, retrieve. The idea of a file system path did not make sense because it would be nearly impossible to keep the same immutable configuration among many distributed systems with a huge capacity. Continue reading “Design Unique ID Generator in Distributed Systems”
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 223 Rectangle Area in Java.
Given the coordinates of two rectilinear rectangles in a 2D plane,
return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
o -10^4 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 10^4
We are given the coordinates of two rectilinear rectangles in a 2D plane and are asked to return the total area covered by the two rectangles. Continue reading “LeetCode 223 Rectangle Area in Java”
In this post I will be solving LeetCode 142 Linked List Cycle II using the Java programming language.
This problem seems to be the continuation of a problem we solved earlier LeetCode 141 Linked List Cycle in Java in this blog. Perhaps you would like to read it before attempting to solve the problem in this post.
Given the head of a linked list,
return the node where the cycle begins.
If there is no cycle, return null.
There is a cycle in a linked list if there is some node in the list that
can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed).
It is -1 if there is no cycle.
Note that pos is not passed as a parameter.
Do not modify the linked list.
o The number of the nodes in the list is in the range [0, 104].
o -10^5 <= Node.val <= 10^5
o pos is -1 or a valid index in the linked-list.
o Hash Table
o Linked List
o Two Pointers
Can you solve it using O(1) (i.e. constant) memory?
We are given a singly linked list which may or may not have a cycle. We need to return `null` if it does not contain a cycle, or the node where the cycle begins. The LeetCode web site contains some diagrams to better illustrate the linked lists with a cycle. Continue reading “LeetCode 142 Linked List Cycle II 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”