Running Locally:
Pre-req: Java8 and Maven3
- Install HomeBrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
-
Install Java:
-
brew tap caskroom/versions
-
brew cask install java8
-
-
Install Maven:
brew install maven
-
Run the project: In the terminal, run the following command in the GiftCard directory.
-
mvn package
- This command runs all the tests and package up a jar in the GiftCard/target directory.
-
cd into GiftCard/target
-
Run the command:
java -jar price-0.0.1-SNAPSHOT.jar /Your/local/path/prices.txt 2500
- The first argument is the text file, the second is an int representing the target.
-
prices.txt
Candy Bar, 500
Paperback Book, 700
Detergent, 1000
Headphones, 1400
Earmuffs, 2000
Bluetooth Stereo, 6000
java -jar price-0.0.1-SNAPSHOT.jar /Your/local/path/prices.txt 2500
Candy Bar 500, Earmuffs 2000
This solution runs in O(n) and has the same space complexity. We iterate through all the lines of the file until we find the best combination.
Assume the file and target are in the right formats and in order.
- /src/main/java/com/giftcard/price/manager/impl/GiftCardManagerImpl.java - source code
- /src/test/java/com/giftcard/price/PriceApplicationTests.java - tests