diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp index 32eb939..0482c5d 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp @@ -2,6 +2,30 @@ #include using namespace std; + +/** + * Brute force approach with simple recursion + */ +class Solution { +public: + int coinChange(vector& coins, int amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + int min_cnt = -1; + for (int coin : coins) { + int cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : min(min_cnt, cnt + 1); + } + return min_cnt; + } +}; + + + class Solution { public: int coinChange(vector& coins, int amount) { diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java index c7d1ee4..59c5ade 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java @@ -1,5 +1,28 @@ import java.util.Arrays; + +/** + * Brute force approach with simple recursion + */ +class Solution { + public int coinChange(int[] coins, int amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + int min_cnt = -1; + for (int coin : coins) { + int cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1); + } + return min_cnt; + } +} + + + public class Solution { public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js index 14fd04c..e6aee29 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js @@ -1,3 +1,26 @@ +/** + * Brute force approach with simple recursion + * + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function(coins, amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + let min_cnt = -1; + for (let coin of coins) { + let cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1); + } + return min_cnt; +}; + + /** * @param {number[]} coins * @param {number} amount diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py index c35592d..6d88a3e 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py @@ -1,3 +1,22 @@ +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + # Brute force with simple recursion + # Time: O(Coins ^ Amount) + # Space: O(Amount) + if amount == 0: + return 0 + elif amount < 0: + return -1 + + min_cnt = -1 + for coin in coins: + cnt = self.coinChange(coins, amount - coin) + if cnt >= 0: + min_cnt = cnt + 1 if min_cnt < 0 else min(min_cnt, cnt + 1) + return min_cnt + + + class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # Top Down DP (Memoization) diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp index 250c098..1932b34 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp @@ -2,6 +2,30 @@ #include using namespace std; + + +class Solution { +public: + int lengthOfLongestSubstring(const string s) { + + int longest = 0; + + for (auto i = 0; i < s.size(); ++i) { + for (auto substr_len = 1; i + substr_len <= s.size(); ++substr_len) { + unordered_set seen{s.cbegin() + i, s.cbegin() + i + substr_len}; + if (seen.size() == substr_len) + longest = max(longest, substr_len); + } + } + + return longest; + } +}; +// Time Complexity: O(n^3) +// Space Complexity: O(n) + + + class Solution { public: int lengthOfLongestSubstring(string s) { diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java index 510219a..1fdaaac 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java @@ -1,5 +1,31 @@ import java.util.HashSet; + +// brute force +// Time Complexity: O(n^3) +// Space Complexity: O(n) +public class Solution { + public int lengthOfLongestSubstring(String s) { + int longest = 0; + int n = s.length(); + for (int i = 0; i < n; i++) { + for (int substr_len = 1; i + substr_len <= n; ++substr_len) { + HashSet seen = new HashSet<>(); + for (int j = i; j < i + substr_len; ++j) + seen.add(s.charAt(j)); + + if (seen.size() == substr_len) + longest = Math.max(longest, substr_len); + } + } + + return longest; + } +} + + + + public class Solution { public int lengthOfLongestSubstring(String s) { HashSet set = new HashSet<>(); diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js index 807da86..499fccf 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js @@ -1,3 +1,26 @@ +// brute force +// Time Complexity: O(n^3) +// Space Complexity: O(n) +var lengthOfLongestSubstring = function(s) { + + let longest = 0; + let n = s.length; + for (let i = 0; i < n; ++i) { + for (let substr_len = 1; i + substr_len <= n; ++substr_len) { + var seen = new Set(); + for (let j = i; j < i + substr_len; ++j) { + seen.add(s[j]); + } + if (seen.size == substr_len) { + longest = Math.max(longest, substr_len); + } + } + } + + return longest; +}; + + var lengthOfLongestSubstring = function(s) { const set = new Set(); let l = 0, longest = 0;