It's a stupid question. Don't bother optimize with sorting or anything.
Just brute force is more than sufficient.
Nothing more to say...sigh...
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 { | |
/** | |
* @param ages: The ages | |
* @return: The answer | |
*/ | |
public int friendRequest(int[] ages) { | |
// Write your code here | |
if (ages == null || ages.length == 0) { | |
return 0; | |
} | |
// Arrays.sort(ages); | |
int answer = 0; | |
for (int b = 0; b < ages.length; b++) { | |
int delta = 0; | |
if (ages[b] >= 100) { | |
for (int a = 0; a < ages.length; a++) { | |
if (a == b) { | |
continue; | |
} | |
// System.out.println("a, b = " + a + ", " + b + "; ages[a], ages[b] = " + ages[a] + ", " + ages[b]); | |
if (ages[a] < ages[b] || ages[a] / 2 + 7 >= ages[b]) { | |
} else { | |
delta++; | |
} | |
} | |
} else { | |
for (int a = 0; a < ages.length; a++) { | |
if (a == b) { | |
continue; | |
} | |
// System.out.println("a, b = " + a + ", " + b + "; ages[a], ages[b] = " + ages[a] + ", " + ages[b]); | |
if (ages[a] > 100 || ages[a] < ages[b] || ages[a] / 2 + 7 >= ages[b]) { | |
} else { | |
delta++; | |
} | |
} | |
} | |
// System.out.println(delta); | |
answer += delta; | |
} | |
return answer; | |
} | |
} |
No comments:
Post a Comment