Skip to content

Commit

Permalink
Solve special array and next letters
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Jun 8, 2023
1 parent 0f0baed commit 34737db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Binary file modified out/production/java-interview-questions/Main.class
Binary file not shown.
5 changes: 2 additions & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ public class Main {

public static void main(String[] args) {

char[] arr = {'x','x','y', 'y'};
char target = 'x';
char result = Searching.nextGreatestLetter(arr, target);
int[][] arr = {{4,3,2,-1},{3,2,1,-1}};
int result = Searching.countNegativeNumbers(arr);
System.out.println(result);
}

Expand Down
21 changes: 21 additions & 0 deletions src/intermediate/Searching.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,25 @@ else if (count < middle)
}
return -1;
}
// https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/description/
public static int countNegativeNumbers(int[][] nums){
int count = 0;

for (int i = 0; i < nums.length; i++){
int start = 0, end = nums[i].length - 1;

while (start <= end){
int middle = start + (end - start) / 2;

if (nums[i][middle] < 0){
count += start - middle + 1;
end = middle - 1;
}
else{
start = middle + 1;
}
}
}
return count;
}
}

0 comments on commit 34737db

Please sign in to comment.