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.
- Prepare
- Algorithms
- Greedy
- Priyanka and Toys
- Discussions
Priyanka and Toys
Priyanka and Toys
+ 0 comments JAVA
public static int toys(List<Integer> w) { System.out.println(w); Collections.sort(w); int containers = 1; int tempLimit = w.get(0); for (int i = 1; i < w.size(); i++) { if (w.get(i) > tempLimit+4) { tempLimit = w.get(i); containers++; } } return containers; }
+ 0 comments C#
public static int toys(List<int> w) { w.Sort(); var result = 0; for(int i = 0; i< w.Count;) { var currMax = w[i] + 4; while(i < w.Count && w[i] <= currMax) { i++; } result++; } return result; }
+ 0 comments c#:
public static int toys(List<int> w) { var containerCount = 1; w.Sort(); var minWeight = w[0]; for (var i = 0; i < w.Count; i++) { if (w[i] - minWeight <= 4) continue; containerCount++; minWeight = w[i]; } return containerCount; }
+ 0 comments def toys(w):
w.sort() l=w[:] c=0 while len(l)!=0 c+=1 w=l[:] small=w[0] for e in w: if e<=small+4: l.remove(e) return c
+ 0 comments java solution
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; class Result { /* * Complete the 'toys' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY w as parameter. */ public static int toys(List<Integer> w) { // Write your code here int c=1; int j=0; Collections.sort(w); for(int i=1;i<w.size();i++){ if(w.get(i)-w.get(j)>=5){ c++; j=i; System.out.println(j); } } return c; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int n = Integer.parseInt(bufferedReader.readLine().trim()); String[] wTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); List<Integer> w = new ArrayList<>(); for (int i = 0; i < n; i++) { int wItem = Integer.parseInt(wTemp[i]); w.add(wItem); } int result = Result.toys(w); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } }
Load more conversations
Sort 465 Discussions, By:
Please Login in order to post a comment