Skip to content

Commit 96a6a66

Browse files
authored
Added Brute Force Solution (#31)
* added: brute force soln for Longest Substring Without Repeating Characters * Added brute force solution for Coin Change * Updated Longest Substring Without Repeating Characters
1 parent 42d52a1 commit 96a6a66

7 files changed

+162
-0
lines changed

Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
#include <algorithm>
33
using namespace std;
44

5+
6+
/**
7+
* Brute force approach with simple recursion
8+
*/
9+
class Solution {
10+
public:
11+
int coinChange(vector<int>& coins, int amount) {
12+
if (amount == 0)
13+
return 0;
14+
else if (amount < 0)
15+
return -1;
16+
17+
int min_cnt = -1;
18+
for (int coin : coins) {
19+
int cnt = coinChange(coins, amount - coin);
20+
if (cnt >= 0)
21+
min_cnt = min_cnt < 0 ? cnt + 1 : min(min_cnt, cnt + 1);
22+
}
23+
return min_cnt;
24+
}
25+
};
26+
27+
28+
529
class Solution {
630
public:
731
int coinChange(vector<int>& coins, int amount) {

Coin Change - Leetcode 322/Coin Change - Leetcode 322.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import java.util.Arrays;
22

3+
4+
/**
5+
* Brute force approach with simple recursion
6+
*/
7+
class Solution {
8+
public int coinChange(int[] coins, int amount) {
9+
if (amount == 0)
10+
return 0;
11+
else if (amount < 0)
12+
return -1;
13+
14+
int min_cnt = -1;
15+
for (int coin : coins) {
16+
int cnt = coinChange(coins, amount - coin);
17+
if (cnt >= 0)
18+
min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1);
19+
}
20+
return min_cnt;
21+
}
22+
}
23+
24+
25+
326
public class Solution {
427
public int coinChange(int[] coins, int amount) {
528
int[] dp = new int[amount + 1];

Coin Change - Leetcode 322/Coin Change - Leetcode 322.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/**
2+
* Brute force approach with simple recursion
3+
*
4+
* @param {number[]} coins
5+
* @param {number} amount
6+
* @return {number}
7+
*/
8+
var coinChange = function(coins, amount) {
9+
if (amount == 0)
10+
return 0;
11+
else if (amount < 0)
12+
return -1;
13+
14+
let min_cnt = -1;
15+
for (let coin of coins) {
16+
let cnt = coinChange(coins, amount - coin);
17+
if (cnt >= 0)
18+
min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1);
19+
}
20+
return min_cnt;
21+
};
22+
23+
124
/**
225
* @param {number[]} coins
326
* @param {number} amount

Coin Change - Leetcode 322/Coin Change - Leetcode 322.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
# Brute force with simple recursion
4+
# Time: O(Coins ^ Amount)
5+
# Space: O(Amount)
6+
if amount == 0:
7+
return 0
8+
elif amount < 0:
9+
return -1
10+
11+
min_cnt = -1
12+
for coin in coins:
13+
cnt = self.coinChange(coins, amount - coin)
14+
if cnt >= 0:
15+
min_cnt = cnt + 1 if min_cnt < 0 else min(min_cnt, cnt + 1)
16+
return min_cnt
17+
18+
19+
120
class Solution:
221
def coinChange(self, coins: List[int], amount: int) -> int:
322
# Top Down DP (Memoization)

Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
#include <algorithm>
33
using namespace std;
44

5+
6+
7+
class Solution {
8+
public:
9+
int lengthOfLongestSubstring(const string s) {
10+
11+
int longest = 0;
12+
13+
for (auto i = 0; i < s.size(); ++i) {
14+
for (auto substr_len = 1; i + substr_len <= s.size(); ++substr_len) {
15+
unordered_set<char> seen{s.cbegin() + i, s.cbegin() + i + substr_len};
16+
if (seen.size() == substr_len)
17+
longest = max(longest, substr_len);
18+
}
19+
}
20+
21+
return longest;
22+
}
23+
};
24+
// Time Complexity: O(n^3)
25+
// Space Complexity: O(n)
26+
27+
28+
529
class Solution {
630
public:
731
int lengthOfLongestSubstring(string s) {

Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import java.util.HashSet;
22

3+
4+
// brute force
5+
// Time Complexity: O(n^3)
6+
// Space Complexity: O(n)
7+
public class Solution {
8+
public int lengthOfLongestSubstring(String s) {
9+
int longest = 0;
10+
int n = s.length();
11+
for (int i = 0; i < n; i++) {
12+
for (int substr_len = 1; i + substr_len <= n; ++substr_len) {
13+
HashSet<Character> seen = new HashSet<>();
14+
for (int j = i; j < i + substr_len; ++j)
15+
seen.add(s.charAt(j));
16+
17+
if (seen.size() == substr_len)
18+
longest = Math.max(longest, substr_len);
19+
}
20+
}
21+
22+
return longest;
23+
}
24+
}
25+
26+
27+
28+
329
public class Solution {
430
public int lengthOfLongestSubstring(String s) {
531
HashSet<Character> set = new HashSet<>();

Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// brute force
2+
// Time Complexity: O(n^3)
3+
// Space Complexity: O(n)
4+
var lengthOfLongestSubstring = function(s) {
5+
6+
let longest = 0;
7+
let n = s.length;
8+
for (let i = 0; i < n; ++i) {
9+
for (let substr_len = 1; i + substr_len <= n; ++substr_len) {
10+
var seen = new Set();
11+
for (let j = i; j < i + substr_len; ++j) {
12+
seen.add(s[j]);
13+
}
14+
if (seen.size == substr_len) {
15+
longest = Math.max(longest, substr_len);
16+
}
17+
}
18+
}
19+
20+
return longest;
21+
};
22+
23+
124
var lengthOfLongestSubstring = function(s) {
225
const set = new Set();
326
let l = 0, longest = 0;

0 commit comments

Comments
 (0)