Solution: I was trying to solve the Leetcode 1996 number of weak characters using a simple traversing approach, but this only works on test cases. Description as follows: You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.
A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels. More formally, a character i is said to be weak if there exists another character (j) where attack[j] > attack[I] and defense[j] > defense[I].
Return the number of weak characters.
Working perfectly on sample testcase but why not on other like as: input: [[1,1],[2,1],[2,2],[1,2]] Expected output: 1 output recieved=0.
class Solution {
public int numberOfWeakCharacters(int[][] properties) {
int answer=0;
for(int i=0;i<properties.length-1;i++){
if((properties[i][0]<properties[i+1][0] && properties[i][1]<properties[i+1][1]) ||
(properties[i][0]>properties[i+1][0] && properties[i][1]>properties[i+1][1]))
++ans;
}
return answer;
}
}