보안 / 개발 챌린저가 목표

[AlphaGo Study] [HackerRank] [JAVA] Halloween Sale 본문

Development/Algorithm

[AlphaGo Study] [HackerRank] [JAVA] Halloween Sale

햄미은서 2020. 9. 14. 00:23

 

문제 설명

 You wish to buy video games from the famous online video game store Mist.

 Usually, all games are sold at the same price, p dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game you buy during the sale will be sold a p dollars, but every subsequent game you buy will be sold at exactly d dollars less than the cost of the previous one you bought. This will coutinue until the cost becomes less than or equal to m dollars, after which every game you buy will cost m dollars each.

 You have s dollars in your Mist wallet. How many games can you buy during the Halloween Sale?

제한 사항

  • 1 ≤ m ≤ p ≤ 100
  • 1 ≤ d ≤ 100
  • 1 ≤ s ≤ 10⁴
  • Print a single line containing a single integer denoting the maximum number of games you can buy.

입출력 예

p, d, m, s result
20, 3, 6, 80 6
20, 3, 6, 85 7

입출력 예 설명

#예제 1

 We have p = 20, d = 3, and m = 6, the same as in the problem statement. We also have s = 80 dollars. We can buy 6 games since they cost 20 + 17 + 14 + 11 + 8 + 6 = 76 dollars. However, we cannot buy a 7 th game. Thus, the answer is 6.

 

#예제 2

 This is the same as the previous case, except this time we have s = 85 dollars. This time, we can buy 7 games since they cost 20 + 17 + 14 + 11 + 8 + 6 + 6 = 82 dollars. However, we cannot buy an 8 th game. Thus, the answer is 7.

 

www.hackerrank.com/challenges/halloween-sale/problem

 

Halloween Sale | HackerRank

How many games can you buy during the Halloween Sale?

www.hackerrank.com


문제를 풀기 전 THINK

  • 모든 게임은 p dollars 였으나, Halloween Sale로 인해 싸게 구입 가능하다.
  • 첫 번째 게임은 p dollars, 그 다음 게임부터는 전에 구입한 가격 - d dollars.
  • m dollars 될 때 까지 -d dollars를 계속 한다.
  • m dollars가 되면 그 다음부터는 계속 m dollars로 구입이 가능하다.

나의 Solution

public class HalloweenSale {
	
	int howManyGames(int p, int d, int m, int s) {
	// 구매한 게임 갯수
        int game_count = 0;       
        
        // 돈이 다 떨어질 때 까지 반복
        while(s >= 0) {
        	s = s - p; // 게임 구매하고 남은 돈
        	
        	if(p - d >= m) {
        		p = p - d;
        		game_count++;
        	} else {
        		p = m;
        		
        		// 남은 돈이 -면 game_count를 증가시키지 않음
        		if(s > 0) {
        			game_count++;
        		} 
        	}
        }
		return game_count;
    }
}

 

Comments