Created
July 2, 2020 04:58
-
-
Save vineethguna/cdc0ebee0c7990efd50e096ff487fbc1 to your computer and use it in GitHub Desktop.
Code for birthday candles problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.vineeth.problemsolving; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class BirthdayCandles { | |
| // Solution using hash set | |
| public int minNumberOfBlowsSolution1(int[] candlesHeight) { | |
| if(candlesHeight != null && candlesHeight.length > 0) { | |
| Set<Integer> uniqueHeightSet = new HashSet<>(); | |
| for(int candleHeight: candlesHeight) { | |
| if(candleHeight > 0) { | |
| uniqueHeightSet.add(candleHeight); | |
| } | |
| } | |
| return uniqueHeightSet.size(); | |
| } | |
| return 0; | |
| } | |
| // Solution without using any java utils or libraries | |
| public int minNumberOfBlowsSolution2(int[] candlesHeight) { | |
| if(candlesHeight != null && candlesHeight.length > 0) { | |
| int minHeight = candlesHeight[0]; | |
| int maxHeight = candlesHeight[0]; | |
| for (int candleHeight : candlesHeight) { | |
| if (candleHeight > 0) { | |
| if (candleHeight < minHeight) { | |
| minHeight = candleHeight; | |
| } else if (candleHeight > maxHeight) { | |
| maxHeight = candleHeight; | |
| } | |
| } | |
| } | |
| boolean[] isCandleHeightPresent = new boolean[maxHeight - minHeight + 1]; | |
| int maxNoOfBlows = 0; | |
| for (int candleHeight : candlesHeight) { | |
| if (candleHeight > 0) { | |
| if (!isCandleHeightPresent[candleHeight - minHeight]) { | |
| maxNoOfBlows++; | |
| isCandleHeightPresent[candleHeight - minHeight] = true; | |
| } | |
| } | |
| } | |
| return maxNoOfBlows; | |
| } | |
| return 0; | |
| } | |
| public static void main(String[] args) { | |
| BirthdayCandles b = new BirthdayCandles(); | |
| // Expected answer is 4 | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{8, 7, 7, 5, 3, 8, 8, 3, 5, 3, 7})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{8, 7, 7, 5, 3, 8, 8, 3, 5, 3, 7})); | |
| // Expected answer is 0 as there are no candles | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{})); | |
| // Expected answer is 0 as the candles are not greater than 0 | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{0,0,0})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{0,0,0})); | |
| // Expected answer is 0 as the candles are not greater than 0 | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{0,0,-1})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{0,0,-1})); | |
| // Expected answer is 1 as all candles are of same height | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{8,8,8,8,8,8})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{8,8,8,8,8,8})); | |
| // Expected answer is 1 as all candles which are greater than 0 are of same height | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{8,8,8,8,8,8,-1,-1,0,0})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{8,8,8,8,8,8,-1,-1,0,0})); | |
| // Expected answer is 8 as there are 8 unique height candles | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{1,2,3,4,5,6,7,8})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{1,2,3,4,5,6,7,8})); | |
| // Expected answer is 6 as there are 6 unique height candles | |
| System.out.println(b.minNumberOfBlowsSolution1(new int[]{52,346,372,173,152,52,67,372})); | |
| System.out.println(b.minNumberOfBlowsSolution2(new int[]{52,346,372,173,152,52,67,372})); | |
| // Expected answer is 0 as the passed input is not valid | |
| System.out.println(b.minNumberOfBlowsSolution1(null)); | |
| System.out.println(b.minNumberOfBlowsSolution2(null)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment