diff --git a/README.md b/README.md index dacb102..9542c5a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LeetCode-in-All -[![Maven Central](https://img.shields.io/maven-central/v/com.github.javadev/leetcode-in-all?style=flat-square)](https://central.sonatype.com/artifact/com.github.javadev/leetcode-in-all/1.8) +[![Maven Central](https://img.shields.io/maven-central/v/com.github.javadev/leetcode-in-all?style=flat-square)](https://central.sonatype.com/artifact/com.github.javadev/leetcode-in-all/1.9) [![Java CI](https://github.com/javadev/LeetCode-in-All/actions/workflows/maven.yml/badge.svg)](https://github.com/javadev/LeetCode-in-All/actions/workflows/maven.yml) [![CodeQL](https://github.com/javadev/LeetCode-in-All/actions/workflows/codeql.yml/badge.svg)](https://github.com/javadev/LeetCode-in-All/actions/workflows/codeql.yml) [![MIT License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/javadev/LeetCode-in-All/blob/main/LICENSE) diff --git a/pom.xml b/pom.xml index 79f1c5f..c8da0dc 100644 --- a/pom.xml +++ b/pom.xml @@ -71,7 +71,7 @@ org.codehaus.mojo build-helper-maven-plugin - 3.6.0 + 3.6.1 add-source diff --git a/src/main/go/g0001_0100/s0001_two_sum/readme.md b/src/main/go/g0001_0100/s0001_two_sum/readme.md index c4ae4e5..d51bd70 100644 --- a/src/main/go/g0001_0100/s0001_two_sum/readme.md +++ b/src/main/go/g0001_0100/s0001_two_sum/readme.md @@ -35,4 +35,4 @@ You can return the answer in any order. * -109 <= target <= 109 * **Only one valid answer exists.** -**Follow-up:** Can you come up with an algorithm that is less than O(n2) time complexity? +**Follow-up:** Can you come up with an algorithm that is less than O(n2) time complexity? \ No newline at end of file diff --git a/src/main/go/g0001_0100/s0001_two_sum/solution.go b/src/main/go/g0001_0100/s0001_two_sum/solution.go index 2ea7f63..3e8d770 100644 --- a/src/main/go/g0001_0100/s0001_two_sum/solution.go +++ b/src/main/go/g0001_0100/s0001_two_sum/solution.go @@ -1,8 +1,9 @@ package s0001_two_sum // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table -// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n) -// #2024_01_28_Time_3_ms_(93.85%)_Space_4.2_MB_(58.64%) +// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Top_Interview_150_Hashmap +// #Big_O_Time_O(n)_Space_O(n) #AI_can_be_used_to_solve_the_task +// #2025_04_27_Time_0_ms_(100.00%)_Space_5.85_MB_(61.87%) func twoSum(nums []int, target int) []int { indexMap := make(map[int]int) diff --git a/src/main/go/g0001_0100/s0002_add_two_numbers/solution.go b/src/main/go/g0001_0100/s0002_add_two_numbers/solution.go index 983095e..8a4a769 100644 --- a/src/main/go/g0001_0100/s0002_add_two_numbers/solution.go +++ b/src/main/go/g0001_0100/s0002_add_two_numbers/solution.go @@ -2,7 +2,8 @@ package s0002_add_two_numbers // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion // #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15 -// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_03_05_Time_4_ms_(84.60%)_Space_4.4_MB_(47.97%) +// #Top_Interview_150_Linked_List #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) +// #AI_can_be_used_to_solve_the_task #2025_04_27_Time_0_ms_(100.00%)_Space_6.17_MB_(92.78%) type ListNode struct { Val int @@ -20,7 +21,6 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { dummyHead := &ListNode{} p, q, curr := l1, l2, dummyHead carry := 0 - for p != nil || q != nil { x, y := 0, 0 if p != nil { @@ -31,16 +31,13 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { y = q.Val q = q.Next } - sum := carry + x + y carry = sum / 10 curr.Next = &ListNode{Val: sum % 10} curr = curr.Next } - if carry > 0 { curr.Next = &ListNode{Val: carry} } - return dummyHead.Next } diff --git a/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md b/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md index 3967809..f0c114d 100644 --- a/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md +++ b/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md @@ -2,7 +2,7 @@ Medium -Given a string `s`, find the length of the **longest** **substring** without repeating characters. +Given a string `s`, find the length of the **longest** **substring** without duplicate characters. **Example 1:** diff --git a/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.go b/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.go index e407884..5ba2d15 100644 --- a/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.go +++ b/src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.go @@ -2,7 +2,8 @@ package s0003_longest_substring_without_repeating_characters // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window // #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings -// #Big_O_Time_O(n)_Space_O(1) #2024_03_05_Time_0_ms_(100.00%)_Space_2.5_MB_(98.66%) +// #Top_Interview_150_Sliding_Window #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.45_MB_(94.51%) func lengthOfLongestSubstring(s string) int { lastIndices := make([]int, 256) diff --git a/src/main/go/g0001_0100/s0004_median_of_two_sorted_arrays/solution.go b/src/main/go/g0001_0100/s0004_median_of_two_sorted_arrays/solution.go index ed0924c..4703adc 100644 --- a/src/main/go/g0001_0100/s0004_median_of_two_sorted_arrays/solution.go +++ b/src/main/go/g0001_0100/s0004_median_of_two_sorted_arrays/solution.go @@ -1,26 +1,22 @@ package s0004_median_of_two_sorted_arrays // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer -// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2024_03_05_Time_9_ms_(72.04%)_Space_4.8_MB_(67.69%) +// #Top_Interview_150_Binary_Search #Big_O_Time_O(log(min(N,M)))_Space_O(1) +// #AI_can_be_used_to_solve_the_task #2025_04_27_Time_0_ms_(100.00%)_Space_6.51_MB_(81.58%) func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { if len(nums1) > len(nums2) { nums1, nums2 = nums2, nums1 } - x, y := len(nums1), len(nums2) low, high := 0, x - for low <= high { partitionX := (low + high) / 2 partitionY := (x+y+1)/2 - partitionX - maxLeftX := getIntValue(nums1, partitionX-1) minRightX := getIntValue(nums1, partitionX) - maxLeftY := getIntValue(nums2, partitionY-1) minRightY := getIntValue(nums2, partitionY) - if maxLeftX <= minRightY && maxLeftY <= minRightX { if (x+y)%2 == 0 { return float64(max(maxLeftX, maxLeftY)+min(minRightX, minRightY)) / 2 @@ -33,7 +29,6 @@ func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { low = partitionX + 1 } } - return 0.0 } diff --git a/src/main/go/g0001_0100/s0005_longest_palindromic_substring/solution.go b/src/main/go/g0001_0100/s0005_longest_palindromic_substring/solution.go index a6c095b..930db9e 100644 --- a/src/main/go/g0001_0100/s0005_longest_palindromic_substring/solution.go +++ b/src/main/go/g0001_0100/s0005_longest_palindromic_substring/solution.go @@ -2,8 +2,8 @@ package s0005_longest_palindromic_substring // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming // #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming -// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n) -// #2024_03_05_Time_0_ms_(100.00%)_Space_3.8_MB_(32.56%) +// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Top_Interview_150_Multidimensional_DP +// #Big_O_Time_O(n)_Space_O(n) #2025_04_27_Time_0_ms_(100.00%)_Space_5.76_MB_(29.62%) func longestPalindrome(s string) string { newStr := make([]byte, len(s)*2+1) @@ -15,7 +15,6 @@ func longestPalindrome(s string) string { dp := make([]int, len(newStr)) friendCenter, friendRadius := 0, 0 lpsCenter, lpsRadius := 0, 0 - for i := 0; i < len(newStr); i++ { if friendCenter+friendRadius > i { dp[i] = min(dp[2*friendCenter-i], (friendCenter+friendRadius)-i) diff --git a/src/main/go/g0001_0100/s0006_zigzag_conversion/solution.go b/src/main/go/g0001_0100/s0006_zigzag_conversion/solution.go index b7860ce..ea69c93 100644 --- a/src/main/go/g0001_0100/s0006_zigzag_conversion/solution.go +++ b/src/main/go/g0001_0100/s0006_zigzag_conversion/solution.go @@ -1,6 +1,7 @@ package s0006_zigzag_conversion -// #Medium #String #2024_03_05_Time_0_ms_(100.00%)_Space_4_MB_(86.83%) +// #Medium #String #Top_Interview_150_Array/String +// #2025_04_27_Time_0_ms_(100.00%)_Space_5.75_MB_(95.79%) func convert(s string, numRows int) string { topJump := (numRows-2)*2 + 2 diff --git a/src/main/go/g0001_0100/s0007_reverse_integer/solution.go b/src/main/go/g0001_0100/s0007_reverse_integer/solution.go index ee5fbc2..f354578 100644 --- a/src/main/go/g0001_0100/s0007_reverse_integer/solution.go +++ b/src/main/go/g0001_0100/s0007_reverse_integer/solution.go @@ -1,7 +1,7 @@ package s0007_reverse_integer // #Medium #Top_Interview_Questions #Math #Udemy_Integers -// #2024_03_07_Time_0_ms_(100.00%)_Space_2.2_MB_(46.99%) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.03_MB_(24.37%) import "math" diff --git a/src/main/go/g0001_0100/s0008_string_to_integer_atoi/solution.go b/src/main/go/g0001_0100/s0008_string_to_integer_atoi/solution.go index 99f0da8..be4df2d 100644 --- a/src/main/go/g0001_0100/s0008_string_to_integer_atoi/solution.go +++ b/src/main/go/g0001_0100/s0008_string_to_integer_atoi/solution.go @@ -1,6 +1,6 @@ package s0008_string_to_integer_atoi -// #Medium #Top_Interview_Questions #String #2024_03_07_Time_0_ms_(100.00%)_Space_2.3_MB_(27.02%) +// #Medium #Top_Interview_Questions #String #2025_04_27_Time_0_ms_(100.00%)_Space_4.07_MB_(99.22%) import ( "math" diff --git a/src/main/go/g0001_0100/s0009_palindrome_number/solution.go b/src/main/go/g0001_0100/s0009_palindrome_number/solution.go index fd71453..edd0291 100644 --- a/src/main/go/g0001_0100/s0009_palindrome_number/solution.go +++ b/src/main/go/g0001_0100/s0009_palindrome_number/solution.go @@ -1,6 +1,7 @@ package s0009_palindrome_number -// #Easy #Math #Udemy_Integers #2024_03_07_Time_0_ms_(100.00%)_Space_4.3_MB_(99.46%) +// #Easy #Math #Udemy_Integers #Top_Interview_150_Math +// #2025_04_27_Time_0_ms_(100.00%)_Space_5.96_MB_(99.06%) func isPalindrome(x int) bool { if x < 10 { diff --git a/src/main/go/g0001_0100/s0010_regular_expression_matching/solution.go b/src/main/go/g0001_0100/s0010_regular_expression_matching/solution.go index 110d80c..c120455 100644 --- a/src/main/go/g0001_0100/s0010_regular_expression_matching/solution.go +++ b/src/main/go/g0001_0100/s0010_regular_expression_matching/solution.go @@ -1,8 +1,7 @@ package s0010_regular_expression_matching -// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion -// #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n) -// #2024_03_07_Time_0_ms_(100.00%)_Space_2.3_MB_(36.02%) +// #Hard #Top_Interview_Questions #String #Dynamic_Programming #Recursion #Udemy_Dynamic_Programming +// #Big_O_Time_O(m*n)_Space_O(m*n) #2025_04_27_Time_0_ms_(100.00%)_Space_4.20_MB_(84.42%) func isMatch(s string, p string) bool { m, n := len(s), len(p) diff --git a/src/main/go/g0001_0100/s0011_container_with_most_water/solution.go b/src/main/go/g0001_0100/s0011_container_with_most_water/solution.go index 7032779..946311b 100644 --- a/src/main/go/g0001_0100/s0011_container_with_most_water/solution.go +++ b/src/main/go/g0001_0100/s0011_container_with_most_water/solution.go @@ -1,26 +1,23 @@ package s0011_container_with_most_water // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers -// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1) -// #2024_03_07_Time_60_ms_(94.99%)_Space_8.3_MB_(24.10%) +// #LeetCode_75_Two_Pointers #Algorithm_II_Day_4_Two_Pointers #Top_Interview_150_Two_Pointers +// #Big_O_Time_O(n)_Space_O(1) #2025_04_27_Time_0_ms_(100.00%)_Space_9.30_MB_(86.74%) func maxArea(height []int) int { l, r := 0, len(height)-1 maxArea := 0 - for l < r { area := min(height[l], height[r]) * (r - l) if area > maxArea { maxArea = area } - if height[l] < height[r] { l++ } else { r-- } } - return maxArea } diff --git a/src/main/go/g0001_0100/s0015_3sum/solution.go b/src/main/go/g0001_0100/s0015_3sum/solution.go index 6ad0964..f6bbbf0 100644 --- a/src/main/go/g0001_0100/s0015_3sum/solution.go +++ b/src/main/go/g0001_0100/s0015_3sum/solution.go @@ -2,7 +2,8 @@ package s0015_3sum // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers // #Data_Structure_II_Day_1_Array #Algorithm_II_Day_3_Two_Pointers #Udemy_Two_Pointers -// #Big_O_Time_O(n*log(n))_Space_O(n^2) #2024_03_07_Time_37_ms_(90.03%)_Space_7.9_MB_(46.68%) +// #Top_Interview_150_Two_Pointers #Big_O_Time_O(n*log(n))_Space_O(n^2) +// #2025_04_27_Time_12_ms_(99.83%)_Space_9.72_MB_(48.62%) import "sort" diff --git a/src/main/go/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.go b/src/main/go/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.go index 0e6fdcf..66e7d4c 100644 --- a/src/main/go/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.go +++ b/src/main/go/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.go @@ -1,8 +1,9 @@ package s0017_letter_combinations_of_a_phone_number // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Backtracking -// #Algorithm_II_Day_11_Recursion_Backtracking #Udemy_Backtracking/Recursion -// #Big_O_Time_O(4^n)_Space_O(n) #2024_03_07_Time_0_ms_(100.00%)_Space_2.1_MB_(87.39%) +// #LeetCode_75_Backtracking #Algorithm_II_Day_11_Recursion_Backtracking +// #Udemy_Backtracking/Recursion #Top_Interview_150_Backtracking #Big_O_Time_O(4^n)_Space_O(n) +// #2025_04_27_Time_0_ms_(100.00%)_Space_3.96_MB_(93.28%) func letterCombinations(digits string) []string { if len(digits) == 0 { @@ -20,7 +21,6 @@ func findCombinations(start int, nums string, letters []string, curr *string, an *ans = append(*ans, *curr) return } - for i := start; i < len(nums); i++ { n := int(nums[i] - '0') for j := 0; j < len(letters[n]); j++ { diff --git a/src/main/go/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.go b/src/main/go/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.go index ed71cc7..5fd6b95 100644 --- a/src/main/go/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.go +++ b/src/main/go/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.go @@ -1,8 +1,8 @@ package s0019_remove_nth_node_from_end_of_list // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Linked_List -// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Big_O_Time_O(L)_Space_O(L) -// #2024_03_07_Time_0_ms_(100.00%)_Space_2.2_MB_(19.66%) +// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Top_Interview_150_Linked_List +// #Big_O_Time_O(L)_Space_O(L) #2025_04_27_Time_0_ms_(100.00%)_Space_4.16_MB_(48.09%) type ListNode struct { Val int diff --git a/src/main/go/g0001_0100/s0020_valid_parentheses/solution.go b/src/main/go/g0001_0100/s0020_valid_parentheses/solution.go index 77578e6..ab32e0a 100644 --- a/src/main/go/g0001_0100/s0020_valid_parentheses/solution.go +++ b/src/main/go/g0001_0100/s0020_valid_parentheses/solution.go @@ -1,8 +1,8 @@ package s0020_valid_parentheses // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack -// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n) -// #2024_03_07_Time_1_ms_(74.95%)_Space_2_MB_(68.19%) +// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Top_Interview_150_Stack +// #Big_O_Time_O(n)_Space_O(n) #2025_04_27_Time_0_ms_(100.00%)_Space_4.27_MB_(35.78%) func isValid(s string) bool { stack := make([]rune, 0) @@ -27,6 +27,5 @@ func isValid(s string) bool { stack = stack[:len(stack)-1] } } - return len(stack) == 0 } diff --git a/src/main/go/g0001_0100/s0021_merge_two_sorted_lists/solution.go b/src/main/go/g0001_0100/s0021_merge_two_sorted_lists/solution.go index 7985d1f..f3b758f 100644 --- a/src/main/go/g0001_0100/s0021_merge_two_sorted_lists/solution.go +++ b/src/main/go/g0001_0100/s0021_merge_two_sorted_lists/solution.go @@ -2,8 +2,8 @@ package s0021_merge_two_sorted_lists // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion // #Data_Structure_I_Day_7_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking -// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(m+n)_Space_O(m+n) -// #2024_03_07_Time_0_ms_(100.00%)_Space_2.5_MB_(95.02%) +// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Top_Interview_150_Linked_List +// #Big_O_Time_O(m+n)_Space_O(m+n) #2025_04_27_Time_0_ms_(100.00%)_Space_4.48_MB_(40.41%) type ListNode struct { Val int @@ -19,7 +19,6 @@ type ListNode struct { */ func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { var sortedListNode, tail *ListNode = &ListNode{}, &ListNode{} - // Start sortedListNode and tail node at the same position tail = sortedListNode for list1 != nil && list2 != nil { @@ -33,14 +32,11 @@ func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { // Progress the tail further tail = tail.Next } - if list1 != nil { tail.Next = list1 } - if list2 != nil { tail.Next = list2 } - return sortedListNode.Next } diff --git a/src/main/go/g0001_0100/s0022_generate_parentheses/solution.go b/src/main/go/g0001_0100/s0022_generate_parentheses/solution.go index ff65fc0..2123c23 100644 --- a/src/main/go/g0001_0100/s0022_generate_parentheses/solution.go +++ b/src/main/go/g0001_0100/s0022_generate_parentheses/solution.go @@ -2,7 +2,8 @@ package s0022_generate_parentheses // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming // #Backtracking #Algorithm_II_Day_11_Recursion_Backtracking #Udemy_Backtracking/Recursion -// #Big_O_Time_O(2^n)_Space_O(n) #2024_03_08_Time_2_ms_(75.02%)_Space_2.8_MB_(72.29%) +// #Top_Interview_150_Backtracking #Big_O_Time_O(2^n)_Space_O(n) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.66_MB_(78.73%) func generateParenthesis(n int) []string { result := []string{} @@ -15,7 +16,6 @@ func generate(s string, start, close, n int, result *[]string) { *result = append(*result, s) return } - if start < n { generate(s+"(", start+1, close, n, result) } diff --git a/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/readme.md b/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/readme.md index db931cc..e507dae 100644 --- a/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/readme.md +++ b/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/readme.md @@ -12,25 +12,25 @@ _Merge all the linked-lists into one sorted linked-list and return it._ **Output:** [1,1,2,3,4,4,5,6] -**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6 +**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6 **Example 2:** **Input:** lists = [] -**Output:** [] +**Output:** [] **Example 3:** **Input:** lists = [[]] -**Output:** [] +**Output:** [] **Constraints:** * `k == lists.length` -* `0 <= k <= 10^4` +* 0 <= k <= 104 * `0 <= lists[i].length <= 500` -* `-10^4 <= lists[i][j] <= 10^4` +* -104 <= lists[i][j] <= 104 * `lists[i]` is sorted in **ascending order**. -* The sum of `lists[i].length` won't exceed `10^4`. \ No newline at end of file +* The sum of `lists[i].length` will not exceed 104. \ No newline at end of file diff --git a/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go b/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go index 766c91e..6205523 100644 --- a/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go +++ b/src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go @@ -1,10 +1,10 @@ package s0023_merge_k_sorted_lists -import "container/heap" - // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List -// #Divide_and_Conquer #Merge_Sort #Big_O_Time_O(k*n*log(k))_Space_O(log(k)) -// #2024_03_08_Time_3_ms_(96.74%)_Space_5.5_MB_(25.28%) +// #Divide_and_Conquer #Merge_Sort #Top_Interview_150_Divide_and_Conquer +// #Big_O_Time_O(k*n*log(k))_Space_O(log(k)) #2025_04_27_Time_0_ms_(100.00%)_Space_7.12_MB_(27.34%) + +import "container/heap" type ListNode struct { Val int diff --git a/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/readme.md b/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/readme.md index 85922fc..e065a07 100644 --- a/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/readme.md +++ b/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/readme.md @@ -6,12 +6,14 @@ Given a linked list, swap every two adjacent nodes and return its head. You must **Example 1:** -![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) - **Input:** head = [1,2,3,4] **Output:** [2,1,4,3] +**Explanation:** + +![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) + **Example 2:** **Input:** head = [] @@ -24,6 +26,12 @@ Given a linked list, swap every two adjacent nodes and return its head. You must **Output:** [1] +**Example 4:** + +**Input:** head = [1,2,3] + +**Output:** [2,1,3] + **Constraints:** * The number of nodes in the list is in the range `[0, 100]`. diff --git a/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/solution.go b/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/solution.go index 3e2743c..006a197 100644 --- a/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/solution.go +++ b/src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/solution.go @@ -2,7 +2,7 @@ package s0024_swap_nodes_in_pairs // #Medium #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_12_Linked_List // #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1) -// #2024_03_08_Time_0_ms_(100.00%)_Space_2.1_MB_(95.56%) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.08_MB_(35.81%) type ListNode struct { Val int diff --git a/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md b/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md index 6a98d1e..85c1581 100644 --- a/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md +++ b/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md @@ -2,9 +2,9 @@ Hard -Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list. +Given the `head` of a linked list, reverse the nodes of the list `k` at a time, and return _the modified list_. -_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes, in the end, should remain as it is. +`k` is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of `k` then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed. @@ -14,7 +14,7 @@ You may not alter the values in the list's nodes, only nodes themselves may be c **Input:** head = [1,2,3,4,5], k = 2 -**Output:** [2,1,4,3,5] +**Output:** [2,1,4,3,5] **Example 2:** @@ -22,25 +22,12 @@ You may not alter the values in the list's nodes, only nodes themselves may be c **Input:** head = [1,2,3,4,5], k = 3 -**Output:** [3,2,1,4,5] - -**Example 3:** - -**Input:** head = [1,2,3,4,5], k = 1 - -**Output:** [1,2,3,4,5] - -**Example 4:** - -**Input:** head = [1], k = 1 - -**Output:** [1] +**Output:** [3,2,1,4,5] **Constraints:** -* The number of nodes in the list is in the range `sz`. -* `1 <= sz <= 5000` +* The number of nodes in the list is `n`. +* `1 <= k <= n <= 5000` * `0 <= Node.val <= 1000` -* `1 <= k <= sz` -**Follow-up:** Can you solve the problem in O(1) extra memory space? \ No newline at end of file +**Follow-up:** Can you solve the problem in `O(1)` extra memory space? \ No newline at end of file diff --git a/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/solution.go b/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/solution.go index b9f5a74..2001a57 100644 --- a/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/solution.go +++ b/src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/solution.go @@ -1,8 +1,8 @@ package s0025_reverse_nodes_in_k_group // #Hard #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_13_Linked_List -// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(k) -// #2024_03_08_Time_0_ms_(100.00%)_Space_3.6_MB_(72.87%) +// #Udemy_Linked_List #Top_Interview_150_Linked_List #Big_O_Time_O(n)_Space_O(k) +// #2025_04_27_Time_0_ms_(100.00%)_Space_5.54_MB_(20.53%) type ListNode struct { Val int diff --git a/src/main/go/g0001_0100/s0031_next_permutation/readme.md b/src/main/go/g0001_0100/s0031_next_permutation/readme.md index a3c4094..d375dfd 100644 --- a/src/main/go/g0001_0100/s0031_next_permutation/readme.md +++ b/src/main/go/g0001_0100/s0031_next_permutation/readme.md @@ -20,19 +20,19 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor **Input:** nums = [1,2,3] -**Output:** [1,3,2] +**Output:** [1,3,2] **Example 2:** **Input:** nums = [3,2,1] -**Output:** [1,2,3] +**Output:** [1,2,3] **Example 3:** **Input:** nums = [1,1,5] -**Output:** [1,5,1] +**Output:** [1,5,1] **Constraints:** diff --git a/src/main/go/g0001_0100/s0031_next_permutation/solution.go b/src/main/go/g0001_0100/s0031_next_permutation/solution.go index 4d8b2a0..a1129b7 100644 --- a/src/main/go/g0001_0100/s0031_next_permutation/solution.go +++ b/src/main/go/g0001_0100/s0031_next_permutation/solution.go @@ -1,7 +1,7 @@ package s0031_next_permutation // #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1) -// #2024_03_08_Time_0_ms_(100.00%)_Space_2.4_MB_(7.18%) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.32_MB_(34.39%) func nextPermutation(nums []int) { if nums == nil || len(nums) <= 1 { diff --git a/src/main/go/g0001_0100/s0032_longest_valid_parentheses/readme.md b/src/main/go/g0001_0100/s0032_longest_valid_parentheses/readme.md index dfddb13..ef219b0 100644 --- a/src/main/go/g0001_0100/s0032_longest_valid_parentheses/readme.md +++ b/src/main/go/g0001_0100/s0032_longest_valid_parentheses/readme.md @@ -2,7 +2,7 @@ Hard -Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring. +Given a string containing just the characters `'('` and `')'`, return _the length of the longest valid (well-formed) parentheses_ **substring**. **Example 1:** @@ -10,7 +10,7 @@ Given a string containing just the characters `'('` and `')'`, find the length o **Output:** 2 -**Explanation:** The longest valid parentheses substring is "()". +**Explanation:** The longest valid parentheses substring is "()". **Example 2:** @@ -18,13 +18,13 @@ Given a string containing just the characters `'('` and `')'`, find the length o **Output:** 4 -**Explanation:** The longest valid parentheses substring is "()()". +**Explanation:** The longest valid parentheses substring is "()()". **Example 3:** **Input:** s = "" -**Output:** 0 +**Output:** 0 **Constraints:** diff --git a/src/main/go/g0001_0100/s0032_longest_valid_parentheses/solution.go b/src/main/go/g0001_0100/s0032_longest_valid_parentheses/solution.go index 07a99eb..41a7808 100644 --- a/src/main/go/g0001_0100/s0032_longest_valid_parentheses/solution.go +++ b/src/main/go/g0001_0100/s0032_longest_valid_parentheses/solution.go @@ -1,7 +1,7 @@ package s0032_longest_valid_parentheses // #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1) -// #2024_03_11_Time_0_ms_(100.00%)_Space_2.3_MB_(100.00%) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.28_MB_(96.40%) func longestValidParentheses(s string) int { max := 0 diff --git a/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md b/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md index 62aa8ba..ebfff91 100644 --- a/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md +++ b/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md @@ -14,19 +14,19 @@ You must write an algorithm with `O(log n)` runtime complexity. **Input:** nums = [4,5,6,7,0,1,2], target = 0 -**Output:** 4 +**Output:** 4 **Example 2:** **Input:** nums = [4,5,6,7,0,1,2], target = 3 -**Output:** -1 +**Output:** -1 **Example 3:** **Input:** nums = [1], target = 0 -**Output:** -1 +**Output:** -1 **Constraints:** diff --git a/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/solution.go b/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/solution.go index 1093e21..f1c06b6 100644 --- a/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/solution.go +++ b/src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/solution.go @@ -2,8 +2,8 @@ package s0033_search_in_rotated_sorted_array // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search // #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_11 #Level_2_Day_8_Binary_Search -// #Udemy_Binary_Search #Big_O_Time_O(log_n)_Space_O(1) -// #2024_03_11_Time_0_ms_(100.00%)_Space_2.6_MB_(41.41%) +// #Udemy_Binary_Search #Top_Interview_150_Binary_Search #Big_O_Time_O(log_n)_Space_O(1) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.51_MB_(4.93%) func search(nums []int, target int) int { var mid, lo, hi int diff --git a/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md b/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md index 3ccb152..7c04eb2 100644 --- a/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md +++ b/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md @@ -12,19 +12,19 @@ You must write an algorithm with `O(log n)` runtime complexity. **Input:** nums = [5,7,7,8,8,10], target = 8 -**Output:** [3,4] +**Output:** [3,4] **Example 2:** **Input:** nums = [5,7,7,8,8,10], target = 6 -**Output:** [-1,-1] +**Output:** [-1,-1] **Example 3:** **Input:** nums = [], target = 0 -**Output:** [-1,-1] +**Output:** [-1,-1] **Constraints:** diff --git a/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.go b/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.go index 208abc9..6f63200 100644 --- a/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.go +++ b/src/main/go/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.go @@ -1,8 +1,8 @@ package s0034_find_first_and_last_position_of_element_in_sorted_array // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search -// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_5 #Big_O_Time_O(log_n)_Space_O(1) -// #2024_03_11_Time_3_ms_(93.06%)_Space_4.5_MB_(100.00%) +// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_5 #Top_Interview_150_Binary_Search +// #Big_O_Time_O(log_n)_Space_O(1) #2025_04_27_Time_0_ms_(100.00%)_Space_6.47_MB_(30.77%) func searchRange(nums []int, target int) []int { ans := make([]int, 2) diff --git a/src/main/go/g0001_0100/s0035_search_insert_position/readme.md b/src/main/go/g0001_0100/s0035_search_insert_position/readme.md index c273f32..79f9ab2 100644 --- a/src/main/go/g0001_0100/s0035_search_insert_position/readme.md +++ b/src/main/go/g0001_0100/s0035_search_insert_position/readme.md @@ -10,31 +10,19 @@ You must write an algorithm with `O(log n)` runtime complexity. **Input:** nums = [1,3,5,6], target = 5 -**Output:** 2 +**Output:** 2 **Example 2:** **Input:** nums = [1,3,5,6], target = 2 -**Output:** 1 +**Output:** 1 **Example 3:** **Input:** nums = [1,3,5,6], target = 7 -**Output:** 4 - -**Example 4:** - -**Input:** nums = [1,3,5,6], target = 0 - -**Output:** 0 - -**Example 5:** - -**Input:** nums = [1], target = 0 - -**Output:** 0 +**Output:** 4 **Constraints:** diff --git a/src/main/go/g0001_0100/s0035_search_insert_position/solution.go b/src/main/go/g0001_0100/s0035_search_insert_position/solution.go index 571e2c1..a95fa82 100644 --- a/src/main/go/g0001_0100/s0035_search_insert_position/solution.go +++ b/src/main/go/g0001_0100/s0035_search_insert_position/solution.go @@ -1,8 +1,8 @@ package s0035_search_insert_position // #Easy #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_I_Day_1_Binary_Search -// #Binary_Search_I_Day_2 #Big_O_Time_O(log_n)_Space_O(1) -// #2024_03_11_Time_0_ms_(100.00%)_Space_2.9_MB_(91.84%) +// #Binary_Search_I_Day_2 #Top_Interview_150_Binary_Search #Big_O_Time_O(log_n)_Space_O(1) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.96_MB_(3.02%) func searchInsert(nums []int, target int) int { lo, hi := 0, len(nums)-1 diff --git a/src/main/go/g0001_0100/s0039_combination_sum/readme.md b/src/main/go/g0001_0100/s0039_combination_sum/readme.md index 3c1934f..c4c8696 100644 --- a/src/main/go/g0001_0100/s0039_combination_sum/readme.md +++ b/src/main/go/g0001_0100/s0039_combination_sum/readme.md @@ -6,7 +6,7 @@ Given an array of **distinct** integers `candidates` and a target integer `targe The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different. -It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input. +The test cases are generated such that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input. **Example 1:** @@ -14,28 +14,23 @@ It is **guaranteed** that the number of unique combinations that sum up to `targ **Output:** [[2,2,3],[7]] -**Explanation:** -2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. - -7 is a candidate, and 7 = 7. - -These are the only two combinations. +**Explanation:** 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations. **Example 2:** **Input:** candidates = [2,3,5], target = 8 -**Output:** [[2,2,2,2],[2,3,3],[3,5]] +**Output:** [[2,2,2,2],[2,3,3],[3,5]] **Example 3:** **Input:** candidates = [2], target = 1 -**Output:** [] +**Output:** [] **Constraints:** * `1 <= candidates.length <= 30` -* `1 <= candidates[i] <= 200` +* `2 <= candidates[i] <= 40` * All elements of `candidates` are **distinct**. -* `1 <= target <= 500` \ No newline at end of file +* `1 <= target <= 40` \ No newline at end of file diff --git a/src/main/go/g0001_0100/s0039_combination_sum/solution.go b/src/main/go/g0001_0100/s0039_combination_sum/solution.go index 36ecd7a..f6cbbf2 100644 --- a/src/main/go/g0001_0100/s0039_combination_sum/solution.go +++ b/src/main/go/g0001_0100/s0039_combination_sum/solution.go @@ -2,7 +2,8 @@ package s0039_combination_sum // #Medium #Top_100_Liked_Questions #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking // #Level_2_Day_20_Brute_Force/Backtracking #Udemy_Backtracking/Recursion -// #Big_O_Time_O(2^n)_Space_O(n+2^n) #2024_03_11_Time_0_ms_(100.00%)_Space_3_MB_(71.24%) +// #Top_Interview_150_Backtracking #Big_O_Time_O(2^n)_Space_O(n+2^n) +// #2025_04_27_Time_0_ms_(100.00%)_Space_4.97_MB_(71.50%) func combinationSum(coins []int, amount int) [][]int { var ans [][]int diff --git a/src/main/go/g0001_0100/s0041_first_missing_positive/readme.md b/src/main/go/g0001_0100/s0041_first_missing_positive/readme.md index 040b8c5..c786c05 100644 --- a/src/main/go/g0001_0100/s0041_first_missing_positive/readme.md +++ b/src/main/go/g0001_0100/s0041_first_missing_positive/readme.md @@ -2,9 +2,9 @@ Hard -Given an unsorted integer array `nums`, return the smallest missing positive integer. +Given an unsorted integer array `nums`. Return the _smallest positive integer_ that is _not present_ in `nums`. -You must implement an algorithm that runs in `O(n)` time and uses constant extra space. +You must implement an algorithm that runs in `O(n)` time and uses `O(1)` auxiliary space. **Example 1:** @@ -12,7 +12,7 @@ You must implement an algorithm that runs in `O(n)` time and uses constant extra **Output:** 3 -**Explanation:** The numbers in the range [1,2] are all in the array. +**Explanation:** The numbers in the range [1,2] are all in the array. **Example 2:** @@ -20,7 +20,7 @@ You must implement an algorithm that runs in `O(n)` time and uses constant extra **Output:** 2 -**Explanation:** 1 is in the array but 2 is missing. +**Explanation:** 1 is in the array but 2 is missing. **Example 3:** @@ -28,7 +28,7 @@ You must implement an algorithm that runs in `O(n)` time and uses constant extra **Output:** 1 -**Explanation:** The smallest positive integer 1 is missing. +**Explanation:** The smallest positive integer 1 is missing. **Constraints:** diff --git a/src/main/go/g0001_0100/s0041_first_missing_positive/solution.go b/src/main/go/g0001_0100/s0041_first_missing_positive/solution.go index 7a80a0c..6bb3e48 100644 --- a/src/main/go/g0001_0100/s0041_first_missing_positive/solution.go +++ b/src/main/go/g0001_0100/s0041_first_missing_positive/solution.go @@ -1,7 +1,7 @@ package s0041_first_missing_positive // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Udemy_Arrays -// #Big_O_Time_O(n)_Space_O(n) #2024_03_12_Time_39_ms_(92.34%)_Space_7.8_MB_(79.50%) +// #Big_O_Time_O(n)_Space_O(n) #2025_05_03_Time_0_ms_(100.00%)_Space_9.49_MB_(95.52%) func firstMissingPositive(nums []int) int { for i := 0; i < len(nums); i++ { @@ -9,7 +9,6 @@ func firstMissingPositive(nums []int) int { nums[i] = 0 } } - for i := 0; i < len(nums); i++ { val := abs(nums[i]) if val > 0 && val <= len(nums) { @@ -17,19 +16,16 @@ func firstMissingPositive(nums []int) int { nums[val-1] = nums[val-1] * -1 continue } - if nums[val-1] == 0 { nums[val-1] = -1 * (len(nums) + 1) } } } - for i := 1; i <= len(nums); i++ { if nums[i-1] >= 0 { return i } } - return len(nums) + 1 } @@ -37,6 +33,5 @@ func abs(n int) int { if n >= 0 { return n } - return n * -1 } diff --git a/src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go b/src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go index b9dd716..393dc09 100644 --- a/src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go +++ b/src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go @@ -2,24 +2,25 @@ package s0042_trapping_rain_water // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Two_Pointers // #Stack #Monotonic_Stack #Dynamic_Programming_I_Day_9 #Udemy_Two_Pointers -// #Big_O_Time_O(n)_Space_O(1) #2024_03_12_Time_3_ms_(99.42%)_Space_5.4_MB_(97.01%) +// #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(1) +// #2025_05_03_Time_0_ms_(100.00%)_Space_7.85_MB_(82.53%) func trap(height []int) int { if height == nil { return 0 } l, r := 0, len(height)-1 - max_left, max_right := height[l], height[r] + maxLeft, maxRight := height[l], height[r] res := 0 for l < r { - if max_left < max_right { + if maxLeft < maxRight { l++ - max_left = max(max_left, height[l]) - res += max_left - height[l] + maxLeft = max(maxLeft, height[l]) + res += maxLeft - height[l] } else { r-- - max_right = max(max_right, height[r]) - res += max_right - height[r] + maxRight = max(maxRight, height[r]) + res += maxRight - height[r] } } return res diff --git a/src/main/go/g0001_0100/s0045_jump_game_ii/readme.md b/src/main/go/g0001_0100/s0045_jump_game_ii/readme.md index e5a76a7..7c5c906 100644 --- a/src/main/go/g0001_0100/s0045_jump_game_ii/readme.md +++ b/src/main/go/g0001_0100/s0045_jump_game_ii/readme.md @@ -2,13 +2,14 @@ Medium -Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array. +You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`. -Each element in the array represents your maximum jump length at that position. +Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where: -Your goal is to reach the last index in the minimum number of jumps. +* `0 <= j <= nums[i]` and +* `i + j < n` -You can assume that you can always reach the last index. +Return _the minimum number of jumps to reach_ `nums[n - 1]`. The test cases are generated such that you can reach `nums[n - 1]`. **Example 1:** @@ -16,15 +17,16 @@ You can assume that you can always reach the last index. **Output:** 2 -**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. +**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. **Example 2:** **Input:** nums = [2,3,0,1,4] -**Output:** 2 +**Output:** 2 **Constraints:** * 1 <= nums.length <= 104 -* `0 <= nums[i] <= 1000` \ No newline at end of file +* `0 <= nums[i] <= 1000` +* It's guaranteed that you can reach `nums[n - 1]`. \ No newline at end of file diff --git a/src/main/go/g0001_0100/s0045_jump_game_ii/solution.go b/src/main/go/g0001_0100/s0045_jump_game_ii/solution.go index d9aa79c..da25186 100644 --- a/src/main/go/g0001_0100/s0045_jump_game_ii/solution.go +++ b/src/main/go/g0001_0100/s0045_jump_game_ii/solution.go @@ -2,7 +2,8 @@ package s0045_jump_game_ii // #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Greedy // #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_4 -// #Big_O_Time_O(n)_Space_O(1) #2024_03_12_Time_6_ms_(96.98%)_Space_6.3_MB_(34.53%) +// #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(1) +// #2025_05_03_Time_0_ms_(100.00%)_Space_7.78_MB_(85.74%) func jump(nums []int) int { length := 0 diff --git a/src/main/go/g0001_0100/s0046_permutations/readme.md b/src/main/go/g0001_0100/s0046_permutations/readme.md index fdeabb6..f66dc4f 100644 --- a/src/main/go/g0001_0100/s0046_permutations/readme.md +++ b/src/main/go/g0001_0100/s0046_permutations/readme.md @@ -2,25 +2,25 @@ Medium -Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**. +Given an array `nums` of distinct integers, return all the possible permutations. You can return the answer in **any order**. **Example 1:** **Input:** nums = [1,2,3] -**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] +**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] **Example 2:** **Input:** nums = [0,1] -**Output:** [[0,1],[1,0]] +**Output:** [[0,1],[1,0]] **Example 3:** **Input:** nums = [1] -**Output:** [[1]] +**Output:** [[1]] **Constraints:** diff --git a/src/main/go/g0001_0100/s0046_permutations/solution.go b/src/main/go/g0001_0100/s0046_permutations/solution.go index 1ff5fb3..b8f957b 100644 --- a/src/main/go/g0001_0100/s0046_permutations/solution.go +++ b/src/main/go/g0001_0100/s0046_permutations/solution.go @@ -2,8 +2,8 @@ package s0046_permutations // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Backtracking // #Algorithm_I_Day_11_Recursion_Backtracking #Level_2_Day_20_Brute_Force/Backtracking -// #Udemy_Backtracking/Recursion #Big_O_Time_O(n*n!)_Space_O(n+n!) -// #2024_03_12_Time_0_ms_(100.00%)_Space_2.7_MB_(51.79%) +// #Udemy_Backtracking/Recursion #Top_Interview_150_Backtracking #Big_O_Time_O(n*n!)_Space_O(n+n!) +// #2025_05_03_Time_0_ms_(100.00%)_Space_4.59_MB_(85.96%) func permute(nums []int) [][]int { if len(nums) == 0 { diff --git a/src/main/go/g0001_0100/s0048_rotate_image/solution.go b/src/main/go/g0001_0100/s0048_rotate_image/solution.go index 79dc2f1..1aab5ca 100644 --- a/src/main/go/g0001_0100/s0048_rotate_image/solution.go +++ b/src/main/go/g0001_0100/s0048_rotate_image/solution.go @@ -2,7 +2,8 @@ package s0048_rotate_image // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Matrix // #Data_Structure_II_Day_3_Array #Programming_Skills_II_Day_7 #Udemy_2D_Arrays/Matrix -// #Big_O_Time_O(n^2)_Space_O(1) #2024_03_12_Time_0_ms_(100.00%)_Space_2.3_MB_(75.46%) +// #Top_Interview_150_Matrix #Big_O_Time_O(n^2)_Space_O(1) +// #2025_05_03_Time_0_ms_(100.00%)_Space_4.06_MB_(97.99%) func rotate(matrix [][]int) { n := len(matrix) diff --git a/src/main/go/g0001_0100/s0049_group_anagrams/solution.go b/src/main/go/g0001_0100/s0049_group_anagrams/solution.go index 48e1145..0186bbe 100644 --- a/src/main/go/g0001_0100/s0049_group_anagrams/solution.go +++ b/src/main/go/g0001_0100/s0049_group_anagrams/solution.go @@ -2,7 +2,8 @@ package s0049_group_anagrams // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting // #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings -// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2024_03_13_Time_16_ms_(85.07%)_Space_7.2_MB_(85.47%) +// #Top_Interview_150_Hashmap #Big_O_Time_O(n*k_log_k)_Space_O(n) +// #2025_05_05_Time_4_ms_(96.55%)_Space_10.20_MB_(30.85%) func groupAnagrams(strings []string) [][]string { stringsByPattern := make(map[[26]byte][]string) diff --git a/src/main/go/g0001_0100/s0051_n_queens/solution.go b/src/main/go/g0001_0100/s0051_n_queens/solution.go index 4bfbc1f..1ab99f1 100644 --- a/src/main/go/g0001_0100/s0051_n_queens/solution.go +++ b/src/main/go/g0001_0100/s0051_n_queens/solution.go @@ -1,7 +1,7 @@ package s0051_n_queens // #Hard #Top_100_Liked_Questions #Array #Backtracking #Big_O_Time_O(N!)_Space_O(N) -// #2024_03_13_Time_0_ms_(100.00%)_Space_3.3_MB_(61.83%) +// #2025_05_05_Time_0_ms_(100.00%)_Space_5.23_MB_(62.71%) func solveNQueens(n int) [][]string { pos := make([]bool, n+2*n-1+2*n-1) diff --git a/src/main/go/g0001_0100/s0053_maximum_subarray/solution.go b/src/main/go/g0001_0100/s0053_maximum_subarray/solution.go index 4ebe76e..3efedbe 100644 --- a/src/main/go/g0001_0100/s0053_maximum_subarray/solution.go +++ b/src/main/go/g0001_0100/s0053_maximum_subarray/solution.go @@ -1,9 +1,9 @@ package s0053_maximum_subarray -// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming +// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming // #Divide_and_Conquer #Data_Structure_I_Day_1_Array #Dynamic_Programming_I_Day_5 -// #Udemy_Famous_Algorithm #Big_O_Time_O(n)_Space_O(1) -// #2024_03_13_Time_79_ms_(87.63%)_Space_7.8_MB_(92.56%) +// #Udemy_Famous_Algorithm #Top_Interview_150_Kadane's_Algorithm #Big_O_Time_O(n)_Space_O(1) +// #2025_05_05_Time_0_ms_(100.00%)_Space_10.37_MB_(42.73%) func maxSubArray(nums []int) int { sum, max := nums[0], nums[0] diff --git a/src/main/go/g0001_0100/s0055_jump_game/solution.go b/src/main/go/g0001_0100/s0055_jump_game/solution.go index 1ef3821..fcc20b2 100644 --- a/src/main/go/g0001_0100/s0055_jump_game/solution.go +++ b/src/main/go/g0001_0100/s0055_jump_game/solution.go @@ -2,7 +2,8 @@ package s0055_jump_game // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Greedy // #Algorithm_II_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_4 #Udemy_Arrays -// #Big_O_Time_O(n)_Space_O(1) #2024_03_13_Time_48_ms_(71.95%)_Space_6.7_MB_(99.94%) +// #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(1) +// #2025_05_05_Time_0_ms_(100.00%)_Space_8.96_MB_(66.09%) func canJump(nums []int) bool { if len(nums) == 1 { diff --git a/src/main/go/g0001_0100/s0056_merge_intervals/solution.go b/src/main/go/g0001_0100/s0056_merge_intervals/solution.go index 62f9428..20ed944 100644 --- a/src/main/go/g0001_0100/s0056_merge_intervals/solution.go +++ b/src/main/go/g0001_0100/s0056_merge_intervals/solution.go @@ -2,7 +2,8 @@ package s0056_merge_intervals // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting // #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix -// #Big_O_Time_O(n_log_n)_Space_O(n) #2024_03_13_Time_12_ms_(90.65%)_Space_6.3_MB_(74.98%) +// #Top_Interview_150_Intervals #Big_O_Time_O(n_log_n)_Space_O(n) +// #2025_05_05_Time_0_ms_(100.00%)_Space_8.61_MB_(25.59%) import "sort" diff --git a/src/main/go/g0001_0100/s0062_unique_paths/solution.go b/src/main/go/g0001_0100/s0062_unique_paths/solution.go index 827ceea..a45aaed 100644 --- a/src/main/go/g0001_0100/s0062_unique_paths/solution.go +++ b/src/main/go/g0001_0100/s0062_unique_paths/solution.go @@ -1,9 +1,9 @@ package s0062_unique_paths // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math -// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15 -// #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n) -// #2024_03_14_Time_0_ms_(100.00%)_Space_2.2_MB_(26.53%) +// #Combinatorics #LeetCode_75_DP/Multidimensional #Algorithm_II_Day_13_Dynamic_Programming +// #Dynamic_Programming_I_Day_15 #Level_1_Day_11_Dynamic_Programming +// #Big_O_Time_O(m*n)_Space_O(m*n) #2025_05_05_Time_0_ms_(100.00%)_Space_4.06_MB_(55.42%) func uniquePaths(m int, n int) int { dp := make([][]int, m) diff --git a/src/main/go/g0001_0100/s0064_minimum_path_sum/solution.go b/src/main/go/g0001_0100/s0064_minimum_path_sum/solution.go index 939c09f..3a22c67 100644 --- a/src/main/go/g0001_0100/s0064_minimum_path_sum/solution.go +++ b/src/main/go/g0001_0100/s0064_minimum_path_sum/solution.go @@ -1,8 +1,8 @@ package s0064_minimum_path_sum // #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix -// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n) -// #2024_03_14_Time_0_ms_(100.00%)_Space_4.1_MB_(24.44%) +// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP +// #Big_O_Time_O(m*n)_Space_O(m*n) #2025_05_05_Time_0_ms_(100.00%)_Space_7.68_MB_(48.53%) func minPathSum(grid [][]int) int { if len(grid) == 1 && len(grid[0]) == 1 { diff --git a/src/main/go/g0001_0100/s0070_climbing_stairs/solution.go b/src/main/go/g0001_0100/s0070_climbing_stairs/solution.go index a9ac842..5a43da1 100644 --- a/src/main/go/g0001_0100/s0070_climbing_stairs/solution.go +++ b/src/main/go/g0001_0100/s0070_climbing_stairs/solution.go @@ -2,8 +2,8 @@ package s0070_climbing_stairs // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization // #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2 -// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n) -// #2024_03_14_Time_0_ms_(100.00%)_Space_2_MB_(50.58%) +// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP +// #Big_O_Time_O(n)_Space_O(n) #2025_05_05_Time_0_ms_(100.00%)_Space_3.88_MB_(81.88%) func climbStairs(n int) int { f := make([]int, n+1) diff --git a/src/main/go/g0001_0100/s0072_edit_distance/solution.go b/src/main/go/g0001_0100/s0072_edit_distance/solution.go index 43cc915..2ec07af 100644 --- a/src/main/go/g0001_0100/s0072_edit_distance/solution.go +++ b/src/main/go/g0001_0100/s0072_edit_distance/solution.go @@ -1,9 +1,9 @@ package s0072_edit_distance -// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming +// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming #LeetCode_75_DP/Multidimensional // #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19 -// #Udemy_Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n2) -// #2024_03_14_Time_0_ms_(100.00%)_Space_2.6_MB_(97.61%) +// #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP #Big_O_Time_O(n^2)_Space_O(n2) +// #2025_05_05_Time_0_ms_(100.00%)_Space_4.46_MB_(99.07%) func minDistance(word1 string, word2 string) int { n1 := len(word1) diff --git a/src/main/go/g0001_0100/s0073_set_matrix_zeroes/solution.go b/src/main/go/g0001_0100/s0073_set_matrix_zeroes/solution.go index 42c9278..e090bd3 100644 --- a/src/main/go/g0001_0100/s0073_set_matrix_zeroes/solution.go +++ b/src/main/go/g0001_0100/s0073_set_matrix_zeroes/solution.go @@ -1,8 +1,8 @@ package s0073_set_matrix_zeroes // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix -// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(m*n)_Space_O(1) -// #2024_03_14_Time_8_ms_(83.64%)_Space_5.9_MB_(75.58%) +// #Udemy_2D_Arrays/Matrix #Top_Interview_150_Matrix #Big_O_Time_O(m*n)_Space_O(1) +// #2025_05_05_Time_0_ms_(100.00%)_Space_7.88_MB_(57.66%) func setZeroes(matrix [][]int) { m := len(matrix) diff --git a/src/main/go/g0001_0100/s0074_search_a_2d_matrix/solution.go b/src/main/go/g0001_0100/s0074_search_a_2d_matrix/solution.go index 07ffe77..d77ef88 100644 --- a/src/main/go/g0001_0100/s0074_search_a_2d_matrix/solution.go +++ b/src/main/go/g0001_0100/s0074_search_a_2d_matrix/solution.go @@ -2,8 +2,8 @@ package s0074_search_a_2d_matrix // #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array // #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search -// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1) -// #2024_03_15_Time_0_ms_(100.00%)_Space_2.6_MB_(75.18%) +// #Udemy_2D_Arrays/Matrix #Top_Interview_150_Binary_Search #Big_O_Time_O(endRow+endCol)_Space_O(1) +// #2025_05_05_Time_0_ms_(100.00%)_Space_4.45_MB_(46.12%) func searchMatrix(matrix [][]int, target int) bool { endRow := len(matrix) diff --git a/src/main/go/g0001_0100/s0075_sort_colors/solution.go b/src/main/go/g0001_0100/s0075_sort_colors/solution.go index 94941f8..2182a0f 100644 --- a/src/main/go/g0001_0100/s0075_sort_colors/solution.go +++ b/src/main/go/g0001_0100/s0075_sort_colors/solution.go @@ -2,7 +2,7 @@ package s0075_sort_colors // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers // #Data_Structure_II_Day_2_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1) -// #2024_03_15_Time_1_ms_(76.26%)_Space_2.1_MB_(8.79%) +// #2025_05_06_Time_0_ms_(100.00%)_Space_4.17_MB_(11.59%) func sortColors(nums []int) { zeroes := 0 diff --git a/src/main/go/g0001_0100/s0076_minimum_window_substring/solution.go b/src/main/go/g0001_0100/s0076_minimum_window_substring/solution.go index 3cc9be0..4e0c99d 100644 --- a/src/main/go/g0001_0100/s0076_minimum_window_substring/solution.go +++ b/src/main/go/g0001_0100/s0076_minimum_window_substring/solution.go @@ -1,8 +1,8 @@ package s0076_minimum_window_substring // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window -// #Level_2_Day_14_Sliding_Window/Two_Pointer #Big_O_Time_O(s.length())_Space_O(1) -// #2024_03_15_Time_0_ms_(100.00%)_Space_3_MB_(66.67%) +// #Level_2_Day_14_Sliding_Window/Two_Pointer #Top_Interview_150_Sliding_Window +// #Big_O_Time_O(s.length())_Space_O(1) #2025_05_06_Time_0_ms_(100.00%)_Space_5.22_MB_(52.57%) import "math" diff --git a/src/main/go/g0001_0100/s0078_subsets/solution.go b/src/main/go/g0001_0100/s0078_subsets/solution.go index 70de62d..52331c7 100644 --- a/src/main/go/g0001_0100/s0078_subsets/solution.go +++ b/src/main/go/g0001_0100/s0078_subsets/solution.go @@ -2,7 +2,7 @@ package s0078_subsets // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation #Backtracking // #Algorithm_II_Day_9_Recursion_Backtracking #Udemy_Backtracking/Recursion -// #Big_O_Time_O(2^n)_Space_O(n*2^n) #2024_03_15_Time_1_ms_(80.53%)_Space_2.4_MB_(60.25%) +// #Big_O_Time_O(2^n)_Space_O(n*2^n) #2025_05_06_Time_0_ms_(100.00%)_Space_4.31_MB_(21.64%) func subsets(nums []int) [][]int { var res [][]int diff --git a/src/main/go/g0001_0100/s0079_word_search/solution.go b/src/main/go/g0001_0100/s0079_word_search/solution.go index fa050c0..4678e35 100644 --- a/src/main/go/g0001_0100/s0079_word_search/solution.go +++ b/src/main/go/g0001_0100/s0079_word_search/solution.go @@ -1,8 +1,8 @@ package s0079_word_search // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking -// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n) -// #2024_03_15_Time_103_ms_(79.70%)_Space_2_MB_(58.16%) +// #Algorithm_II_Day_11_Recursion_Backtracking #Top_Interview_150_Backtracking +// #Big_O_Time_O(4^(m*n))_Space_O(m*n) #2025_05_06_Time_99_ms_(79.91%)_Space_4.02_MB_(88.63%) func exist(board [][]byte, word string) bool { var run func(i, j, k int) bool diff --git a/src/main/go/g0001_0100/s0084_largest_rectangle_in_histogram/solution.go b/src/main/go/g0001_0100/s0084_largest_rectangle_in_histogram/solution.go index e93881a..9f405f9 100644 --- a/src/main/go/g0001_0100/s0084_largest_rectangle_in_histogram/solution.go +++ b/src/main/go/g0001_0100/s0084_largest_rectangle_in_histogram/solution.go @@ -1,9 +1,9 @@ package s0084_largest_rectangle_in_histogram -import "math" - // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Stack #Monotonic_Stack -// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2024_03_15_Time_95_ms_(77.81%)_Space_7.8_MB_(91.91%) +// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2025_05_06_Time_2_ms_(94.16%)_Space_10.14_MB_(81.09%) + +import "math" func largestRectangleArea(heights []int) int { return largestArea(heights, 0, len(heights)) diff --git a/src/main/go/g0001_0100/s0094_binary_tree_inorder_traversal/solution.go b/src/main/go/g0001_0100/s0094_binary_tree_inorder_traversal/solution.go index 1381051..0c708c6 100644 --- a/src/main/go/g0001_0100/s0094_binary_tree_inorder_traversal/solution.go +++ b/src/main/go/g0001_0100/s0094_binary_tree_inorder_traversal/solution.go @@ -2,7 +2,7 @@ package s0094_binary_tree_inorder_traversal // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree // #Stack #Data_Structure_I_Day_10_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n) -// #2024_03_15_Time_0_ms_(100.00%)_Space_2.3_MB_(82.78%) +// #2025_05_06_Time_0_ms_(100.00%)_Space_4.12_MB_(29.86%) type TreeNode struct { Val int diff --git a/src/main/go/g0001_0100/s0096_unique_binary_search_trees/solution.go b/src/main/go/g0001_0100/s0096_unique_binary_search_trees/solution.go index 2db0bc4..0a35bbd 100644 --- a/src/main/go/g0001_0100/s0096_unique_binary_search_trees/solution.go +++ b/src/main/go/g0001_0100/s0096_unique_binary_search_trees/solution.go @@ -1,8 +1,8 @@ package s0096_unique_binary_search_trees -// #Medium #Top_100_Liked_Questions #Dynamic_Programming #Math #Tree #Binary_Tree -// #Binary_Search_Tree #Dynamic_Programming_I_Day_11 #Big_O_Time_O(n)_Space_O(1) -// #2024_03_15_Time_0_ms_(100.00%)_Space_2.1_MB_(46.95%) +// #Medium #Dynamic_Programming #Math #Tree #Binary_Tree #Binary_Search_Tree +// #Dynamic_Programming_I_Day_11 #Big_O_Time_O(n)_Space_O(1) +// #2025_05_06_Time_0_ms_(100.00%)_Space_3.85_MB_(79.74%) func numTrees(n int) int { result := 1 diff --git a/src/main/go/g0001_0100/s0098_validate_binary_search_tree/solution.go b/src/main/go/g0001_0100/s0098_validate_binary_search_tree/solution.go index daa419a..02171fd 100644 --- a/src/main/go/g0001_0100/s0098_validate_binary_search_tree/solution.go +++ b/src/main/go/g0001_0100/s0098_validate_binary_search_tree/solution.go @@ -2,8 +2,8 @@ package s0098_validate_binary_search_tree // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree // #Binary_Search_Tree #Data_Structure_I_Day_14_Tree #Level_1_Day_8_Binary_Search_Tree -// #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(log(N)) -// #2024_03_15_Time_6_ms_(55.36%)_Space_5.4_MB_(59.64%) +// #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Search_Tree #Big_O_Time_O(N)_Space_O(log(N)) +// #2025_05_06_Time_0_ms_(100.00%)_Space_7.21_MB_(46.50%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0101_symmetric_tree/solution.go b/src/main/go/g0101_0200/s0101_symmetric_tree/solution.go index c072519..79ee164 100644 --- a/src/main/go/g0101_0200/s0101_symmetric_tree/solution.go +++ b/src/main/go/g0101_0200/s0101_symmetric_tree/solution.go @@ -2,7 +2,8 @@ package s0101_symmetric_tree // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search // #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_2_Day_15_Tree -// #Big_O_Time_O(N)_Space_O(log(N)) #2024_03_15_Time_0_ms_(100.00%)_Space_3_MB_(8.89%) +// #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(log(N)) +// #2025_05_06_Time_0_ms_(100.00%)_Space_4.79_MB_(79.66%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0102_binary_tree_level_order_traversal/solution.go b/src/main/go/g0101_0200/s0102_binary_tree_level_order_traversal/solution.go index 65d9a7f..93095bc 100644 --- a/src/main/go/g0101_0200/s0102_binary_tree_level_order_traversal/solution.go +++ b/src/main/go/g0101_0200/s0102_binary_tree_level_order_traversal/solution.go @@ -2,7 +2,8 @@ package s0102_binary_tree_level_order_traversal // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Breadth_First_Search #Tree // #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_1_Day_6_Tree #Udemy_Tree_Stack_Queue -// #Big_O_Time_O(N)_Space_O(N) #2024_03_19_Time_3_ms_(70.81%)_Space_3.8_MB_(31.96%) +// #Top_Interview_150_Binary_Tree_BFS #Big_O_Time_O(N)_Space_O(N) +// #2025_05_06_Time_0_ms_(100.00%)_Space_5.45_MB_(96.04%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.go b/src/main/go/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.go index 1c25f5f..883051d 100644 --- a/src/main/go/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.go +++ b/src/main/go/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.go @@ -1,9 +1,10 @@ package s0104_maximum_depth_of_binary_tree // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search -// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree +// #Tree #Binary_Tree #LeetCode_75_Binary_Tree/DFS #Data_Structure_I_Day_11_Tree // #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue -// #Big_O_Time_O(N)_Space_O(H) #2024_03_19_Time_0_ms_(100.00%)_Space_4.6_MB_(19.81%) +// #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(H) +// #2025_05_06_Time_0_ms_(100.00%)_Space_6.36_MB_(51.89%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.go b/src/main/go/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.go index 25b4e6f..b8b62f8 100644 --- a/src/main/go/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.go +++ b/src/main/go/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.go @@ -1,8 +1,8 @@ package s0105_construct_binary_tree_from_preorder_and_inorder_traversal // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Tree #Binary_Tree -// #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Big_O_Time_O(N)_Space_O(N) -// #2024_03_19_Time_0_ms_(100.00%)_Space_4.4_MB_(16.83%) +// #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Top_Interview_150_Binary_Tree_General +// #Big_O_Time_O(N)_Space_O(N) #2025_05_06_Time_0_ms_(100.00%)_Space_6.11_MB_(20.43%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.go b/src/main/go/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.go index 4f3b6fe..eff05d3 100644 --- a/src/main/go/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.go +++ b/src/main/go/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.go @@ -1,8 +1,8 @@ package s0114_flatten_binary_tree_to_linked_list // #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Stack #Linked_List -// #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N) -// #2024_03_19_Time_0_ms_(100.00%)_Space_3.1_MB_(5.75%) +// #Udemy_Linked_List #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(N) +// #2025_05_07_Time_0_ms_(100.00%)_Space_4.88_MB_(66.49%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.go b/src/main/go/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.go index 3e2553b..5194aff 100644 --- a/src/main/go/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.go +++ b/src/main/go/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.go @@ -2,7 +2,8 @@ package s0121_best_time_to_buy_and_sell_stock // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming // #Data_Structure_I_Day_3_Array #Dynamic_Programming_I_Day_7 #Level_1_Day_5_Greedy #Udemy_Arrays -// #Big_O_Time_O(N)_Space_O(1) #2024_03_19_Time_76_ms_(99.34%)_Space_7.4_MB_(98.91%) +// #Top_Interview_150_Array/String #Big_O_Time_O(N)_Space_O(1) +// #2025_05_07_Time_0_ms_(100.00%)_Space_9.74_MB_(72.19%) func maxProfit(prices []int) int { maxProfit := 0 diff --git a/src/main/go/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.go b/src/main/go/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.go index c33a4bf..b1a99ef 100644 --- a/src/main/go/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.go +++ b/src/main/go/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.go @@ -1,8 +1,8 @@ package s0124_binary_tree_maximum_path_sum // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Depth_First_Search -// #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(N) -// #2024_03_19_Time_10_ms_(89.68%)_Space_7.8_MB_(94.84%) +// #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_General +// #Big_O_Time_O(N)_Space_O(N) #2025_05_07_Time_0_ms_(100.00%)_Space_10.08_MB_(59.76%) type TreeNode struct { Val int diff --git a/src/main/go/g0101_0200/s0128_longest_consecutive_sequence/solution.go b/src/main/go/g0101_0200/s0128_longest_consecutive_sequence/solution.go index c7bbdc6..cba1d4a 100644 --- a/src/main/go/g0101_0200/s0128_longest_consecutive_sequence/solution.go +++ b/src/main/go/g0101_0200/s0128_longest_consecutive_sequence/solution.go @@ -1,9 +1,10 @@ package s0128_longest_consecutive_sequence -import "sort" - // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Union_Find -// #Big_O_Time_O(N_log_N)_Space_O(1) #2024_03_19_Time_54_ms_(99.84%)_Space_8.9_MB_(93.24%) +// #Top_Interview_150_Hashmap #Big_O_Time_O(N_log_N)_Space_O(1) +// #2025_05_07_Time_4_ms_(99.84%)_Space_10.05_MB_(99.24%) + +import "sort" func longestConsecutive(nums []int) int { if len(nums) == 0 { diff --git a/src/main/go/g0101_0200/s0131_palindrome_partitioning/solution.go b/src/main/go/g0101_0200/s0131_palindrome_partitioning/solution.go index 397650b..c645471 100644 --- a/src/main/go/g0101_0200/s0131_palindrome_partitioning/solution.go +++ b/src/main/go/g0101_0200/s0131_palindrome_partitioning/solution.go @@ -2,7 +2,7 @@ package s0131_palindrome_partitioning // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming // #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N) -// #2024_03_19_Time_221_ms_(97.16%)_Space_19.1_MB_(89.91%) +// #2025_05_07_Time_18_ms_(92.42%)_Space_27.26_MB_(16.97%) func partition(s string) [][]string { var res [][]string diff --git a/src/main/go/g0101_0200/s0136_single_number/solution.go b/src/main/go/g0101_0200/s0136_single_number/solution.go index 0859753..1ce86ae 100644 --- a/src/main/go/g0101_0200/s0136_single_number/solution.go +++ b/src/main/go/g0101_0200/s0136_single_number/solution.go @@ -1,8 +1,9 @@ package s0136_single_number // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation -// #Data_Structure_II_Day_1_Array #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Integers -// #Big_O_Time_O(N)_Space_O(1) #2024_03_19_Time_8_ms_(95.49%)_Space_6.1_MB_(77.03%) +// #LeetCode_75_Bit_Manipulation #Data_Structure_II_Day_1_Array +// #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Integers #Top_Interview_150_Bit_Manipulation +// #Big_O_Time_O(N)_Space_O(1) #2025_05_07_Time_0_ms_(100.00%)_Space_7.82_MB_(83.54%) func singleNumber(nums []int) int { res := nums[0] diff --git a/src/main/go/g0101_0200/s0138_copy_list_with_random_pointer/solution.go b/src/main/go/g0101_0200/s0138_copy_list_with_random_pointer/solution.go index 2c15669..c5aae65 100644 --- a/src/main/go/g0101_0200/s0138_copy_list_with_random_pointer/solution.go +++ b/src/main/go/g0101_0200/s0138_copy_list_with_random_pointer/solution.go @@ -1,8 +1,8 @@ package s0138_copy_list_with_random_pointer // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List -// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N) -// #2024_03_19_Time_2_ms_(70.11%)_Space_3.6_MB_(96.36%) +// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Top_Interview_150_Linked_List +// #Big_O_Time_O(N)_Space_O(N) #2025_05_07_Time_0_ms_(100.00%)_Space_5.44_MB_(28.63%) type Node struct { Val int diff --git a/src/main/go/g0101_0200/s0139_word_break/solution.go b/src/main/go/g0101_0200/s0139_word_break/solution.go index 98f1ae8..d865ef8 100644 --- a/src/main/go/g0101_0200/s0139_word_break/solution.go +++ b/src/main/go/g0101_0200/s0139_word_break/solution.go @@ -2,8 +2,8 @@ package s0139_word_break // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table // #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming -// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max) -// #2024_03_20_Time_0_ms_(100.00%)_Space_2.4_MB_(16.32%) +// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP +// #Big_O_Time_O(M+max*N)_Space_O(M+N+max) #2025_05_07_Time_0_ms_(100.00%)_Space_4.24_MB_(37.18%) func wordBreak(s string, wordDict []string) bool { if len(wordDict) == 0 { diff --git a/src/main/go/g0101_0200/s0141_linked_list_cycle/solution.go b/src/main/go/g0101_0200/s0141_linked_list_cycle/solution.go index 6860859..4c1e57d 100644 --- a/src/main/go/g0101_0200/s0141_linked_list_cycle/solution.go +++ b/src/main/go/g0101_0200/s0141_linked_list_cycle/solution.go @@ -1,8 +1,8 @@ package s0141_linked_list_cycle // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List -// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1) -// #2024_03_20_Time_7_ms_(69.12%)_Space_4.6_MB_(82.42%) +// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Top_Interview_150_Linked_List +// #Big_O_Time_O(N)_Space_O(1) #2025_05_07_Time_4_ms_(76.55%)_Space_6.23_MB_(51.42%) type ListNode struct { Val int diff --git a/src/main/go/g0101_0200/s0142_linked_list_cycle_ii/solution.go b/src/main/go/g0101_0200/s0142_linked_list_cycle_ii/solution.go index 96b1744..caba4bc 100644 --- a/src/main/go/g0101_0200/s0142_linked_list_cycle_ii/solution.go +++ b/src/main/go/g0101_0200/s0142_linked_list_cycle_ii/solution.go @@ -2,7 +2,7 @@ package s0142_linked_list_cycle_ii // #Medium #Top_100_Liked_Questions #Hash_Table #Two_Pointers #Linked_List // #Data_Structure_II_Day_10_Linked_List #Level_1_Day_4_Linked_List #Udemy_Linked_List -// #Big_O_Time_O(N)_Space_O(1) #2024_03_20_Time_5_ms_(69.02%)_Space_3.8_MB_(98.54%) +// #Big_O_Time_O(N)_Space_O(1) #2025_05_07_Time_0_ms_(100.00%)_Space_6.80_MB_(18.18%) type ListNode struct { Val int @@ -17,26 +17,13 @@ type ListNode struct { * } */ func detectCycle(head *ListNode) *ListNode { - if head == nil || head.Next == nil { - return nil - } - slow := head - fast := head - for fast != nil && fast.Next != nil { - fast = fast.Next.Next - slow = slow.Next - // intersected inside the loop. - if slow == fast { - break + m := map[*ListNode]bool{} + for head != nil { + if m[head] == true { + return head } + m[head] = true + head = head.Next } - if fast == nil || fast.Next == nil { - return nil - } - slow = head - for slow != fast { - slow = slow.Next - fast = fast.Next - } - return slow + return nil } diff --git a/src/main/go/g0101_0200/s0146_lru_cache/lrucache.go b/src/main/go/g0101_0200/s0146_lru_cache/lrucache.go index 7c1db3e..c32166a 100644 --- a/src/main/go/g0101_0200/s0146_lru_cache/lrucache.go +++ b/src/main/go/g0101_0200/s0146_lru_cache/lrucache.go @@ -1,8 +1,8 @@ package s0146_lru_cache // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Design #Linked_List -// #Doubly_Linked_List #Udemy_Linked_List #Big_O_Time_O(1)_Space_O(capacity) -// #2024_03_21_Time_421_ms_(93.60%)_Space_69_MB_(91.02%) +// #Doubly_Linked_List #Udemy_Linked_List #Top_Interview_150_Linked_List +// #Big_O_Time_O(1)_Space_O(capacity) #2025_05_08_Time_75_ms_(84.31%)_Space_72.81_MB_(71.88%) import "container/list" diff --git a/src/main/go/g0101_0200/s0148_sort_list/solution.go b/src/main/go/g0101_0200/s0148_sort_list/solution.go index 93a157e..401d99b 100644 --- a/src/main/go/g0101_0200/s0148_sort_list/solution.go +++ b/src/main/go/g0101_0200/s0148_sort_list/solution.go @@ -1,10 +1,8 @@ package s0148_sort_list // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List -// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N)) -// #2024_03_21_Time_40_ms_(97.89%)_Space_7_MB_(83.99%) - -import "slices" +// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Top_Interview_150_Divide_and_Conquer +// #Big_O_Time_O(log(N))_Space_O(log(N)) #2025_05_08_Time_7_ms_(87.91%)_Space_8.75_MB_(91.94%) type ListNode struct { Val int @@ -19,21 +17,45 @@ type ListNode struct { * } */ func sortList(head *ListNode) *ListNode { - curr := head - a := []*ListNode{} - for curr != nil { - a = append(a, curr) - curr = curr.Next + if head == nil || head.Next == nil { + return head + } + mid := findMid(head) + left := sortList(head) + right := sortList(mid) + return merge(left, right) +} + +func findMid(head *ListNode) *ListNode { + var prev *ListNode + slow, fast := head, head + for fast != nil && fast.Next != nil { + prev = slow + slow = slow.Next + fast = fast.Next.Next + } + prev.Next = nil + return slow +} + +func merge(left, right *ListNode) *ListNode { + pointer := new(ListNode) + res := pointer + for left != nil && right != nil { + if left.Val < right.Val { + pointer.Next = left + left = left.Next + } else { + pointer.Next = right + right = right.Next + } + pointer = pointer.Next + } + if left != nil { + pointer.Next = left } - slices.SortFunc(a, func(l1, l2 *ListNode) int { - return l1.Val - l2.Val - }) - newNode := &ListNode{} - curr = newNode - for _, node := range a { - curr.Next = node - curr = node + if right != nil { + pointer.Next = right } - curr.Next = nil - return newNode.Next + return res.Next } diff --git a/src/main/go/g0101_0200/s0152_maximum_product_subarray/solution.go b/src/main/go/g0101_0200/s0152_maximum_product_subarray/solution.go index 094cd87..2695743 100644 --- a/src/main/go/g0101_0200/s0152_maximum_product_subarray/solution.go +++ b/src/main/go/g0101_0200/s0152_maximum_product_subarray/solution.go @@ -2,7 +2,7 @@ package s0152_maximum_product_subarray // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming // #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming -// #Big_O_Time_O(N)_Space_O(1) #2024_03_22_Time_0_ms_(100.00%)_Space_3.6_MB_(17.73%) +// #Big_O_Time_O(N)_Space_O(1) #2025_05_08_Time_0_ms_(100.00%)_Space_5.25_MB_(91.63%) func maxProduct(nums []int) int { res := findMax(nums...) diff --git a/src/main/go/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.go b/src/main/go/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.go index 52db7bc..3f1f48d 100644 --- a/src/main/go/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.go +++ b/src/main/go/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.go @@ -1,8 +1,8 @@ package s0153_find_minimum_in_rotated_sorted_array // #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search -// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N) -// #2024_03_22_Time_0_ms_(100.00%)_Space_2.7_MB_(12.41%) +// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Top_Interview_150_Binary_Search +// #Big_O_Time_O(log_N)_Space_O(log_N) #2025_05_08_Time_0_ms_(100.00%)_Space_4.49_MB_(14.09%) func findMin(nums []int) int { low, mid, high := 0, 0, len(nums)-1 diff --git a/src/main/go/g0101_0200/s0155_min_stack/minstack.go b/src/main/go/g0101_0200/s0155_min_stack/minstack.go index 483d45f..4052528 100644 --- a/src/main/go/g0101_0200/s0155_min_stack/minstack.go +++ b/src/main/go/g0101_0200/s0155_min_stack/minstack.go @@ -1,8 +1,9 @@ package s0155_min_stack -// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design +// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design // #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design -// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2024_03_22_Time_12_ms_(79.58%)_Space_7.4_MB_(21.27%) +// #Udemy_Design #Top_Interview_150_Stack #Big_O_Time_O(1)_Space_O(N) +// #2025_05_08_Time_0_ms_(100.00%)_Space_8.84_MB_(92.55%) import "math" diff --git a/src/main/go/g0101_0200/s0160_intersection_of_two_linked_lists/solution.go b/src/main/go/g0101_0200/s0160_intersection_of_two_linked_lists/solution.go index 79fd237..fda7313 100644 --- a/src/main/go/g0101_0200/s0160_intersection_of_two_linked_lists/solution.go +++ b/src/main/go/g0101_0200/s0160_intersection_of_two_linked_lists/solution.go @@ -2,7 +2,7 @@ package s0160_intersection_of_two_linked_lists // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List // #Data_Structure_II_Day_11_Linked_List #Udemy_Linked_List #Big_O_Time_O(M+N)_Space_O(1) -// #2024_03_22_Time_16_ms_(99.84%)_Space_6.8_MB_(74.72%) +// #2025_05_08_Time_24_ms_(85.37%)_Space_8.62_MB_(59.52%) type ListNode struct { Val int diff --git a/src/main/go/g0101_0200/s0169_majority_element/solution.go b/src/main/go/g0101_0200/s0169_majority_element/solution.go index 6458be6..e5fd0c0 100644 --- a/src/main/go/g0101_0200/s0169_majority_element/solution.go +++ b/src/main/go/g0101_0200/s0169_majority_element/solution.go @@ -2,7 +2,8 @@ package s0169_majority_element // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting // #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm -// #Big_O_Time_O(n)_Space_O(1) #2024_03_22_Time_15_ms_(70.01%)_Space_6.6_MB_(22.94%) +// #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(1) +// #2025_05_08_Time_0_ms_(100.00%)_Space_8.53_MB_(56.86%) func majorityElement(nums []int) int { count := 1 diff --git a/src/main/go/g0101_0200/s0189_rotate_array/solution.go b/src/main/go/g0101_0200/s0189_rotate_array/solution.go index 4e75b22..bdd162e 100644 --- a/src/main/go/g0101_0200/s0189_rotate_array/solution.go +++ b/src/main/go/g0101_0200/s0189_rotate_array/solution.go @@ -1,8 +1,8 @@ package s0189_rotate_array // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers -// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1) -// #2024_03_22_Time_20_ms_(92.61%)_Space_8.4_MB_(12.16%) +// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Top_Interview_150_Array/String +// #Big_O_Time_O(n)_Space_O(1) #2025_05_08_Time_0_ms_(100.00%)_Space_9.63_MB_(40.57%) func reverse(nums []int, l, r int) { for l <= r { diff --git a/src/main/go/g0101_0200/s0198_house_robber/solution.go b/src/main/go/g0101_0200/s0198_house_robber/solution.go index 8df89fd..3d634c3 100644 --- a/src/main/go/g0101_0200/s0198_house_robber/solution.go +++ b/src/main/go/g0101_0200/s0198_house_robber/solution.go @@ -1,9 +1,9 @@ package s0198_house_robber // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming -// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3 -// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n) -// #2024_03_22_Time_1_ms_(78.69%)_Space_2.3_MB_(7.60%) +// #LeetCode_75_DP/1D #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3 +// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP +// #Big_O_Time_O(n)_Space_O(n) #2025_05_08_Time_0_ms_(100.00%)_Space_3.88_MB_(99.40%) func rob(nums []int) int { if len(nums) == 0 { diff --git a/src/main/go/g0101_0200/s0200_number_of_islands/solution.go b/src/main/go/g0101_0200/s0200_number_of_islands/solution.go index 9b9fad1..78bb81a 100644 --- a/src/main/go/g0101_0200/s0200_number_of_islands/solution.go +++ b/src/main/go/g0101_0200/s0200_number_of_islands/solution.go @@ -4,7 +4,8 @@ package s0200_number_of_islands // #Breadth_First_Search #Matrix #Union_Find // #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search // #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph -// #Big_O_Time_O(M*N)_Space_O(M*N) #2024_03_22_Time_4_ms_(76.94%)_Space_4_MB_(44.10%) +// #Top_Interview_150_Graph_General #Big_O_Time_O(M*N)_Space_O(M*N) +// #2025_05_08_Time_2_ms_(87.44%)_Space_5.69_MB_(90.06%) func numIslands(grid [][]byte) int { islands := 0 diff --git a/src/main/go/g0201_0300/s0206_reverse_linked_list/solution.go b/src/main/go/g0201_0300/s0206_reverse_linked_list/solution.go index 15ccf57..548a997 100644 --- a/src/main/go/g0201_0300/s0206_reverse_linked_list/solution.go +++ b/src/main/go/g0201_0300/s0206_reverse_linked_list/solution.go @@ -1,9 +1,9 @@ package s0206_reverse_linked_list // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion -// #Data_Structure_I_Day_8_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking -// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1) -// #2024_03_22_Time_2_ms_(76.97%)_Space_2.7_MB_(21.67%) +// #LeetCode_75_LinkedList #Data_Structure_I_Day_8_Linked_List +// #Algorithm_I_Day_10_Recursion_Backtracking #Level_1_Day_3_Linked_List #Udemy_Linked_List +// #Big_O_Time_O(N)_Space_O(1) #2025_05_09_Time_0_ms_(100.00%)_Space_4.36_MB_(95.25%) type ListNode struct { Val int diff --git a/src/main/go/g0201_0300/s0207_course_schedule/solution.go b/src/main/go/g0201_0300/s0207_course_schedule/solution.go index 5105d8f..0f6a893 100644 --- a/src/main/go/g0201_0300/s0207_course_schedule/solution.go +++ b/src/main/go/g0201_0300/s0207_course_schedule/solution.go @@ -1,37 +1,46 @@ package s0207_course_schedule // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search -// #Breadth_First_Search #Graph #Topological_Sort #Big_O_Time_O(N)_Space_O(N) -// #2024_03_22_Time_7_ms_(83.17%)_Space_6.3_MB_(51.07%) +// #Breadth_First_Search #Graph #Topological_Sort #Top_Interview_150_Graph_General +// #Big_O_Time_O(N)_Space_O(N) #2025_05_09_Time_0_ms_(100.00%)_Space_7.98_MB_(72.75%) + +type State int + +const ( + Unvisited State = iota + Visiting + Visited +) func canFinish(numCourses int, prerequisites [][]int) bool { - graph := map[int][]int{} - for _, edge := range prerequisites { - graph[edge[1]] = append(graph[edge[1]], edge[0]) + visited := make([]State, numCourses) + graph := make([][]int, numCourses) + for _, dep := range prerequisites { + graph[dep[1]] = append(graph[dep[1]], dep[0]) } - indegree := make([]int, numCourses) - queue := []int{} for i := 0; i < numCourses; i++ { - for _, v := range graph[i] { - indegree[v]++ + if visited[i] == Unvisited { + if !dfs(i, visited, graph) { + return false + } } } - for i := 0; i < numCourses; i++ { - if indegree[i] == 0 { - queue = append(queue, i) - } + return true +} + +func dfs(start int, visited []State, graph [][]int) bool { + if visited[start] == Visiting { + return false } - visited := 0 - for len(queue) > 0 { - node := queue[0] - queue = queue[1:] - visited++ - for _, v := range graph[node] { - indegree[v]-- - if indegree[v] == 0 { - queue = append(queue, v) - } + if visited[start] == Visited { + return true + } + visited[start] = Visiting + for _, next := range graph[start] { + if !dfs(next, visited, graph) { + return false } } - return visited == numCourses + visited[start] = Visited + return true } diff --git a/src/main/go/g0201_0300/s0208_implement_trie_prefix_tree/trie.go b/src/main/go/g0201_0300/s0208_implement_trie_prefix_tree/trie.go index 12d2bd6..e82b899 100644 --- a/src/main/go/g0201_0300/s0208_implement_trie_prefix_tree/trie.go +++ b/src/main/go/g0201_0300/s0208_implement_trie_prefix_tree/trie.go @@ -1,9 +1,9 @@ package s0208_implement_trie_prefix_tree // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Design #Trie -// #Level_2_Day_16_Design #Udemy_Trie_and_Heap +// #LeetCode_75_Trie #Level_2_Day_16_Design #Udemy_Trie_and_Heap #Top_Interview_150_Trie // #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) -// #2024_03_22_Time_40_ms_(94.90%)_Space_8.6_MB_(96.69%) +// #2025_05_09_Time_11_ms_(98.49%)_Space_16.18_MB_(42.49%) import "strings" diff --git a/src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go b/src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go index 3ef92a4..681c16c 100644 --- a/src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go +++ b/src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go @@ -1,8 +1,9 @@ package s0215_kth_largest_element_in_an_array // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Heap_Priority_Queue -// #Divide_and_Conquer #Quickselect #Data_Structure_II_Day_20_Heap_Priority_Queue -// #Big_O_Time_O(n*log(n))_Space_O(log(n)) #2024_03_22_Time_62_ms_(98.32%)_Space_7.5_MB_(94.86%) +// #Divide_and_Conquer #Quickselect #LeetCode_75_Heap/Priority_Queue +// #Data_Structure_II_Day_20_Heap_Priority_Queue #Top_Interview_150_Heap +// #Big_O_Time_O(n*log(n))_Space_O(log(n)) #2025_05_09_Time_17_ms_(83.49%)_Space_10.00_MB_(66.67%) import "sort" diff --git a/src/main/go/g0201_0300/s0221_maximal_square/solution.go b/src/main/go/g0201_0300/s0221_maximal_square/solution.go index 0330aa8..73afa00 100644 --- a/src/main/go/g0201_0300/s0221_maximal_square/solution.go +++ b/src/main/go/g0201_0300/s0221_maximal_square/solution.go @@ -1,8 +1,8 @@ package s0221_maximal_square -// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix -// #Dynamic_Programming_I_Day_16 #Big_O_Time_O(m*n)_Space_O(m*n) -// #2024_03_22_Time_0_ms_(100.00%)_Space_7_MB_(29.80%) +// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_16 +// #Top_Interview_150_Multidimensional_DP #Big_O_Time_O(m*n)_Space_O(m*n) +// #2025_05_09_Time_1_ms_(93.36%)_Space_8.70_MB_(81.82%) func maximalSquare(matrix [][]byte) int { m := len(matrix) diff --git a/src/main/go/g0201_0300/s0226_invert_binary_tree/solution.go b/src/main/go/g0201_0300/s0226_invert_binary_tree/solution.go index 748da66..2216671 100644 --- a/src/main/go/g0201_0300/s0226_invert_binary_tree/solution.go +++ b/src/main/go/g0201_0300/s0226_invert_binary_tree/solution.go @@ -2,7 +2,8 @@ package s0226_invert_binary_tree // #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree // #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue -// #Big_O_Time_O(n)_Space_O(n) #2024_03_22_Time_0_ms_(100.00%)_Space_2.4_MB_(5.25%) +// #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(n)_Space_O(n) +// #2025_05_09_Time_0_ms_(100.00%)_Space_4.17_MB_(55.19%) type TreeNode struct { Val int diff --git a/src/main/go/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.go b/src/main/go/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.go index 7a6b88a..81a9e72 100644 --- a/src/main/go/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.go +++ b/src/main/go/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.go @@ -1,8 +1,9 @@ package s0230_kth_smallest_element_in_a_bst -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree -// #Binary_Search_Tree #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree -// #Big_O_Time_O(n)_Space_O(n) #2024_03_22_Time_0_ms_(100.00%)_Space_6.4_MB_(46.83%) +// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree +// #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree +// #Top_Interview_150_Binary_Search_Tree #Big_O_Time_O(n)_Space_O(n) +// #2025_05_09_Time_0_ms_(100.00%)_Space_8.52_MB_(17.71%) type TreeNode struct { Val int diff --git a/src/main/go/g0201_0300/s0234_palindrome_linked_list/solution.go b/src/main/go/g0201_0300/s0234_palindrome_linked_list/solution.go index c7e4c19..7d1f4e1 100644 --- a/src/main/go/g0201_0300/s0234_palindrome_linked_list/solution.go +++ b/src/main/go/g0201_0300/s0234_palindrome_linked_list/solution.go @@ -1,8 +1,8 @@ package s0234_palindrome_linked_list -// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Stack #Linked_List -// #Recursion #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1) -// #2024_03_23_Time_104_ms_(97.77%)_Space_8.2_MB_(84.40%) +// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion +// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1) +// #2025_05_09_Time_1_ms_(87.36%)_Space_10.64_MB_(70.30%) type ListNode struct { Val int diff --git a/src/main/go/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.go b/src/main/go/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.go index e6b3987..34fffc6 100644 --- a/src/main/go/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.go +++ b/src/main/go/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.go @@ -1,8 +1,9 @@ package s0236_lowest_common_ancestor_of_a_binary_tree -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree -// #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n) -// #2024_03_23_Time_5_ms_(90.18%)_Space_7.7_MB_(22.79%) +// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree +// #LeetCode_75_Binary_Tree/DFS #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue +// #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(n)_Space_O(n) +// #2025_05_09_Time_5_ms_(89.93%)_Space_9.14_MB_(38.67%) type TreeNode struct { Val int diff --git a/src/main/go/g0201_0300/s0238_product_of_array_except_self/solution.go b/src/main/go/g0201_0300/s0238_product_of_array_except_self/solution.go index 422d425..df82017 100644 --- a/src/main/go/g0201_0300/s0238_product_of_array_except_self/solution.go +++ b/src/main/go/g0201_0300/s0238_product_of_array_except_self/solution.go @@ -1,8 +1,8 @@ package s0238_product_of_array_except_self -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Prefix_Sum -// #Data_Structure_II_Day_5_Array #Udemy_Arrays #Big_O_Time_O(n^2)_Space_O(n) -// #2024_03_23_Time_20_ms_(77.65%)_Space_7.8_MB_(37.12%) +// #Medium #Top_100_Liked_Questions #Array #Prefix_Sum #LeetCode_75_Array/String +// #Data_Structure_II_Day_5_Array #Udemy_Arrays #Top_Interview_150_Array/String +// #Big_O_Time_O(n^2)_Space_O(n) #2025_05_09_Time_0_ms_(100.00%)_Space_9.70_MB_(86.27%) func productExceptSelf(nums []int) []int { n := len(nums) diff --git a/src/main/go/g0201_0300/s0239_sliding_window_maximum/solution.go b/src/main/go/g0201_0300/s0239_sliding_window_maximum/solution.go index 6873c51..34e4915 100644 --- a/src/main/go/g0201_0300/s0239_sliding_window_maximum/solution.go +++ b/src/main/go/g0201_0300/s0239_sliding_window_maximum/solution.go @@ -1,8 +1,8 @@ package s0239_sliding_window_maximum -// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue -// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k) -// #2024_03_18_Time_168_ms_(99.41%)_Space_9.3_MB_(64.37%) +// #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue +// #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k) +// #2025_05_09_Time_10_ms_(98.48%)_Space_19.08_MB_(19.58%) func maxSlidingWindow(nums []int, k int) []int { if k == 1 { diff --git a/src/main/go/g0201_0300/s0240_search_a_2d_matrix_ii/solution.go b/src/main/go/g0201_0300/s0240_search_a_2d_matrix_ii/solution.go index 00f3398..9d4c773 100644 --- a/src/main/go/g0201_0300/s0240_search_a_2d_matrix_ii/solution.go +++ b/src/main/go/g0201_0300/s0240_search_a_2d_matrix_ii/solution.go @@ -1,8 +1,8 @@ package s0240_search_a_2d_matrix_ii -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix -// #Divide_and_Conquer #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8 -// #Big_O_Time_O(n+m)_Space_O(1) #2024_03_18_Time_11_ms_(96.10%)_Space_6.6_MB_(39.51%) +// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Divide_and_Conquer +// #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8 #Big_O_Time_O(n+m)_Space_O(1) +// #2025_05_10_Time_9_ms_(98.86%)_Space_8.76_MB_(12.12%) func searchMatrix(matrix [][]int, target int) bool { r := 0 diff --git a/src/main/go/g0201_0300/s0283_move_zeroes/solution.go b/src/main/go/g0201_0300/s0283_move_zeroes/solution.go index 0e38510..0943531 100644 --- a/src/main/go/g0201_0300/s0283_move_zeroes/solution.go +++ b/src/main/go/g0201_0300/s0283_move_zeroes/solution.go @@ -1,8 +1,8 @@ package s0283_move_zeroes -// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Two_Pointers +// #Easy #Top_100_Liked_Questions #Array #Two_Pointers #LeetCode_75_Two_Pointers // #Algorithm_I_Day_3_Two_Pointers #Programming_Skills_I_Day_6_Array #Udemy_Arrays -// #Big_O_Time_O(n)_Space_O(1) #2024_03_18_Time_15_ms_(88.69%)_Space_8_MB_(5.38%) +// #Big_O_Time_O(n)_Space_O(1) #2025_05_10_Time_0_ms_(100.00%)_Space_9.28_MB_(18.25%) func moveZeroes(nums []int) { var j int diff --git a/src/main/go/g0201_0300/s0287_find_the_duplicate_number/solution.go b/src/main/go/g0201_0300/s0287_find_the_duplicate_number/solution.go index b79a182..8ae98d9 100644 --- a/src/main/go/g0201_0300/s0287_find_the_duplicate_number/solution.go +++ b/src/main/go/g0201_0300/s0287_find_the_duplicate_number/solution.go @@ -1,8 +1,8 @@ package s0287_find_the_duplicate_number -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Two_Pointers -// #Bit_Manipulation #Binary_Search_II_Day_5 #Big_O_Time_O(n)_Space_O(n) -// #2024_03_18_Time_68_ms_(98.63%)_Space_8.3_MB_(69.20%) +// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Two_Pointers #Bit_Manipulation +// #Binary_Search_II_Day_5 #Big_O_Time_O(n)_Space_O(n) +// #2025_05_10_Time_1_ms_(90.38%)_Space_10.41_MB_(41.51%) func findDuplicate(nums []int) int { arr := make([]int, len(nums)+1) diff --git a/src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go b/src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go index 76287e4..c3046a4 100644 --- a/src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go +++ b/src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go @@ -1,8 +1,8 @@ package s0295_find_median_from_data_stream -// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Design -// #Heap_Priority_Queue #Data_Stream #Big_O_Time_O(n*log_n)_Space_O(n) -// #2024_03_18_Time_240_ms_(100.00%)_Space_22.5_MB_(23.89%) +// #Hard #Top_100_Liked_Questions #Sorting #Two_Pointers #Design #Heap_Priority_Queue #Data_Stream +// #Top_Interview_150_Heap #Big_O_Time_O(n*log_n)_Space_O(n) +// #2025_05_10_Time_59_ms_(96.70%)_Space_25.46_MB_(10.38%) func pushHeap(a *[]int, x int) { *a = append(*a, x) diff --git a/src/main/go/g0201_0300/s0300_longest_increasing_subsequence/solution.go b/src/main/go/g0201_0300/s0300_longest_increasing_subsequence/solution.go index 9d60bfd..b951ac2 100644 --- a/src/main/go/g0201_0300/s0300_longest_increasing_subsequence/solution.go +++ b/src/main/go/g0201_0300/s0300_longest_increasing_subsequence/solution.go @@ -1,9 +1,9 @@ package s0300_longest_increasing_subsequence -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming -// #Binary_Search #Algorithm_II_Day_16_Dynamic_Programming #Binary_Search_II_Day_3 -// #Dynamic_Programming_I_Day_18 #Udemy_Dynamic_Programming #Big_O_Time_O(n*log_n)_Space_O(n) -// #2024_03_16_Time_5_ms_(91.77%)_Space_3.5_MB_(70.57%) +// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Binary_Search +// #Algorithm_II_Day_16_Dynamic_Programming #Binary_Search_II_Day_3 #Dynamic_Programming_I_Day_18 +// #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP #Big_O_Time_O(n*log_n)_Space_O(n) +// #2025_05_10_Time_0_ms_(100.00%)_Space_5.28_MB_(69.25%) import "math" diff --git a/src/main/go/g0301_0400/s0322_coin_change/solution.go b/src/main/go/g0301_0400/s0322_coin_change/solution.go index 3ddbdad..04c50af 100644 --- a/src/main/go/g0301_0400/s0322_coin_change/solution.go +++ b/src/main/go/g0301_0400/s0322_coin_change/solution.go @@ -1,9 +1,9 @@ package s0322_coin_change -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming -// #Breadth_First_Search #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20 -// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount) -// #2024_03_16_Time_10_ms_(73.33%)_Space_6.4_MB_(71.07%) +// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search +// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20 +// #Level_2_Day_12_Dynamic_Programming #Top_Interview_150_1D_DP #Big_O_Time_O(m*n)_Space_O(amount) +// #2025_05_10_Time_10_ms_(79.59%)_Space_8.61_MB_(51.21%) func coinChange(coins []int, amount int) int { dp := make([]int, amount+1) diff --git a/src/main/go/g0301_0400/s0338_counting_bits/solution.go b/src/main/go/g0301_0400/s0338_counting_bits/solution.go index d49c415..c31a1c8 100644 --- a/src/main/go/g0301_0400/s0338_counting_bits/solution.go +++ b/src/main/go/g0301_0400/s0338_counting_bits/solution.go @@ -1,7 +1,8 @@ package s0338_counting_bits -// #Easy #Top_100_Liked_Questions #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation -// #Big_O_Time_O(num)_Space_O(num) #2024_03_16_Time_0_ms_(100.00%)_Space_4.7_MB_(25.18%) +// #Easy #Dynamic_Programming #Bit_Manipulation #LeetCode_75_Bit_Manipulation +// #Udemy_Bit_Manipulation #Big_O_Time_O(num)_Space_O(num) +// #2025_05_10_Time_0_ms_(100.00%)_Space_6.48_MB_(48.26%) func countBits(num int) []int { result := make([]int, num+1) diff --git a/src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go b/src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go index 86f32f5..d4df1a1 100644 --- a/src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go +++ b/src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go @@ -1,9 +1,8 @@ package s0347_top_k_frequent_elements -// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting -// #Heap_Priority_Queue #Counting #Divide_and_Conquer #Quickselect #Bucket_Sort -// #Data_Structure_II_Day_20_Heap_Priority_Queue #Big_O_Time_O(n*log(n))_Space_O(k) -// #2024_03_16_Time_8_ms_(88.72%)_Space_6.2_MB_(61.53%) +// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting +// #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue +// #Big_O_Time_O(n*log(n))_Space_O(k) #2025_05_10_Time_0_ms_(100.00%)_Space_7.96_MB_(62.32%) import "sort" diff --git a/src/main/go/g0301_0400/s0394_decode_string/solution.go b/src/main/go/g0301_0400/s0394_decode_string/solution.go index 26b7e6b..8aa67fb 100644 --- a/src/main/go/g0301_0400/s0394_decode_string/solution.go +++ b/src/main/go/g0301_0400/s0394_decode_string/solution.go @@ -1,7 +1,8 @@ package s0394_decode_string -// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings -// #Big_O_Time_O(n)_Space_O(n) #2024_03_16_Time_1_ms_(76.33%)_Space_2.2_MB_(26.06%) +// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #LeetCode_75_Stack +// #Level_1_Day_14_Stack #Udemy_Strings #Big_O_Time_O(n)_Space_O(n) +// #2025_05_10_Time_0_ms_(100.00%)_Space_4.00_MB_(85.31%) import ( "strings" diff --git a/src/main/go/g0401_0500/s0416_partition_equal_subset_sum/solution.go b/src/main/go/g0401_0500/s0416_partition_equal_subset_sum/solution.go index 7a1507b..50b1bd3 100644 --- a/src/main/go/g0401_0500/s0416_partition_equal_subset_sum/solution.go +++ b/src/main/go/g0401_0500/s0416_partition_equal_subset_sum/solution.go @@ -1,23 +1,30 @@ package s0416_partition_equal_subset_sum // #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming -// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_03_12_Time_13_ms_(91.83%)_Space_2.5_MB_(98.44%) +// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2025_05_10_Time_4_ms_(97.02%)_Space_4.42_MB_(72.07%) func canPartition(nums []int) bool { - sums := 0 - for _, num := range nums { - sums += num + sum := 0 + for _, v := range nums { + sum += v } - if sums%2 == 1 { + if sum%2 == 1 { return false } - sums /= 2 - dp := make([]bool, sums+1) + sum = sum / 2 + dp := make([]bool, sum, sum) dp[0] = true - for _, num := range nums { - for sum := sums; sum >= num; sum-- { - dp[sum] = dp[sum] || dp[sum-num] + for _, n := range nums { + if n <= sum { + if dp[sum-n] == true { + return true + } + for j := sum - n - 1; j >= 0; j-- { + if dp[j] == true { + dp[j+n] = true + } + } } } - return dp[sums] + return false } diff --git a/src/main/go/g0401_0500/s0437_path_sum_iii/solution.go b/src/main/go/g0401_0500/s0437_path_sum_iii/solution.go index 9adcfd3..df8cd92 100644 --- a/src/main/go/g0401_0500/s0437_path_sum_iii/solution.go +++ b/src/main/go/g0401_0500/s0437_path_sum_iii/solution.go @@ -1,7 +1,7 @@ package s0437_path_sum_iii -// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree -// #Big_O_Time_O(n)_Space_O(n) #2024_03_12_Time_7_ms_(74.18%)_Space_5.3_MB_(19.02%) +// #Medium #Depth_First_Search #Tree #Binary_Tree #LeetCode_75_Binary_Tree/DFS #Level_2_Day_7_Tree +// #Big_O_Time_O(n)_Space_O(n) #2025_05_10_Time_0_ms_(100.00%)_Space_6.94_MB_(22.90%) type TreeNode struct { Val int diff --git a/src/main/go/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.go b/src/main/go/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.go index 14bd14c..d728a4b 100644 --- a/src/main/go/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.go +++ b/src/main/go/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.go @@ -3,7 +3,7 @@ package s0438_find_all_anagrams_in_a_string // #Medium #Top_100_Liked_Questions #String #Hash_Table #Sliding_Window // #Algorithm_II_Day_5_Sliding_Window #Programming_Skills_II_Day_12 // #Level_1_Day_12_Sliding_Window/Two_Pointer #Big_O_Time_O(n+m)_Space_O(1) -// #2024_03_12_Time_0_ms_(100.00%)_Space_5_MB_(45.34%) +// #2025_05_11_Time_0_ms_(100.00%)_Space_6.83_MB_(90.16%) func findAnagrams(s string, p string) []int { var mapArr [26]int diff --git a/src/main/go/g0401_0500/s0494_target_sum/solution.go b/src/main/go/g0401_0500/s0494_target_sum/solution.go index 0679ce4..fdd1609 100644 --- a/src/main/go/g0401_0500/s0494_target_sum/solution.go +++ b/src/main/go/g0401_0500/s0494_target_sum/solution.go @@ -1,7 +1,7 @@ package s0494_target_sum -// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Backtracking -// #Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s)) #2024_03_12_Time_7_ms_(80.54%)_Space_6.6_MB_(40.47%) +// #Medium #Array #Dynamic_Programming #Backtracking #Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s)) +// #2025_05_11_Time_3_ms_(84.35%)_Space_8.52_MB_(36.96%) import "math" @@ -11,17 +11,14 @@ func findTargetSumWays(nums []int, s int) int { for _, num := range nums { sum += num } - if s > sum || (sum+s)%2 != 0 { return 0 } - dp := make([][]int, (sum+s)/2+1) for i := range dp { dp[i] = make([]int, len(nums)+1) } dp[0][0] = 1 - for i := 0; i < len(nums); i++ { if nums[i] == 0 { dp[0][i+1] = dp[0][i] * 2 @@ -29,7 +26,6 @@ func findTargetSumWays(nums []int, s int) int { dp[0][i+1] = dp[0][i] } } - for i := 1; i < len(dp); i++ { for j := 0; j < len(nums); j++ { dp[i][j+1] += dp[i][j] @@ -38,6 +34,5 @@ func findTargetSumWays(nums []int, s int) int { } } } - return dp[(sum+s)/2][len(nums)] } diff --git a/src/main/go/g0501_0600/s0543_diameter_of_binary_tree/solution.go b/src/main/go/g0501_0600/s0543_diameter_of_binary_tree/solution.go index 4454e9b..0716369 100644 --- a/src/main/go/g0501_0600/s0543_diameter_of_binary_tree/solution.go +++ b/src/main/go/g0501_0600/s0543_diameter_of_binary_tree/solution.go @@ -2,7 +2,7 @@ package s0543_diameter_of_binary_tree // #Easy #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree // #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n) -// #2024_03_12_Time_3_ms_(71.60%)_Space_6.8_MB_(5.31%) +// #2025_05_11_Time_0_ms_(100.00%)_Space_8.78_MB_(30.66%) type TreeNode struct { Val int diff --git a/src/main/go/g0501_0600/s0560_subarray_sum_equals_k/solution.go b/src/main/go/g0501_0600/s0560_subarray_sum_equals_k/solution.go index 7cb6027..2257768 100644 --- a/src/main/go/g0501_0600/s0560_subarray_sum_equals_k/solution.go +++ b/src/main/go/g0501_0600/s0560_subarray_sum_equals_k/solution.go @@ -1,7 +1,7 @@ package s0560_subarray_sum_equals_k // #Medium #Top_100_Liked_Questions #Array #Hash_Table #Prefix_Sum #Data_Structure_II_Day_5_Array -// #Big_O_Time_O(n)_Space_O(n) #2024_03_10_Time_40_ms_(70.56%)_Space_7.4_MB_(50.68%) +// #Big_O_Time_O(n)_Space_O(n) #2025_05_11_Time_11_ms_(93.74%)_Space_9.28_MB_(56.67%) func subarraySum(nums []int, k int) int { tempSum := 0 diff --git a/src/main/go/g0601_0700/s0647_palindromic_substrings/solution.go b/src/main/go/g0601_0700/s0647_palindromic_substrings/solution.go index 59d0ee8..ad7be69 100644 --- a/src/main/go/g0601_0700/s0647_palindromic_substrings/solution.go +++ b/src/main/go/g0601_0700/s0647_palindromic_substrings/solution.go @@ -1,7 +1,7 @@ package s0647_palindromic_substrings -// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n) -// #2024_03_10_Time_0_ms_(100.00%)_Space_2.1_MB_(41.43%) +// #Medium #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n) +// #2025_05_11_Time_0_ms_(100.00%)_Space_4.02_MB_(35.44%) func expand(a []rune, l int, r int, res *int) { for l >= 0 && r < len(a) { diff --git a/src/main/go/g0701_0800/s0739_daily_temperatures/solution.go b/src/main/go/g0701_0800/s0739_daily_temperatures/solution.go index d29430b..0616b54 100644 --- a/src/main/go/g0701_0800/s0739_daily_temperatures/solution.go +++ b/src/main/go/g0701_0800/s0739_daily_temperatures/solution.go @@ -1,7 +1,8 @@ package s0739_daily_temperatures -// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #Programming_Skills_II_Day_6 -// #Big_O_Time_O(n)_Space_O(n) #2024_03_10_Time_120_ms_(98.07%)_Space_8.2_MB_(98.62%) +// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #LeetCode_75_Monotonic_Stack +// #Programming_Skills_II_Day_6 #Big_O_Time_O(n)_Space_O(n) +// #2025_05_11_Time_5_ms_(96.26%)_Space_10.49_MB_(86.49%) func dailyTemperatures(temperatures []int) []int { sol := make([]int, len(temperatures)) diff --git a/src/main/go/g0701_0800/s0763_partition_labels/solution.go b/src/main/go/g0701_0800/s0763_partition_labels/solution.go index 4b01b54..e3ef515 100644 --- a/src/main/go/g0701_0800/s0763_partition_labels/solution.go +++ b/src/main/go/g0701_0800/s0763_partition_labels/solution.go @@ -1,8 +1,7 @@ package s0763_partition_labels -// #Medium #Top_100_Liked_Questions #String #Hash_Table #Greedy #Two_Pointers -// #Data_Structure_II_Day_7_String #Big_O_Time_O(n)_Space_O(1) -// #2024_03_10_Time_0_ms_(100.00%)_Space_2.3_MB_(63.50%) +// #Medium #String #Hash_Table #Greedy #Two_Pointers #Data_Structure_II_Day_7_String +// #Big_O_Time_O(n)_Space_O(1) #2025_05_11_Time_0_ms_(100.00%)_Space_4.24_MB_(29.03%) func partitionLabels(s string) []int { letters := []rune(s) diff --git a/src/main/go/g1101_1200/s1143_longest_common_subsequence/solution.go b/src/main/go/g1101_1200/s1143_longest_common_subsequence/solution.go index cadd3c8..a4ef0c8 100644 --- a/src/main/go/g1101_1200/s1143_longest_common_subsequence/solution.go +++ b/src/main/go/g1101_1200/s1143_longest_common_subsequence/solution.go @@ -1,9 +1,9 @@ package s1143_longest_common_subsequence -// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming +// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming #LeetCode_75_DP/Multidimensional // #Algorithm_II_Day_17_Dynamic_Programming #Dynamic_Programming_I_Day_19 // #Udemy_Dynamic_Programming #Big_O_Time_O(n*m)_Space_O(n*m) -// #2024_03_10_Time_12_ms_(48.57%)_Space_18.6_MB_(27.42%) +// #2025_05_11_Time_11_ms_(84.94%)_Space_20.05_MB_(31.35%) func longestCommonSubsequence(text1 string, text2 string) int { n := len(text1)