Skip to content

Updated tags 224-918 #1935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2025
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
@@ -1,7 +1,7 @@
package g0201_0300.s0224_basic_calculator;

// #Hard #String #Math #Stack #Recursion #Top_Interview_150_Stack
// #2022_07_04_Time_3_ms_(98.92%)_Space_44.6_MB_(43.19%)
// #2025_03_09_Time_2_ms_(96.52%)_Space_45.07_MB_(23.63%)

public class Solution {
private int i = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g0201_0300.s0228_summary_ranges;

// #Easy #Array #Top_Interview_150_Intervals #2022_07_04_Time_0_ms_(100.00%)_Space_42.7_MB_(15.43%)
// #Easy #Array #Top_Interview_150_Intervals #2025_03_09_Time_0_ms_(100.00%)_Space_41.53_MB_(90.54%)

import java.util.ArrayList;
import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0201_0300/s0242_valid_anagram/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Easy #String #Hash_Table #Sorting #Data_Structure_I_Day_6_String
// #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Strings #Top_Interview_150_Hashmap
// #2022_07_05_Time_2_ms_(99.01%)_Space_42.4_MB_(91.86%)
// #2025_03_09_Time_2_ms_(97.76%)_Space_43.41_MB_(66.14%)

public class Solution {
public boolean isAnagram(String s, String t) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0201_0300/s0289_game_of_life/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0201_0300.s0289_game_of_life;

// #Medium #Array #Matrix #Simulation #Top_Interview_150_Matrix
// #2022_07_06_Time_0_ms_(100.00%)_Space_42.9_MB_(10.73%)
// #2025_03_09_Time_0_ms_(100.00%)_Space_41.90_MB_(24.53%)

public class Solution {
public void gameOfLife(int[][] board) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0201_0300/s0290_word_pattern/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0201_0300.s0290_word_pattern;

// #Easy #String #Hash_Table #Data_Structure_II_Day_7_String #Top_Interview_150_Hashmap
// #2022_07_06_Time_1_ms_(97.26%)_Space_40.4_MB_(85.78%)
// #2025_03_09_Time_0_ms_(100.00%)_Space_41.27_MB_(92.07%)

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,104 @@
package g0301_0400.s0373_find_k_pairs_with_smallest_sums;

// #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
// #2022_07_12_Time_59_ms_(46.79%)_Space_120.7_MB_(83.25%)
// #2025_03_09_Time_0_ms_(100.00%)_Space_57.66_MB_(90.88%)

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Solution {
private static class Node {
long sum;
List<Integer> al;
int index;

Node(int index, int num1, int num2) {
this.sum = (long) num1 + (long) num2;
this.al = new ArrayList<>();
this.al.add(num1);
this.al.add(num2);
this.index = index;
}
}

public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
PriorityQueue<Node> queue = new PriorityQueue<>((a, b) -> a.sum < b.sum ? -1 : 1);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < nums1.length && i < k; i++) {
queue.add(new Node(0, nums1[i], nums2[0]));
}
for (int i = 1; i <= k && !queue.isEmpty(); i++) {
Node cur = queue.poll();
res.add(cur.al);
int next = cur.index;
int lastNum1 = cur.al.get(0);
if (next + 1 < nums2.length) {
queue.add(new Node(next + 1, lastNum1, nums2[next + 1]));
return new AbstractList<List<Integer>>() {
private List<List<Integer>> pairs;

@Override
public List<Integer> get(int index) {
init();
return pairs.get(index);
}

@Override
public int size() {
init();
return pairs.size();
}

private void load() {
int n = nums1.length;
int m = nums2.length;
int left = nums1[0] + nums2[0];
int right = nums1[n - 1] + nums2[m - 1];
int middle;

while (left <= right) {
middle = (left + right) / 2;
long count = getCount(nums1, nums2, middle, k);
if (count < k) {
left = middle + 1;
} else if (count > k) {
right = middle - 1;
} else {
left = middle;
break;
}
}
getPairs(nums1, nums2, left, k);
}

private long getCount(int[] nums1, int[] nums2, int goal, int k) {
int prevRight = nums2.length - 1;
int count = 0;

for (int i = 0; i < nums1.length && nums1[i] + nums2[0] <= goal; i++) {
int left = 0;
int right = prevRight;
int position = -1;
while (left <= right) {
int middle = (right + left) / 2;
int sum = nums1[i] + nums2[middle];
if (sum <= goal) {
position = middle;
left = middle + 1;
} else {
right = middle - 1;
}
}
if (position >= 0) {
count += position + 1;
prevRight = position;
}
if (count > k) {
return count;
}
}
return count;
}

private void getPairs(int[] nums1, int[] nums2, int sum, int k) {
pairs = new ArrayList<>();
for (int item : nums1) {
for (int j = 0; j < nums2.length && item + nums2[j] < sum; j++) {
pairs.add(Arrays.asList(item, nums2[j]));
}
}
for (int value : nums1) {
for (int j = 0;
j < nums2.length && value + nums2[j] <= sum && pairs.size() < k;
j++) {
if (value + nums2[j] == sum) {
pairs.add(Arrays.asList(value, nums2[j]));
}
}
}
}

public void init() {
if (null == pairs) {
load();
}
}
}
return res;
};
}
}
2 changes: 1 addition & 1 deletion src/main/java/g0301_0400/s0383_ransom_note/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0301_0400.s0383_ransom_note;

// #Easy #String #Hash_Table #Counting #Data_Structure_I_Day_6_String #Top_Interview_150_Hashmap
// #2022_07_13_Time_1_ms_(99.97%)_Space_46_MB_(62.86%)
// #2025_03_09_Time_1_ms_(99.10%)_Space_44.62_MB_(86.13%)

public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Easy #String #Dynamic_Programming #Two_Pointers #Dynamic_Programming_I_Day_19
// #Level_1_Day_2_String #Udemy_Two_Pointers #Top_Interview_150_Two_Pointers
// #2022_07_13_Time_1_ms_(93.01%)_Space_42.2_MB_(32.57%)
// #2025_03_09_Time_1_ms_(93.13%)_Space_41.65_MB_(37.86%)

public class Solution {
public boolean isSubsequence(String s, String t) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0401_0500.s0427_construct_quad_tree;

// #Medium #Array #Tree #Matrix #Divide_and_Conquer #Top_Interview_150_Divide_and_Conquer
// #2022_07_16_Time_0_ms_(100.00%)_Space_42.6_MB_(89.45%)
// #2025_03_09_Time_0_ms_(100.00%)_Space_44.55_MB_(62.63%)

/*
// Definition for a QuadTree node.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0401_0500.s0433_minimum_genetic_mutation;

// #Medium #String #Hash_Table #Breadth_First_Search #Graph_Theory_I_Day_12_Breadth_First_Search
// #Top_Interview_150_Graph_BFS #2022_07_16_Time_1_ms_(90.95%)_Space_41.9_MB_(56.72%)
// #Top_Interview_150_Graph_BFS #2025_03_09_Time_0_ms_(100.00%)_Space_41.65_MB_(47.68%)

import java.util.ArrayList;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0401_0500.s0452_minimum_number_of_arrows_to_burst_balloons;

// #Medium #Array #Sorting #Greedy #Top_Interview_150_Intervals
// #2022_07_18_Time_84_ms_(71.26%)_Space_100.7_MB_(21.68%)
// #2025_03_09_Time_52_ms_(89.91%)_Space_68.86_MB_(77.92%)

import java.util.Arrays;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0501_0600/s0502_ipo/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0501_0600.s0502_ipo;

// #Hard #Array #Sorting #Greedy #Heap_Priority_Queue #Top_Interview_150_Heap
// #2022_07_24_Time_51_ms_(89.62%)_Space_101.7_MB_(47.03%)
// #2025_03_09_Time_64_ms_(97.22%)_Space_59.96_MB_(87.77%)

import java.util.Comparator;
import java.util.PriorityQueue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0501_0600.s0530_minimum_absolute_difference_in_bst;

// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
// #Top_Interview_150_Binary_Search_Tree #2022_07_28_Time_1_ms_(92.05%)_Space_45_MB_(70.03%)
// #Top_Interview_150_Binary_Search_Tree #2025_03_09_Time_0_ms_(100.00%)_Space_44.75_MB_(31.94%)

import com_github_leetcode.TreeNode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0601_0700.s0637_average_of_levels_in_binary_tree;

// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #Top_Interview_150_Binary_Tree_BFS #2022_03_21_Time_2_ms_(89.32%)_Space_44.7_MB_(77.73%)
// #Top_Interview_150_Binary_Tree_BFS #2025_03_09_Time_2_ms_(94.34%)_Space_45.93_MB_(25.94%)

import com_github_leetcode.TreeNode;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0901_1000.s0909_snakes_and_ladders;

// #Medium #Array #Breadth_First_Search #Matrix #Top_Interview_150_Graph_BFS
// #2022_03_28_Time_7_ms_(79.52%)_Space_47.7_MB_(58.43%)
// #2025_03_09_Time_4_ms_(95.81%)_Space_43.82_MB_(99.52%)

import java.util.LinkedList;
import java.util.Queue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Array #Dynamic_Programming #Divide_and_Conquer #Queue #Monotonic_Queue
// #Dynamic_Programming_I_Day_5 #Top_Interview_150_Kadane's_Algorithm
// #2022_03_29_Time_3_ms_(92.86%)_Space_64.3_MB_(40.27%)
// #2025_03_09_Time_2_ms_(99.34%)_Space_49.52_MB_(29.39%)

public class Solution {
private int kadane(int[] nums, int sign) {
Expand Down