- Prepare
- Algorithms
- Greedy
- Priyanka and Toys
- Discussions
Priyanka and Toys
Priyanka and Toys
+ 5 comments Going to be honest here and say that this is my third foray into HackerRank (first one was a work thing, second one was a quiz for a potential job opportunity) and I have to say that these test write-ups for what is expected is REALLY difficult to suss out.
No, I'm not a college graduate, no I don't have extensive experience in high level math, but I'm pretty good at reading comprehension and the way this stuff is written makes my head hurt trying to figure out what is expected.
And I've been doing code reading and writing for a living for the past 25 years.
Perhaps you guys could offer a tutorial on how to read the test descriptions given that the use of this site for applicant filtering seems to have become an industry standard?
Just a thought.
Thanks! Mike Poz
+ 8 comments I am sharing my solution below, feel free to leave comments please.
#!/usr/bin/python3 N = int(input()) W = set(map(int, input().split())) i = 0 while len(W) > 0: i += 1 m = min(W) W = W.difference(set(range(m, m + 5))) print(i)
+ 4 comments I think the question is a bit ambigious: "Minimum units with which Priyanka could buy all of toys", I think "Minimum group of units with which Priyanka could buy all of toys" would explain the problem statement better, and in the given example it would be [(1,2,3),(10),(17)] -> 3 groups of units
+ 5 comments import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } Arrays.sort(a); int cost=1; int p=a[0]; for(int i=0;i<n;i++){ if(a[i]>p+4){ cost=cost+1; p=a[i]; } } System.out.println(cost); } }
+ 6 comments my code friends
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for(int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); int cost = 0; for(int i = 0; i < n; i = upper_bound(v.begin(), v.end(), v[i]+4) - v.begin()) cost++; cout << cost; return 0; }
Sort 411 Discussions, By:
Please Login in order to post a comment