We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Halloween Sale
Halloween Sale
+ 0 comments Here are my approaches in c++, you can see the explanation here : https://youtu.be/VFVaLMRzhV4
Approach 1 O(n)
int howManyGames(int p, int d, int m, int s) { int result = 0; while( s >= p){ s -= p; p = max(m, p-d); result++; } return result; }
Approach 2 O(n) with recursion
int howManyGames(int p, int d, int m, int s){ if( s < p) return 0; return howManyGames( max(m, p-d), d, m, s - p) + 1; }
Approach 3 O(1), incomplete solution (failure of a test case), feel free to comment to share a way to complete it.
int howManyGames(int p, int d, int m, int s) { int n = 1 + (p - m) / d; int total = (p + p - (n - 1) * d) * n / 2; if(total <= s) { return n + (s - total) / m; } else return 0; // this return need to be computed properly }
+ 0 comments /* * Cuddle time * Phuong Chi * Miss you */ import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Solve_problems { static int solve(int p, int d, int m, int s) { int gameBought=0; while(s>=p) { s-=p; ++gameBought; p=Math.max(p-d,m);// Ensure the price doesn't go below the minimum cost 'm' } return gameBought; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int p = sc.nextInt(); int d = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); System.out.println(solve(p, d, m, s)); sc.close(); } }
+ 0 comments Typescript
function howManyGames(p: number, d: number, m: number, s: number): number { let r = 0 while(s - p >= 0){ r++ s -= p p = p - d <= m ? m : p - d } return r }
+ 0 comments def howManyGames(p, d, m, s): counter = 0 while s >= 0: s -= p if s < 0: break if p > m+d: p -= d else: p = m counter += 1 return counter
+ 0 comments C++ Solution:
int games=0; while(s >= p) { s -= p; if(p - d >= m) p -= d; else p = m; games++; } return games;
Load more conversations
Sort 621 Discussions, By:
Please Login in order to post a comment