diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..097f9f9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux start script should use lf +/gradlew text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b6985c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/README.md b/README.md index 8a56aed..f36f528 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ 1. [Leetcode Top 150](https://leetcode.com/studyplan/top-interview-150/) 2. [Striver's SDE sheet](https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems) -3. [Team Blind Top 75 Blind](https://www.teamblind.com/post/new-year-gift---curated-list-of-top-75-leetcode-questions-to-save-your-time-oam1oreu) \ No newline at end of file +3. [Team Blind Top 75](https://www.teamblind.com/post/new-year-gift---curated-list-of-top-75-leetcode-questions-to-save-your-time-oam1oreu) \ No newline at end of file diff --git a/dsa/.gitignore b/dsa/.gitignore new file mode 100644 index 0000000..b63da45 --- /dev/null +++ b/dsa/.gitignore @@ -0,0 +1,42 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/dsa/.idea/.gitignore b/dsa/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/dsa/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/dsa/.idea/gradle.xml b/dsa/.idea/gradle.xml new file mode 100644 index 0000000..2a65317 --- /dev/null +++ b/dsa/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/dsa/.idea/misc.xml b/dsa/.idea/misc.xml new file mode 100644 index 0000000..1f701d1 --- /dev/null +++ b/dsa/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/dsa/.idea/vcs.xml b/dsa/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/dsa/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/dsa/build.gradle b/dsa/build.gradle new file mode 100644 index 0000000..e38b0f8 --- /dev/null +++ b/dsa/build.gradle @@ -0,0 +1,19 @@ +plugins { + id 'java' +} + +group = 'com.hithomelabs.dsa' +version = '1.0-SNAPSHOT' + +repositories { + mavenCentral() +} + +dependencies { + testImplementation platform('org.junit:junit-bom:5.10.0') + testImplementation 'org.junit.jupiter:junit-jupiter' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/dsa/gradle/wrapper/gradle-wrapper.jar b/dsa/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/dsa/gradle/wrapper/gradle-wrapper.jar differ diff --git a/dsa/gradle/wrapper/gradle-wrapper.properties b/dsa/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..591199f --- /dev/null +++ b/dsa/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Feb 26 15:43:32 IST 2025 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/dsa/gradlew b/dsa/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/dsa/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/dsa/gradlew.bat b/dsa/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/dsa/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/dsa/prompts/generate_code_packages_and interfaces.txt b/dsa/prompts/generate_code_packages_and interfaces.txt new file mode 100644 index 0000000..7dee713 --- /dev/null +++ b/dsa/prompts/generate_code_packages_and interfaces.txt @@ -0,0 +1,9 @@ +I am trying to check in my leetcode solutions to a gradle project . Help me reduce my manual work. + +For each question present except the first one, + +1. Create a new package for the problem inside /dsa/src/main/java with package name com.hithomelabs.dsa.array. where package name is the name of the question in all lower case with '_' instead of spaces. + +2. Create a new java file with a public class Solution as in the leetcode URL associated with each question. + +3. Create a new java file for a package protected interface Solvable belonging to the above package with a method solve having the same return type and arguments as of the metod present in the class Solution. \ No newline at end of file diff --git a/dsa/settings.gradle b/dsa/settings.gradle new file mode 100644 index 0000000..3144ed6 --- /dev/null +++ b/dsa/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'dsa' + diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solution.java new file mode 100644 index 0000000..5e99bbb --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock; + +public class Solution implements Solvable { + @Override + public int solve(int[] prices) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solvable.java new file mode 100644 index 0000000..2c1346c --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/Solvable.java @@ -0,0 +1,6 @@ +package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock; + +public interface Solvable { + public int solve(int[] prices); +} + diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solution.java new file mode 100644 index 0000000..25f2bb3 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.container_with_most_water; + +public class Solution implements Solvable { + @Override + public int solve(int[] height) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solvable.java new file mode 100644 index 0000000..ed995ea --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/container_with_most_water/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.container_with_most_water; + +public interface Solvable { + public int solve(int[] height); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solution.java new file mode 100644 index 0000000..3734c83 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.product_of_array_except_self; + +public class Solution implements Solvable { + @Override + public int[] solve(int[] nums) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solvable.java new file mode 100644 index 0000000..0f1711b --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/contains_duplicate/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.contains_duplicate; + +public interface Solvable { + public boolean solve(int[] nums); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solution.java new file mode 100644 index 0000000..b663a8d --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array; + +public class Solution implements Solvable { + @Override + public int solve(int[] nums) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solvable.java new file mode 100644 index 0000000..88907e1 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array; + +public interface Solvable { + public int solve(int[] nums); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solution.java new file mode 100644 index 0000000..45de9d1 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.maximum_product_subarray; + +public class Solution implements Solvable { + @Override + public int solve(int[] nums) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solvable.java new file mode 100644 index 0000000..0c83bed --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/maximum_product_subarray/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.maximum_product_subarray; + +public interface Solvable { + public int solve(int[] nums); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solution.java new file mode 100644 index 0000000..d0b1fe4 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.product_of_array_except_self; + +public class Solution { + // Method signature for the solution + public int[] solve(int[] nums) { + // Implementation here + } +} diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solvable.java new file mode 100644 index 0000000..ad5d285 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/product_of_array_except_self/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.product_of_array_except_self; + +public interface Solvable { + public int[] solve(int[] nums); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solution.java new file mode 100644 index 0000000..907e877 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solution.java @@ -0,0 +1,8 @@ +package com.hithomelabs.dsa.array.search_in_rotated_sorted_array; + +public class Solution implements Solvable { + @Override + public int solve(int[] nums, int target) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solvable.java new file mode 100644 index 0000000..79e86c1 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.search_in_rotated_sorted_array; + +public interface Solvable { + public int solve(int[] nums, int target); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solution.java new file mode 100644 index 0000000..82af426 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solution.java @@ -0,0 +1,10 @@ +package com.hithomelabs.dsa.array.three_sum; + +import java.util.List; + +public class Solution implements Solvable { + @Override + public List> solve(int[] nums) { + // Implementation of the solution + } +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solvable.java new file mode 100644 index 0000000..e40150e --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/three_sum/Solvable.java @@ -0,0 +1,7 @@ +package com.hithomelabs.dsa.array.three_sum; + +import java.util.List; + +public interface Solvable { + public List> solve(int[] nums); +} \ No newline at end of file diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/HashMapSolution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/HashMapSolution.java new file mode 100644 index 0000000..c42baf0 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/HashMapSolution.java @@ -0,0 +1,26 @@ +package com.hithomelabs.dsa.array.two_sum; +import java.util.Map; + +public class HashMapSolution implements Solvable { + + @Override + public int[] solve(int[] nums, int target) { + // Create a map to store the numbers and their corresponding indices + Map map = new HashMap(); + + // Iterate through the array of numbers + for (int i = 0; i < nums.length; i++){ + // Check if the complement (target - current number) exists in the map + if (map.containsKey(target - nums[i])){ + // If found, return the indices of the current number and its complement + int[] solution = {i, map.get(target - nums[i])}; + return solution; + } + else + // If not found, add the current number and its index to the map + map.put(nums[i], i); + } + // Return null if no solution is found + return null; + } +} diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solution.java b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solution.java new file mode 100644 index 0000000..ec99462 --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solution.java @@ -0,0 +1,10 @@ +package com.hithomelabs.dsa.array.two_sum; + +class Solution { + + private Solution solution = new HashMapSolution(); + + public int[] twoSum(int[] nums, int target) { + return solution.solve(nums, target); + } +} diff --git a/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solvable.java b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solvable.java new file mode 100644 index 0000000..6f69fcd --- /dev/null +++ b/dsa/src/main/java/com/hithomelabs/dsa/array/two_sum/Solvable.java @@ -0,0 +1,5 @@ +package com.hithomelabs.dsa.array.two_sum; + +public interface Solvable { + public int[] solve(int[] nums, int target); +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/SolutionTest.java new file mode 100644 index 0000000..9492966 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/best_time_to_buy_and_sell_stock/SolutionTest.java @@ -0,0 +1,35 @@ +package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock; +import static org.junit.jupiter.api.Assertions.assertEquals; +// https://example.com/best_time_to_buy_and_sell_stock +public class SolutionTest { + + + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case with increasing prices + int[] prices1 = {7, 1, 5, 3, 6, 4}; + assertEquals(5, solution.solve(prices1)); + + // Test case 2: Prices decrease + int[] prices2 = {7, 6, 4, 3, 1}; + assertEquals(0, solution.solve(prices2)); + + // Test case 3: Prices are constant + int[] prices3 = {5, 5, 5, 5, 5}; + assertEquals(0, solution.solve(prices3)); + + // Test case 4: Prices with a single day + int[] prices4 = {1}; + assertEquals(0, solution.solve(prices4)); + + // Test case 5: Prices with two days, increasing + int[] prices5 = {1, 2}; + assertEquals(1, solution.solve(prices5)); + + // Test case 6: Prices with two days, decreasing + int[] prices6 = {2, 1}; + assertEquals(0, solution.solve(prices6)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/container_with_most_water/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/container_with_most_water/SolutionTest.java new file mode 100644 index 0000000..d54b73f --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/container_with_most_water/SolutionTest.java @@ -0,0 +1,31 @@ +package com.hithomelabs.dsa.array.container_with_most_water; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Example case + int[] heights1 = {1,8,6,2,5,4,8,3,7}; + assertEquals(49, solution.solve(heights1)); + + // Test case 2: Minimum input + int[] heights2 = {1, 1}; + assertEquals(1, solution.solve(heights2)); + + // Test case 3: All heights are the same + int[] heights3 = {4, 4, 4, 4}; + assertEquals(12, solution.solve(heights3)); + + // Test case 4: Decreasing heights + int[] heights4 = {5, 4, 3, 2, 1}; + assertEquals(6, solution.solve(heights4)); + + // Test case 5: Increasing heights + int[] heights5 = {1, 2, 3, 4, 5}; + assertEquals(6, solution.solve(heights5)); + } +} + diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/contains_duplicate/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/contains_duplicate/SolutionTest.java new file mode 100644 index 0000000..96d7699 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/contains_duplicate/SolutionTest.java @@ -0,0 +1,29 @@ +package com.hithomelabs.dsa.array.contains_duplicate; + + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Example case with duplicates + int[] nums1 = {1, 2, 3, 1}; + assertTrue(solution.containsDuplicate(nums1)); + + // Test case 2: No duplicates + int[] nums2 = {1, 2, 3, 4}; + assertFalse(solution.containsDuplicate(nums2)); + + // Test case 3: All elements are the same + int[] nums3 = {1, 1, 1, 1}; + assertTrue(solution.containsDuplicate(nums3)); + + // Test case 4: Empty array + int[] nums4 = {}; + assertFalse(solution.containsDuplicate(nums4)); + + // Test case 5: Single element + int[] nums5 = {1}; + assertFalse(solution.containsDuplicate(nums5)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/SolutionTest.java new file mode 100644 index 0000000..e650133 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/find_minimum_in_rotated_sorted_array/SolutionTest.java @@ -0,0 +1,29 @@ +package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array; + + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case with rotation + int[] nums1 = {3, 4, 5, 1, 2}; + assertEquals(1, solution.solve(nums1)); + + // Test case 2: Array not rotated + int[] nums2 = {1, 2, 3, 4, 5}; + assertEquals(1, solution.solve(nums2)); + + // Test case 3: Array with one element + int[] nums3 = {1}; + assertEquals(1, solution.solve(nums3)); + + // Test case 4: Array with two elements, rotated + int[] nums4 = {2, 1}; + assertEquals(1, solution.solve(nums4)); + + // Test case 5: Array with duplicates + int[] nums5 = {2, 2, 2, 0, 1, 2}; + assertEquals(0, solution.solve(nums5)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/maximum_product_subarray/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/maximum_product_subarray/SolutionTest.java new file mode 100644 index 0000000..5037ae6 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/maximum_product_subarray/SolutionTest.java @@ -0,0 +1,33 @@ +package com.hithomelabs.dsa.array.maximum_product_subarray; + + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case with positive numbers + int[] nums1 = {2, 3, -2, 4}; + assertEquals(6, solution.solve(nums1)); + + // Test case 2: Array with all negative numbers + int[] nums2 = {-2, -3, -1}; + assertEquals(6, solution.solve(nums2)); + + // Test case 3: Array with a mix of positive and negative numbers + int[] nums3 = {-2, 0, -1}; + assertEquals(0, solution.solve(nums3)); + + // Test case 4: Array with one element + int[] nums4 = {3}; + assertEquals(3, solution.solve(nums4)); + + // Test case 5: Array with two elements, one negative + int[] nums5 = {2, -5}; + assertEquals(2, solution.solve(nums5)); + + // Test case 6: Array with multiple zeros + int[] nums6 = {0, 2, 0, 3, 0}; + assertEquals(6, solution.solve(nums6)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/product_of_array_except_self/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/product_of_array_except_self/SolutionTest.java new file mode 100644 index 0000000..a74645c --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/product_of_array_except_self/SolutionTest.java @@ -0,0 +1,34 @@ +package com.hithomelabs.dsa.array.product_of_array_except_self; + + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case + int[] nums1 = {1, 2, 3, 4}; + int[] expected1 = {24, 12, 8, 6}; + assertArrayEquals(expected1, solution.solve(nums1)); + + // Test case 2: Array with one element + int[] nums2 = {5}; + int[] expected2 = {1}; + assertArrayEquals(expected2, solution.solve(nums2)); + + // Test case 3: Array with two elements + int[] nums3 = {1, 2}; + int[] expected3 = {2, 1}; + assertArrayEquals(expected3, solution.solve(nums3)); + + // Test case 4: Array with zeros + int[] nums4 = {0, 1, 2, 3}; + int[] expected4 = {6, 0, 0, 0}; + assertArrayEquals(expected4, solution.solve(nums4)); + + // Test case 5: Array with negative numbers + int[] nums5 = {-1, -2, -3, -4}; + int[] expected5 = {-24, -12, -8, -6}; + assertArrayEquals(expected5, solution.solve(nums5)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/SolutionTest.java new file mode 100644 index 0000000..5be0570 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/search_in_rotated_sorted_array/SolutionTest.java @@ -0,0 +1,39 @@ +package com.hithomelabs.dsa.array.search_in_rotated_sorted_array; + + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case with rotation + int[] nums1 = {4, 5, 6, 7, 0, 1, 2}; + int target1 = 0; + assertEquals(4, solution.solve(nums1, target1)); + + // Test case 2: Target not present + int[] nums2 = {4, 5, 6, 7, 0, 1, 2}; + int target2 = 3; + assertEquals(-1, solution.solve(nums2, target2)); + + // Test case 3: Array with one element (target present) + int[] nums3 = {1}; + int target3 = 1; + assertEquals(0, solution.solve(nums3, target3)); + + // Test case 4: Array with one element (target not present) + int[] nums4 = {1}; + int target4 = 0; + assertEquals(-1, solution.solve(nums4, target4)); + + // Test case 5: Array is rotated at the pivot + int[] nums5 = {3, 1}; + int target5 = 1; + assertEquals(1, solution.solve(nums5, target5)); + + // Test case 6: Array is not rotated + int[] nums6 = {1, 2, 3, 4, 5}; + int target6 = 3; + assertEquals(2, solution.solve(nums6, target6)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/three_sum/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/three_sum/SolutionTest.java new file mode 100644 index 0000000..efad51a --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/three_sum/SolutionTest.java @@ -0,0 +1,35 @@ +package com.hithomelabs.dsa.array.three_sum; +import java.util.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SolutionTest { + @Test + void testSolve() { + Solution solution = new Solution(); + + // Test case 1: Normal case with positive numbers + int[] nums1 = {1, 2, -2, -1}; + List> expected1 = Arrays.asList(Arrays.asList(-2, -1, 1), Arrays.asList(-2, 1, 2)); + assertEquals(expected1, solution.solve(nums1)); + + // Test case 2: No solution + int[] nums2 = {1, 2, 3}; + List> expected2 = new ArrayList<>(); + assertEquals(expected2, solution.solve(nums2)); + + // Test case 3: Array with duplicates + int[] nums3 = {0, 0, 0, 0}; + List> expected3 = Arrays.asList(Arrays.asList(0, 0, 0)); + assertEquals(expected3, solution.solve(nums3)); + + // Test case 4: Mixed positive and negative numbers + int[] nums4 = {-1, 0, 1, 2, -1, -4}; + List> expected4 = Arrays.asList(Arrays.asList(-1, -1, 2), Arrays.asList(-1, 0, 1)); + assertEquals(expected4, solution.solve(nums4)); + + // Test case 5: All elements are zero + int[] nums5 = {0, 0, 0}; + List> expected5 = Arrays.asList(Arrays.asList(0, 0, 0)); + assertEquals(expected5, solution.solve(nums5)); + } +} diff --git a/dsa/src/test/java/com/hithomelabs/dsa/array/two_sum/SolutionTest.java b/dsa/src/test/java/com/hithomelabs/dsa/array/two_sum/SolutionTest.java new file mode 100644 index 0000000..e4dd470 --- /dev/null +++ b/dsa/src/test/java/com/hithomelabs/dsa/array/two_sum/SolutionTest.java @@ -0,0 +1,26 @@ +package com.hithomelabs.dsa.array.two_sum; + +import static org.junit.Assert.assertArrayEquals; + +public class SolutionTest { + @Test + void testTwoSum() { + // Test case 1: nums = [2, 7, 11, 15], target = 9 + int[] nums1 = {2, 7, 11, 15}; + int target1 = 9; + int[] expected1 = {0, 1}; // indices of the numbers that add up to target + assertArrayEquals(expected1, new HashMapSolution().solve(nums1, target1)); + + // Test case 2: nums = [3, 2, 4], target = 6 + int[] nums2 = {3, 2, 4}; + int target2 = 6; + int[] expected2 = {1, 2}; // indices of the numbers that add up to target + assertArrayEquals(expected2, new HashMapSolution().solve(nums2, target2)); + + // Test case 3: nums = [3, 3], target = 6 + int[] nums3 = {3, 3}; + int target3 = 6; + int[] expected3 = {0, 1}; // indices of the numbers that add up to target + assertArrayEquals(expected3, new HashMapSolution().solve(nums3, target3)); + } +}