Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MedianFinder {

// initialize your data structure here.
public MedianFinder() {
maxHeap = new PriorityQueue<>((a, b) -> (b - a));
maxHeap = new PriorityQueue<>((a, b) -> (Integer.compare(b, a)));
minHeap = new PriorityQueue<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Solution {
public int[] topKFrequent(int[] nums, int k) {
Arrays.sort(nums);
// Min heap of <number, frequency>
Queue<int[]> queue = new PriorityQueue<>(k + 1, (a, b) -> (a[1] - b[1]));
Queue<int[]> queue = new PriorityQueue<>(k + 1, (a, b) -> (Integer.compare(a[1], b[1])));
// Filter with min heap
int j = 0;
for (int i = 0; i <= nums.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Cell(int r, int c, int v) {

@Override
public int compareTo(Cell other) {
return value - other.value;
return Integer.compare(value, other.value);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/g0501_0600/s0502_ipo/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
PriorityQueue<int[]> minCapital =
new PriorityQueue<>(Comparator.comparingInt((int[] a) -> a[1]));
PriorityQueue<int[]> maxProfit = new PriorityQueue<>((int[] a, int[] b) -> b[0] - a[0]);
PriorityQueue<int[]> maxProfit =
new PriorityQueue<>((int[] a, int[] b) -> Integer.compare(b[0], a[0]));
for (int i = 0; i < profits.length; i++) {
if (w >= capital[i]) {
maxProfit.offer(new int[] {profits[i], capital[i]});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
public class Solution {
public int scheduleCourse(int[][] courses) {
// Sort the courses based on their deadline date.
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
Arrays.sort(courses, (a, b) -> Integer.compare(a[1], b[1]));
// Only the duration is stored. We don't care which course
// is the longest, we only care about the total courses can
// be taken.
// If the question wants the course ids to be returned.
// Consider use a Pair<Duration, CourseId> int pair.
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
// Total time consumed.
int time = 0;
// At the given time `course`, the overall "time limit" is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static class Triplet implements Comparable<Triplet> {
}

public int compareTo(Triplet obj) {
return this.value - obj.value;
return Integer.compare(this.value, obj.value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public int findLongestChain(int[][] pairs) {
if (pairs.length == 1) {
return 1;
}
Arrays.sort(pairs, (a, b) -> a[1] - b[1]);
Arrays.sort(pairs, (a, b) -> Integer.compare(a[1], b[1]));
int min = pairs[0][1];
int max = 1;
for (int i = 1; i < pairs.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<String> topKFrequent(String[] words, int k) {
new TreeSet<>(
(e1, e2) -> {
if (e1.getValue().intValue() != e2.getValue().intValue()) {
return e2.getValue() - e1.getValue();
return Integer.compare(e2.getValue(), e1.getValue());
} else {
return e1.getKey().compareToIgnoreCase(e2.getKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Meeting(int start, int end) {

@Override
public int compareTo(Meeting anotherMeeting) {
return this.start - anotherMeeting.start;
return Integer.compare(this.start, anotherMeeting.start);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static class Exp {

public Exp(Exp from) {
this.exps = new LinkedList<>();
this.op = null;
this.parent = from;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public int minRefuelStops(int target, int startFuel, int[][] stations) {
} else if (stations == null || stations.length == 0) {
return -1;
}
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]);
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(b[1], a[1]));
int start = 0;
int end = stations.length;
int currentFuel = startFuel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Solution {
public int twoCitySchedCost(int[][] costs) {
Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1])));
Arrays.sort(costs, (a, b) -> Long.compare((long) a[0] - a[1], (long) b[0] - b[1]));
int cost = 0;
for (int i = 0; i < costs.length; i++) {
if (i < costs.length / 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
for (int stone : stones) {
heap.offer(stone);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String rankTeams(String[] votes) {
(o1, o2) -> {
for (int i = 0; i < 26; i++) {
if (o1.count[i] != o2.count[i]) {
return o2.count[i] - o1.count[i];
return Integer.compare(o2.count[i], o1.count[i]);
}
}
return o1.c - o2.c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
engineers[i][0] = speed[i];
engineers[i][1] = efficiency[i];
}
Arrays.sort(engineers, (engineer1, engineer2) -> engineer2[1] - engineer1[1]);
Arrays.sort(
engineers, (engineer1, engineer2) -> Integer.compare(engineer2[1], engineer1[1]));
long speedSum = 0;
long maximumPerformance = 0;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public int kthSmallest(int[][] mat, int k) {
new TreeSet<>(
(o1, o2) -> {
if (o1[0] != o2[0]) {
return o1[0] - o2[0];
return Integer.compare(o1[0], o2[0]);
} else {
for (int i = 1; i < o1.length; i++) {
if (o1[i] != o2[i]) {
return o1[i] - o2[i];
return Integer.compare(o1[i], o2[i]);
}
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ private static class Angle implements Comparable<Angle> {

@Override
public int compareTo(Angle o) {
if (a > o.a) {
return 1;
} else if (a < o.a) {
return -1;
int angleComparison = Double.compare(a, o.a);
if (angleComparison != 0) {
return angleComparison;
} else if (enter == o.enter) {
return 0;
} else if (enter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Solution {
public int maxNumEdgesToRemove(int n, int[][] edges) {
Arrays.sort(edges, (a, b) -> (b[0] - a[0]));
Arrays.sort(edges, (a, b) -> (Integer.compare(b[0], a[0])));
int[] alice = new int[n + 1];
int[] rankAlice = new int[n + 1];
int[] bob = new int[n + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int getV() {

@Override
public int compare(Pair p1, Pair p2) {
return p1.dis - p2.dis;
return Integer.compare(p1.dis, p2.dis);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static class Pair implements Comparable<Pair> {
}

public int compareTo(Pair o) {
return this.diff - o.diff;
return Integer.compare(this.diff, o.diff);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Solution {
public int minimumDeviation(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
int min = Integer.MAX_VALUE;
for (int num : nums) {
if (num % 2 == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g1601_1700/s1686_stone_game_vi/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static class Pair implements Comparable<Pair> {
}

public int compareTo(Pair p) {
return p.sum - this.sum;
return Integer.compare(p.sum, this.sum);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public int maxHeight(int[][] arr) {
arr,
(a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
return Integer.compare(a[0], b[0]);
} else if (a[1] != b[1]) {
return a[1] - b[1];
return Integer.compare(a[1], b[1]);
}
return a[2] - b[2];
return Integer.compare(a[2], b[2]);
});
int ans = 0;
int[] dp = new int[arr.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Solution {
public int eatenApples(int[] apples, int[] days) {
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
int eatenApples = 0;
for (int i = 0; i < apples.length || !minHeap.isEmpty(); i++) {
if (i < apples.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static class Pair implements Comparable<Pair> {
}

public int compareTo(Pair o) {
return this.cwt - o.cwt;
return Integer.compare(this.cwt, o.cwt);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static class Order {

public int getNumberOfBacklogOrders(int[][] orders) {
PriorityQueue<Order> sell = new PriorityQueue<>(Comparator.comparingInt(a -> a.price));
PriorityQueue<Order> buy = new PriorityQueue<>((a, b) -> b.price - a.price);
PriorityQueue<Order> buy = new PriorityQueue<>((a, b) -> Integer.compare(b.price, a.price));
for (int[] order : orders) {
int price = order[0];
int amount = order[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public int[] getOrder(int[][] tasks1) {
new PriorityQueue<>(
(a, b) -> {
if (a[1] == b[1]) {
return a[2] - b[2];
return Integer.compare(a[2], b[2]);
} else {
return a[1] - b[1];
return Integer.compare(a[1], b[1]);
}
});
int time = tasks[0][0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public int[] assignTasks(int[] servers, int[] tasks) {
for (int i = 0; i < servers.length; i++) {
serverq.offer(i);
}
PriorityQueue<int[]> activetaskq = new PriorityQueue<>((i1, i2) -> i1[1] - i2[1]);
PriorityQueue<int[]> activetaskq =
new PriorityQueue<>((i1, i2) -> Integer.compare(i1[1], i2[1]));
int time = 0;
int[] res = new int[tasks.length];
for (int i = 0; i < tasks.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public int maxProduct(String s) {
recur(chars, new State(i, i, 0, mask), list, visited);
recur(chars, new State(i, i + 1, 0, mask), list, visited);
}
list.sort((a, b) -> b.cnt - a.cnt);
list.sort((a, b) -> Integer.compare(b.cnt, a.cnt));
int res = 1;
Set<Integer> explored = new HashSet<>();
for (int i = 0; i < list.size() - 1; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Solution {
public int maxTwoEvents(int[][] events) {
Arrays.sort(events, (a, b) -> a[0] - b[0]);
Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));
int[] max = new int[events.length];
for (int i = events.length - 1; i >= 0; i--) {
if (i == events.length - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class Solution {
public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
Arrays.sort(meetings, ((a, b) -> a[2] - b[2]));
Arrays.sort(meetings, ((a, b) -> Integer.compare(a[2], b[2])));
UF uf = new UF(n);
// base
uf.union(0, firstPerson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public int[] maxSubsequence(int[] nums, int k) {
// Loop from 0 to n-1 and add element in result if set contains those index
// For ex. set has index 3,5,6 Just add those element. Order will be maintained
// We are defining the min priority queue
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> (Integer.compare(a[0], b[0])));
// Add element with index to priority queue
for (int i = 0; i < nums.length; i++) {
q.offer(new int[] {nums[i], i});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SORTracker() {
new TreeSet<>(
(a, b) -> {
if (a.score != b.score) {
return b.getScore() - a.getScore();
return Integer.compare(b.getScore(), a.getScore());
} else {
return a.getName().compareTo(b.getName());
}
Expand All @@ -42,7 +42,7 @@ public SORTracker() {
new TreeSet<>(
(a, b) -> {
if (a.score != b.score) {
return b.getScore() - a.getScore();
return Integer.compare(b.getScore(), a.getScore());
} else {
return a.getName().compareTo(b.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static class Seed implements Comparable<Seed> {
}

public int compareTo(Seed s) {
return this.growTime - s.growTime;
return Integer.compare(this.growTime, s.growTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public List<List<Integer>> highestRankedKItems(
if (distDiff == 0) {
int priceDiff = a.price - b.price;
if (priceDiff == 0) {
int rowDiff = a.row - b.row;
int rowDiff = Integer.compare(a.row, b.row);
if (rowDiff == 0) {
return a.col - b.col;
return Integer.compare(a.col, b.col);
}
return rowDiff;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Solution {
public long minimumDifference(int[] nums) {
int n = nums.length / 3;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
long[] leftMemo = new long[nums.length];
long[] rightMemo = new long[nums.length];
long current = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public int[] sortJumbled(int[] mapping, int[] nums) {
if (retval != 0) {
return retval;
}
return a.index - b.index;
return Integer.compare(a.index, b.index);
});
int[] retval = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public int minimumLines(int[][] stockPrices) {
if (stockPrices.length == 1) {
return 0;
}
Arrays.sort(stockPrices, (a, b) -> a[0] - b[0]);
Arrays.sort(stockPrices, (a, b) -> Integer.compare(a[0], b[0]));
// multiply with 1.0 to make it double and multiply with 100 for making it big so that
// difference won't come out to be very less and after division it become 0.
// failing for one of the case without multiply 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public int minimumObstacles(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int[][] dirs = new int[][] {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
Queue<State> q = new PriorityQueue<>((a, b) -> a.removed - b.removed);
Queue<State> q = new PriorityQueue<>((a, b) -> Integer.compare(a.removed, b.removed));
q.add(new State(0, 0, 0));
boolean[][] visited = new boolean[n][m];
visited[0][0] = true;
Expand Down
Loading