LeetCode - Blind 75 - Maximum Subarray

The problem Given an integer array nums, find the subarray with the largest sum, and return its sum. A subarray is a contiguous non-empty sequence of elements within an array. Examples Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. Constraints 1 <= nums.length <= 10⁵ -10⁴ <= nums[i] <= 10⁴ Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. ...

April 10, 2025 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Longest Common Subsequence

The problem Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, ”ace” is a subsequence of ”abcde”. A common subsequence of two strings is a subsequence that is common to both strings. ...

April 8, 2025 · 7 min · Dmytro Chumakov

LeetCode - Blind 75 - Unique Paths

The problem There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. ...

April 5, 2025 · 4 min · Dmytro Chumakov

LeetCode - Blind 75 - Longest Increasing Subsequence

The problem Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Examples Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Input: nums = [0,1,0,3,2,3] Output: 4 Input: nums = [7,7,7,7,7,7,7] Output: 1 Constraints 1 <= nums.length <= 2500 -10⁴ <= nums[i] <= 10⁴ Follow-up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? ...

April 3, 2025 · 5 min · Dmytro Chumakov

LeetCode - Blind 75 - Word Break

The problem Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words. Note that the same word in the dictionary may be reused multiple times in the segmentation. Examples Input: s = "leetcode", wordDict = ["leet","code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code". Input: s = "applepenapple", wordDict = ["apple","pen"] Output: true Explanation: Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word. Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] Output: false Constraints 1 <= s.length <= 300 1 <= wordDict.length <= 1000 1 <= wordDict[i].length <= 20 s and wordDict[i] consist of only lowercase English letters. All the strings of wordDict are unique. Recursion solution func wordBreak(_ s: String, _ wordDict: [String]) -> Bool { let sCount = s.count let sArray = Array(s) func dfs(_ i: Int) -> Bool { if i == sCount { return true } for w in wordDict { let wCount = w.count if ((i + wCount) <= sCount && String(sArray[i ..< i + wCount]) == w ) { if dfs(i + wCount) { return true } } } return false } return dfs(0) } Explanation There are multiple ways to solve this problem. For example, we can check every character in input s and compare it with characters in wordDict, but this approach is inefficient from a time complexity perspective. Instead: ...

March 31, 2025 · 5 min · Dmytro Chumakov