updated Trie problem
This commit is contained in:
@@ -27,7 +27,7 @@ public class test {
|
||||
for (int i = 0; i < n; i++) {
|
||||
/* Remove old value (if necessary) */
|
||||
if (i >= m) {
|
||||
int old = deque.remove();
|
||||
int old = deque.removeFirst();
|
||||
if (map.get(old) == 1) {
|
||||
map.remove(old);
|
||||
} else {
|
||||
@@ -37,7 +37,7 @@ public class test {
|
||||
|
||||
/* Add new value */
|
||||
int num = scan.nextInt();
|
||||
deque.add(num);
|
||||
deque.addLast(num);
|
||||
map.merge(num, 1, Integer::sum);
|
||||
|
||||
max = Math.max(max, map.size());
|
||||
|
||||
@@ -64,10 +64,9 @@ public class Solution {
|
||||
}
|
||||
|
||||
class StudentComparator implements Comparator<Student> {
|
||||
double epsilon = 0.001; // since we shouldn't use "==" with doubles
|
||||
@Override
|
||||
public int compare(Student s1, Student s2) {
|
||||
if (Math.abs(s1.getCgpa() - s2.getCgpa()) > epsilon) {
|
||||
if (Double.compare(s1.getCgpa(), s2.getCgpa()) != 0) {
|
||||
return s1.getCgpa() < s2.getCgpa() ? 1 : -1; // descending order
|
||||
} else if ( ! s1.getFname().equals(s2.getFname())) {
|
||||
return s1.getFname().compareTo(s2.getFname());
|
||||
|
||||
@@ -4,16 +4,15 @@
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Our subarray must be contiguous, so we have O(n^2) contiguous subarrays
|
||||
// A subarray must be contiguous. There are O(n^2) contiguous subarrays.
|
||||
|
||||
// Time Complexity: O(n^2)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int size = scan.nextInt();
|
||||
int [] array = new int[size];
|
||||
int[] array = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = scan.nextInt();
|
||||
}
|
||||
@@ -22,17 +21,16 @@ public class Solution {
|
||||
System.out.println(negativeSubarrays(array));
|
||||
}
|
||||
|
||||
private static int negativeSubarrays(int [] array) {
|
||||
private static int negativeSubarrays(int[] array) {
|
||||
int count = 0;
|
||||
int sum = 0;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
int sum = 0;
|
||||
for (int j = i; j < array.length; j++) {
|
||||
sum += array[j];
|
||||
if (sum < 0) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
sum = 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user