Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 29753d4

Browse files
Updated tag for tasks 139-208
1 parent 2150e4f commit 29753d4

File tree

16 files changed

+48
-53
lines changed

16 files changed

+48
-53
lines changed

‎src/main/java/g0101_0200/s0139_word_break/Solution.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,36 @@
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table
44
// #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
55
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
6-
// #2022_06_24_Time_2_ms_(97.08%)_Space_42.1_MB_(90.92%)
6+
// #2024_11_15_Time_1_ms_(99.42%)_Space_42.1_MB_(80.42%)
77

8-
import java.util.HashSet;
98
import java.util.List;
10-
import java.util.Set;
119

1210
public class Solution {
11+
private Boolean[] memo;
12+
1313
public boolean wordBreak(String s, List<String> wordDict) {
14-
Set<String> set = new HashSet<>();
15-
int max = 0;
16-
boolean[] flag = new boolean[s.length() + 1];
17-
for (String st : wordDict) {
18-
set.add(st);
19-
if (max < st.length()) {
20-
max = st.length();
21-
}
22-
}
23-
for (int i = 1; i <= max; i++) {
24-
if (dfs(s, 0, i, max, set, flag)) {
25-
return true;
26-
}
27-
}
28-
return false;
14+
memo = new Boolean[s.length() + 1];
15+
return dp(s, 0, wordDict);
2916
}
3017

31-
private boolean dfs(String s, int start, int end, int max, Set<String> set, boolean[] flag) {
32-
if (!flag[end] && set.contains(s.substring(start, end))) {
33-
flag[end] = true;
34-
if (end == s.length()) {
35-
return true;
18+
public boolean dp(String s, int i, List<String> wordDict) {
19+
if (i == s.length()) {
20+
return true;
21+
}
22+
if (memo[i] != null) {
23+
return memo[i];
24+
}
25+
for (String word : wordDict) {
26+
int len = word.length();
27+
if (i + len > s.length() || !s.substring(i, i + len).equals(word)) {
28+
continue;
3629
}
37-
for (int i = 1; i <= max; i++) {
38-
if (end + i <= s.length() && dfs(s, end, end + i, max, set, flag)) {
39-
return true;
40-
}
30+
if (dp(s, i + len, wordDict)) {
31+
memo[i] = true;
32+
return true;
4133
}
4234
}
35+
memo[i] = false;
4336
return false;
4437
}
4538
}

‎src/main/java/g0101_0200/s0141_linked_list_cycle/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
44
// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
5-
// #2022_06_24_Time_0_ms_(100.00%)_Space_45.5_MB_(68.52%)
5+
// #2024_11_15_Time_0_ms_(100.00%)_Space_44.3_MB_(52.46%)
66

77
import com_github_leetcode.ListNode;
88

‎src/main/java/g0101_0200/s0142_linked_list_cycle_ii/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Hash_Table #Two_Pointers #Linked_List
44
// #Data_Structure_II_Day_10_Linked_List #Level_1_Day_4_Linked_List #Udemy_Linked_List
5-
// #Big_O_Time_O(N)_Space_O(1) #2022_06_24_Time_0_ms_(100.00%)_Space_42.3_MB_(98.70%)
5+
// #Big_O_Time_O(N)_Space_O(1) #2024_11_15_Time_0_ms_(100.00%)_Space_44.7_MB_(20.31%)
66

77
import com_github_leetcode.ListNode;
88

‎src/main/java/g0101_0200/s0146_lru_cache/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Design #Linked_List
44
// #Doubly_Linked_List #Udemy_Linked_List #Big_O_Time_O(1)_Space_O(capacity)
5-
// #2022_06_24_Time_87_ms_(50.80%)_Space_125.2_MB_(58.73%)
5+
// #2024_11_15_Time_40_ms_(98.20%)_Space_111.4_MB_(88.70%)
66

77
import java.util.HashMap;
88
import java.util.Map;

‎src/main/java/g0101_0200/s0148_sort_list/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
44
// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N))
5-
// #2022_06_24_Time_12_ms_(85.82%)_Space_76_MB_(43.84%)
5+
// #2024_11_15_Time_9_ms_(93.90%)_Space_56.9_MB_(37.47%)
66

77
import com_github_leetcode.ListNode;
88

‎src/main/java/g0101_0200/s0152_maximum_product_subarray/Solution.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
44
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
5-
// #Big_O_Time_O(N)_Space_O(1) #2024_07_03_Time_1_ms_(92.31%)_Space_44.6_MB_(75.65%)
5+
// #Big_O_Time_O(N)_Space_O(1) #2024_11_15_Time_1_ms_(92.74%)_Space_45_MB_(23.41%)
66

77
public class Solution {
88
public int maxProduct(int[] nums) {
9-
int currentMaxProd = nums[0];
10-
int currentMinProd = nums[0];
11-
int overAllMaxProd = nums[0];
12-
for (int i = 1; i < nums.length; i++) {
13-
if (nums[i] < 0) {
14-
int temp = currentMaxProd;
15-
currentMaxProd = currentMinProd;
16-
currentMinProd = temp;
9+
int overAllMaxProd = Integer.MIN_VALUE;
10+
int n = nums.length;
11+
int start = 1;
12+
int end = 1;
13+
for (int i = 0; i < n; i++) {
14+
if (start == 0) {
15+
start = 1;
1716
}
18-
currentMaxProd = Math.max(nums[i], nums[i] * currentMaxProd);
19-
currentMinProd = Math.min(nums[i], nums[i] * currentMinProd);
20-
overAllMaxProd = Math.max(overAllMaxProd, currentMaxProd);
17+
if (end == 0) {
18+
end = 1;
19+
}
20+
start = start * nums[i];
21+
end = end * nums[n - i - 1];
22+
overAllMaxProd = Math.max(overAllMaxProd, Math.max(start, end));
2123
}
2224
return overAllMaxProd;
2325
}

‎src/main/java/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
44
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
5-
// #2022_06_25_Time_0_ms_(100.00%)_Space_43.3_MB_(6.36%)
5+
// #2024_11_15_Time_0_ms_(100.00%)_Space_42.1_MB_(33.31%)
66

77
public class Solution {
88
private int findMinUtil(int[] nums, int l, int r) {

‎src/main/java/g0101_0200/s0155_min_stack/MinStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
44
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
5-
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2022_06_25_Time_3_ms_(100.00%)_Space_44.3_MB_(85.39%)
5+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2024_11_15_Time_4_ms_(96.54%)_Space_44.5_MB_(84.54%)
66

77
public class MinStack {
88
private static class Node {

‎src/main/java/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
44
// #Data_Structure_II_Day_11_Linked_List #Udemy_Linked_List #Big_O_Time_O(M+N)_Space_O(1)
5-
// #2022_06_25_Time_1_ms_(99.68%)_Space_55.1_MB_(63.42%)
5+
// #2024_11_15_Time_1_ms_(99.92%)_Space_48.4_MB_(68.45%)
66

77
import com_github_leetcode.ListNode;
88

‎src/main/java/g0101_0200/s0169_majority_element/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
44
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
5-
// #Big_O_Time_O(n)_Space_O(1) #2022_06_25_Time_1_ms_(100.00%)_Space_45.5_MB_(97.51%)
5+
// #Big_O_Time_O(n)_Space_O(1) #2024_11_15_Time_1_ms_(99.89%)_Space_52.8_MB_(64.33%)
66

77
public class Solution {
88
public int majorityElement(int[] arr) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /