Skip to content

Dsa #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Dsa #152

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/codestream.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

290 changes: 170 additions & 120 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* Big (O) of Nested Loop With Addition
* Compute the Big O complexity of the code snippet given below.
* Big (O) of Nested Loop With Addition Compute the Big O complexity of the code snippet given
* below.
*
* @authr David Kariuki
* @since 10/8/2022
*/

package AceTheJavaCodingInterview.module1_big_o_notation;
package AceTheJavaCodingInterview.big_o_notation;

public class BigONestedLoopWithAddition {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* Big (O) of Nested Loop With Multiplication
* Compute the Big O complexity of the code snippet given below.
* Big (O) of Nested Loop With Multiplication Compute the Big O complexity of the code snippet given
* below.
*
* @authr David Kariuki
* @since 12/8/2022
*/

package AceTheJavaCodingInterview.module1_big_o_notation;
package AceTheJavaCodingInterview.big_o_notation;

@SuppressWarnings("ALL")
public class BigONestedLoopWithMultiplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* Big (O) of Nested Loop With Subtraction
* Compute the Big O complexity of the code snippet given below.
* Big (O) of Nested Loop With Subtraction Compute the Big O complexity of the code snippet given
* below.
*
* @authr David Kariuki
* @since 11/8/2022
*/

package AceTheJavaCodingInterview.module1_big_o_notation;
package AceTheJavaCodingInterview.big_o_notation;

public class BigONestedLoopWithSubtraction {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Problem set 1
*
* @author David Kariuki
* @since 14/8/2022
*/

package AceTheJavaCodingInterview.module1_big_o_notation.problem_sets;
package AceTheJavaCodingInterview.big_o_notation.problem_sets;

@SuppressWarnings("unused")
public class ProblemSet1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Problem set 2
*
* @author David Kariuki
* @since 14/8/2022
*/

package AceTheJavaCodingInterview.module1_big_o_notation.problem_sets;

package AceTheJavaCodingInterview.big_o_notation.problem_sets;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* <p>
*
* @question Problem Statement -
* @example
* @example Example 1:
* @example Example 2:
* @example Example 3:
* @note Solution -
* @note Time Complexity -
* @note Space Complexity -
* @author David Kariuki
* @since /8/2022
* @since /9/2022
*/
package AceTheJavaCodingInterview.module2_data_structures;
package AceTheJavaCodingInterview.data_structures;

public class ClassTemplate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* @author David Kariuki
* @since 17/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import AceTheJavaCodingInterview.module2_data_structures.utils.DataStructuresUtils;
import AceTheJavaCodingInterview.data_structures.utils.DataStructuresUtils;

import java.util.ArrayDeque;
import java.util.Deque;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*
* <p>
*
* @question Given three integer arrays sorted in ascending order, return the smallest number found
* in all three arrays.
* @question Given three integer array sorted in ascending order, return the smallest number found
* in all three array.
* @author David Kariuki
* @since 18/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import AceTheJavaCodingInterview.module2_data_structures.utils.DataStructuresUtils;
import AceTheJavaCodingInterview.data_structures.utils.DataStructuresUtils;

public class FindTheSmallestCommonNumber {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author David Kariuki
* @since 23/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

public class MaximumSumSubarrayOfSizeK {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* @author David Kariuki
* @since 22/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import AceTheJavaCodingInterview.module2_data_structures.tuples.Interval;
import AceTheJavaCodingInterview.data_structures.tuple.Interval;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author David Kariuki
* @since 14/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

@SuppressWarnings({"ForLoopReplaceableByForEach", "SameParameterValue"})
public class OneDimensionalArrays {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author David Kariuki
* @since 24/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

public class PairWithTargetSum {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* <p>{1, 2, 3} {1, 3, 2} {2, 1, 3} {2, 3, 1} {3, 1, 2} {3, 2, 1}
* <p>If a set has n distinct elements it will have n! n! permutations.
* @note Solution This problem follows the Subsets pattern ({@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.Subset}) and, we can follow a
* similar Breadth First Search (BFS) approach. However, unlike Subsets, every permutation must
* contain all the numbers.
* AceTheJavaCodingInterview.data_structures.array.Subset}) and, we can follow a similar Breadth
* First Search (BFS) approach. However, unlike Subsets, every permutation must contain all the
* numbers.
* <p>Let’s take the example-1 mentioned above to generate all the permutations. Following a BFS
* approach, we will consider one number at a time:
* <p>1. If the given set is empty then we have only an empty permutation set: []
Expand Down Expand Up @@ -39,12 +39,12 @@
* and the queue to store the intermediate permutations. If you see closely, at any time, we
* don’t have more than N! permutations between the result list and the queue. Therefore, the
* overall space complexity to store N! permutations each containing N elements will be O(N*N!).
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.Subset
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.SubsetsWithDuplicates
* @see AceTheJavaCodingInterview.data_structures.array.Subset
* @see AceTheJavaCodingInterview.data_structures.array.SubsetsWithDuplicates
* @author David Kariuki
* @since 24/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author David Kariuki
* @since 23/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

public class SmallestSubarrayWithAGreaterSum {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author David Kariuki
* @since 23/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

public class SquaringASortedArray {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@
* <p>
*
* @question Problem Statement - Given an array with positive numbers and a positive target number,
* find all of its contiguous sub-arrays whose product is less than the target number.
* find all of its contiguous sub-array whose product is less than the target number.
* @example Example 1:
* <p>Input: [2, 5, 3, 10], target=30 Output: [2], [5], [2, 5], [3], [5, 3], [10] Explanation:
* There are six contiguous sub-arrays whose product is less than the target.
* There are six contiguous sub-array whose product is less than the target.
* @example Example 2:
* <p>Input: [8, 2, 6, 5], target=50 Output: [8], [2], [8, 2], [6], [2, 6], [5], [6, 5]
* Explanation: There are seven contiguous sub-arrays whose product is less than the target.
* Explanation: There are seven contiguous sub-array whose product is less than the target.
* @note Solution - This problem follows the Sliding Window and the Two Pointers pattern and shares
* similarities with Triplets with Smaller Sum
* <p>({@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.TripletsWithSmallerSum_ReturnCount},
* {@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.TripletsWithSmallerSum_ReturnList})
* with two differences:
* AceTheJavaCodingInterview.data_structures.array.TripletsWithSmallerSum_ReturnCount}, {@link
* AceTheJavaCodingInterview.data_structures.array.TripletsWithSmallerSum_ReturnList}) with two
* differences:
* <p>In this problem, the input array is not sorted. Instead of finding triplets with sum less
* than a target, we need to find all sub-arrays having a product less than the target. The
* than a target, we need to find all sub-array having a product less than the target. The
* implementation will be quite similar to Triplets with Smaller Sum.
* @note Time Complexity -
* @note Space Complexity -
* @note Time Complexity - The main for-loop managing the sliding window takes O(N) but creating
* sub-array can take up to O(N^2) in the worst case. Therefore overall, our algorithm will take
* O(N^3).
* @note Space Complexity - Ignoring the space required for the output list, the algorithm runs in
* O(N) space which is used for the temp list.
* @author David Kariuki
* @since 26/8/2022
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.TripletsWithSmallerSum_ReturnCount
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.TripletsWithSmallerSum_ReturnList
* @see AceTheJavaCodingInterview.data_structures.array.TripletsWithSmallerSum_ReturnCount
* @see AceTheJavaCodingInterview.data_structures.array.TripletsWithSmallerSum_ReturnList
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.LinkedList;
Expand All @@ -49,7 +51,7 @@ public static void main(String[] args) {
}

/**
* Method to find sub arrays
* Method to find sub array
*
* @param arr - int[]
* @param target - int
Expand All @@ -72,7 +74,7 @@ public static List<List<Integer>> findSubArrays(int[] arr, int target) {
}

// Since the product of all numbers from left to right is less than the
// target therefore, all sub-arrays from left to right will have a
// target therefore, all sub-array from left to right will have a
// product less than the target too; to avoid duplicates, we will start
// with a sub-array containing only arr[right] and then extend it.
List<Integer> tempList = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author David Kariuki
* @since 23/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @author David Kariuki
* @since 24/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
* of the triplet. If there are more than one such triplet, return the sum of the triplet with
* the smallest sum.
* @note Solution - This problem follows the Two Pointers pattern and is quite similar to Triplet
* Sum to Zero({@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.TripletSumToZero}).
* Sum to Zero({@link AceTheJavaCodingInterview.data_structures.array.TripletSumToZero}).
* <p>We can follow a similar approach to iterate through the array, taking one number at a
* time. At every step, we will save the difference between the triplet and the target number,
* so that in the end, we can return the triplet with the closest sum.
Expand All @@ -19,9 +18,9 @@
* for sorting.
* @author David Kariuki
* @since 25/8/2022
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.TripletSumToZero
* @see AceTheJavaCodingInterview.data_structures.array.TripletSumToZero
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@
* unique triplets whose sum is equal to zero.
* @solution Solution - This problem follows the Two Pointers pattern and shares similarities with
* Pair with Target Sum ({@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.PairWithTargetSum}). A couple of
* differences are that the input array is not sorted and instead of a pair we need to find
* triplets with a target sum of zero.
* AceTheJavaCodingInterview.data_structures.array.PairWithTargetSum}). A couple of differences
* are that the input array is not sorted and instead of a pair we need to find triplets with a
* target sum of zero.
* <p>To follow a similar approach, first, we will sort the array and then iterate through it
* taking one number at a time. Let’s say during our iteration we are at number ‘X’, so we need
* to find ‘Y’ and ‘Z’ such that X + Y + Z == 0. At this stage, our problem translates into
* finding a pair whose sum is equal to -X (as from the above equation Y + Z == -X).
* <p>
* <p>Another difference from Pair with Target Sum({@link
* AceTheJavaCodingInterview.module2_data_structures.arrays.PairWithTargetSum}) is that we need
* to find all the unique triplets. To handle this, we have to skip any duplicate number. Since
* we will be sorting the array, so all the duplicate numbers will be next to each other and are
* easier to skip.
* AceTheJavaCodingInterview.data_structures.array.PairWithTargetSum}) is that we need to find
* all the unique triplets. To handle this, we have to skip any duplicate number. Since we will
* be sorting the array, so all the duplicate numbers will be next to each other and are easier
* to skip.
* @note Time complexity - Sorting the array will take O(N * logN). The searchPair() function will
* take O(N). As we are calling searchPair() for every number in the input array, this means
* that overall searchTriplets() will take O(N * logN + N^2), which is asymptotically equivalent
* to O(N^2).
* @note Space complexity - Ignoring the space required for the output array, the space complexity
* of the above algorithm will be O(N) which is required for sorting.
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.PairWithTargetSum
* @see AceTheJavaCodingInterview.module2_data_structures.hash_tables.PairWithTargetSum
* @see AceTheJavaCodingInterview.data_structures.array.PairWithTargetSum
* @see AceTheJavaCodingInterview.data_structures.hash_table.PairWithTargetSum
* @author David Kariuki
* @since 25/8/2022
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* of the above algorithm will be O(N) which is required for sorting.
* @author David Kariuki
* @since 26/8/2022
* @see AceTheJavaCodingInterview.module2_data_structures.arrays.TripletSumToZero
* @see AceTheJavaCodingInterview.data_structures.array.TripletSumToZero
*/
package AceTheJavaCodingInterview.module2_data_structures.arrays;
package AceTheJavaCodingInterview.data_structures.array;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Loading