Last active
May 29, 2023 01:52
-
-
Save itugs/76b05401dd662412990ce1e8b846e235 to your computer and use it in GitHub Desktop.
HackerCards for Leanne
This file contains 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
public class Solution { | |
static int[] hackerCards(int[] collection, int d) { | |
List<Integer> result = new ArrayList<>(); | |
int s, e; | |
for(int idx = 0; idx <= collection.length; idx++) { | |
if (idx == 0) s = 1; else s = collection[idx-1] + 1; | |
if (idx != collection.length) e = collection[idx]; else e = Integer.MAX_VALUE; | |
if (d < s) break; | |
for (int k = s; k < e; k++) { | |
if (k <= d) { | |
result.add(k); | |
d -= k; | |
} else { | |
break; | |
} | |
} | |
} | |
return result.stream().mapToInt(i->i).toArray(); | |
} | |
} |
can you give explaination for this?
I too would like an explanation of the code. I recently had to do this problem and would like to compare my answer to yours!
Will the input Collections come in ascending order?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please explain the solution ?