forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2322 from Mohammed785/main
Create: 2002-maximum-product-of-the-length-of-two-palindromic-subsequences , 0179-largest-number, 0706-design-hashmap
- Loading branch information
Showing
17 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class Solution { | ||
public string LargestNumber(int[] nums) | ||
{ | ||
if(nums.All(_ => _ == 0)) return "0"; | ||
|
||
var s = nums.Select(_ => _.ToString()).ToList(); | ||
|
||
s.Sort((a, b) => (b+a).CompareTo(a+b)); | ||
|
||
return string.Concat(s); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
csharp/2002-maximum-product-of-the-length of-2-palindromic-subsequences.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
public class Solution { | ||
public int MaxProduct(string s) { | ||
if(s == null || s.Length < 2) | ||
return 0; | ||
if(s.Length == 2) | ||
return 1; | ||
|
||
int n = s.Length; | ||
int total = 1 << n; | ||
|
||
List<(int, int)> possible = new List<(int, int)>(); | ||
|
||
for(int i = 0; i < total; i++) { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for(int j = 0; j < n; j++) { | ||
if((i & (1 << j)) != 0) { | ||
sb.Append(s[j]); | ||
} | ||
} | ||
|
||
if(IsPalindrome(sb.ToString())) { | ||
possible.Add((i, sb.Length)); | ||
} | ||
} | ||
|
||
int ans = 0; | ||
for(int i = 0; i < possible.Count; i++) { | ||
int bitmask = possible[i].Item1; | ||
int count = possible[i].Item2; | ||
for(int j = i + 1; j < possible.Count; j++) { | ||
int bitmask2 = possible[j].Item1; | ||
int count2 = possible[j].Item2; | ||
if((bitmask & bitmask2) == 0) | ||
ans = Math.Max(ans, count * count2); | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
private bool IsPalindrome(string s){ | ||
int i = 0; | ||
int j = s.Length - 1; | ||
while(i < j) { | ||
if(s[i++] != s[j--]) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
func largestNumber(nums []int) string { | ||
ans, s := "", make([]string, len(nums)) | ||
for i, num := range nums { s[i] = strconv.Itoa(num) } | ||
sort.Slice(s, func(a, b int) bool { return s[a] + s[b] > s[b] + s[a] }) | ||
if s[0] == "0" { return "0" } | ||
for _, v := range s { ans += v } | ||
return ans | ||
} |
46 changes: 46 additions & 0 deletions
46
go/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
func maxProduct(s string) (res int) { | ||
dp := make([]int, 1<<len(s)) | ||
mask := (1 << len(s)) - 1 | ||
palindromeSize := func(s string, mask int) (res int) { | ||
i, j := 0, len(s) | ||
for i <= j { | ||
if mask&(1<<i) == 0 { | ||
i++ | ||
} else if mask&(1<<j) == 0 { | ||
j-- | ||
} else if s[i] != s[j] { | ||
return 0 | ||
} else { | ||
if i == j { | ||
res++ | ||
} else { | ||
res += 2 | ||
} | ||
i++ | ||
j-- | ||
} | ||
} | ||
return res | ||
} | ||
|
||
max := func(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
for m := 1; m <= mask; m++ { | ||
dp[m] = palindromeSize(s, m) | ||
} | ||
|
||
for m1 := mask; m1 > 0; m1-- { | ||
if dp[m1]*(len(s)-dp[m1]) <= res { | ||
continue | ||
} | ||
for m2 := mask ^ m1; m2 > 0; m2 = (m2 - 1) & (mask ^ m1) { | ||
res = max(res, dp[m1]*dp[m2]) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class ListNode { | ||
int key, val; | ||
ListNode next; | ||
public ListNode(int key, int val, ListNode next) { | ||
this.key = key; | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} | ||
class MyHashMap { | ||
static final int size = 19997; | ||
static final int mult = 12582917; | ||
ListNode[] data; | ||
public MyHashMap() { | ||
this.data = new ListNode[size]; | ||
} | ||
private int hash(int key) { | ||
return (int)((long)key * mult % size); | ||
} | ||
public void put(int key, int val) { | ||
remove(key); | ||
int h = hash(key); | ||
ListNode node = new ListNode(key, val, data[h]); | ||
data[h] = node; | ||
} | ||
public int get(int key) { | ||
int h = hash(key); | ||
ListNode node = data[h]; | ||
for (; node != null; node = node.next) | ||
if (node.key == key) return node.val; | ||
return -1; | ||
} | ||
public void remove(int key) { | ||
int h = hash(key); | ||
ListNode node = data[h]; | ||
if (node == null) return; | ||
if (node.key == key) data[h] = node.next; | ||
else for (; node.next != null; node = node.next) | ||
if (node.next.key == key) { | ||
node.next = node.next.next; | ||
return; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
java/2002-maximum-product-of-the-length of-2-palindromic-subsequences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public int maxProduct(String s) { | ||
int[] dp = new int[4096]; | ||
int res = 0, mask = (1 << s.length()) - 1; | ||
for (int m = 1; m <= mask; ++m) | ||
dp[m] = palSize(s, m); | ||
for (int m1 = mask; m1 > 0; --m1) | ||
if (dp[m1] * (s.length() - dp[m1]) > res) | ||
for(int m2 = mask ^ m1; m2 > 0; m2 = (m2 - 1) & (mask ^ m1)) | ||
res = Math.max(res, dp[m1] * dp[m2]); | ||
return res; | ||
} | ||
private int palSize(String s, int mask) { | ||
int p1 = 0, p2 = s.length(), res = 0; | ||
while (p1 <= p2) { | ||
if ((mask & (1 << p1)) == 0) | ||
++p1; | ||
else if ((mask & (1 << p2)) == 0) | ||
--p2; | ||
else if (s.charAt(p1) != s.charAt(p2)) | ||
return 0; | ||
else | ||
res += 1 + (p1++ != p2-- ? 1 : 0); | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {string} | ||
*/ | ||
var largestNumber = function (nums) { | ||
let largest = nums | ||
.map((n) => n.toString()) | ||
.sort((x, y) => y + x - (x + y)) | ||
.join(''); | ||
return largest[0] === '0' ? '0' : largest; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var MyHashMap = function () { | ||
this.map = new Map(); | ||
}; | ||
|
||
/** | ||
* @param {number} key | ||
* @param {number} value | ||
* @return {void} | ||
*/ | ||
MyHashMap.prototype.put = function (key, value) { | ||
this.map.set(key, value); | ||
}; | ||
|
||
/** | ||
* @param {number} key | ||
* @return {number} | ||
*/ | ||
MyHashMap.prototype.get = function (key) { | ||
const val = this.map.get(key); | ||
return val !== undefined ? val : -1; | ||
}; | ||
|
||
/** | ||
* @param {number} key | ||
* @return {void} | ||
*/ | ||
MyHashMap.prototype.remove = function (key) { | ||
this.map.delete(key); | ||
}; | ||
|
||
/** | ||
* Your MyHashMap object will be instantiated and called as such: | ||
* var obj = new MyHashMap() | ||
* obj.put(key,value) | ||
* var param_2 = obj.get(key) | ||
* obj.remove(key) | ||
*/ |
51 changes: 51 additions & 0 deletions
51
javascript/2002-maximum-product-of-the-length of-2-palindromic-subsequences.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
* Time Complexity: O(2^N) | ||
* Space Complexity: O(2^N) | ||
*/ | ||
var maxProduct = function (s) { | ||
const N = s.length; | ||
const first = new Array(1 << N).fill(0), | ||
last = new Array(1 << N).fill(0); | ||
for (let i = 0; i < N; i++) { | ||
for (let j = 1 << i; j < 1 << (i + 1); j++) { | ||
first[j] = i; | ||
} | ||
} | ||
for (let i = 0; i < N; i++) { | ||
for (let j = 1 << i; j < 1 << N; j += 1 << (i + 1)) { | ||
last[j] = i; | ||
} | ||
} | ||
const dp = Memo((m) => { | ||
if ((m & (m - 1)) === 0) { | ||
return m != 0; | ||
} | ||
const l = last[m], | ||
f = first[m]; | ||
const lb = 1 << l, | ||
fb = 1 << f; | ||
return Math.max( | ||
dp(m - lb), | ||
dp(m - fb), | ||
dp(m - lb - fb) + (s[l] == s[f]) * 2 | ||
); | ||
}); | ||
let ans = 0; | ||
for (let m = 1; m < 1 << N; m++) { | ||
ans = Math.max(ans, dp(m) * dp((1 << N) - 1 - m)); | ||
} | ||
return ans; | ||
}; | ||
|
||
var Memo = (func) => { | ||
const map = new Map(); | ||
var wrapper = (m) => { | ||
if (!map.get(m)) { | ||
map.set(m, func(m)); | ||
} | ||
return map.get(m); | ||
}; | ||
return wrapper; | ||
}; |
30 changes: 30 additions & 0 deletions
30
python/2002-maximum-product-of-the-length of-2-palindromic-subsequences.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Time Complexity: O(2^N) | ||
Space Complexity: O(2^N) | ||
""" | ||
class Solution: | ||
def maxProduct(self, s): | ||
n = len(s) | ||
|
||
first, last = [0]*(1<<n), [0]*(1<<n) | ||
|
||
for i in range(n): | ||
for j in range(1<<i, 1<<(i+1)): | ||
first[j] = i | ||
|
||
for i in range(n): | ||
for j in range(1<<i, 1<<n, 1<<(i+1)): | ||
last[j] = i | ||
|
||
@lru_cache(None) | ||
def dp(m): | ||
if m & (m-1) == 0: return m != 0 | ||
l, f = last[m], first[m] | ||
lb, fb = 1<<l, 1<<f | ||
return max(dp(m-lb), dp(m-fb), dp(m-lb-fb) + (s[l] == s[f]) * 2) | ||
|
||
ans = 0 | ||
for m in range(1, 1<<n): | ||
ans = max(ans, dp(m)*dp((1<<n) - 1 - m)) | ||
|
||
return ans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @param {Integer[]} nums | ||
# @return {String} | ||
def largest_number(nums) | ||
nums.sort! {|a, b| b.to_s + a.to_s <=> a.to_s + b.to_s}.join.to_i.to_s | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
impl Solution { | ||
pub fn largest_number(nums: Vec<i32>) -> String { | ||
let mut v: Vec<String> = nums.iter().map(|&num| num.to_string()).collect(); | ||
v.sort_by(|a: &String, b: &String| (b.clone() + a).cmp(&(a.clone() + b))); | ||
if v[0] == "0" { | ||
String::from("0") | ||
} else { | ||
v.join("") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
struct MyHashMap { | ||
buckets: Vec<Vec<(i32,i32)>>, | ||
} | ||
|
||
// Prime number of buckets to reduce collisions | ||
const N_BUCKETS: usize = 1031; | ||
|
||
impl MyHashMap { | ||
|
||
fn new() -> Self { | ||
Self{ buckets: vec![vec![]; N_BUCKETS] } | ||
} | ||
|
||
fn hash(key: i32) -> usize { | ||
key as usize % N_BUCKETS | ||
} | ||
|
||
fn find_entry(&mut self, key: i32) -> (&mut Vec<(i32, i32)>, Result<usize, usize>) { | ||
let bucket = &mut self.buckets[Self::hash(key)]; | ||
let result = bucket.binary_search_by(|(k, v)| k.cmp(&key)); | ||
(bucket, result) | ||
} | ||
|
||
fn put(&mut self, key: i32, value: i32) { | ||
match self.find_entry(key) { | ||
(bucket, Ok(index)) => bucket[index] = (key, value), | ||
(bucket, Err(index)) => bucket.insert(index, (key, value)), | ||
} | ||
} | ||
|
||
fn get(&self, key: i32) -> i32 { | ||
let bucket = &self.buckets[Self::hash(key)]; | ||
match bucket.binary_search_by(|(k, v)| k.cmp(&key)) { | ||
Ok(index) => bucket[index].1, | ||
Err(index) => -1, | ||
} | ||
} | ||
|
||
fn remove(&mut self, key: i32) { | ||
match self.find_entry(key) { | ||
(bucket, Ok(index)) => { bucket.remove(index); }, | ||
_ => (), | ||
} | ||
} | ||
} |
Oops, something went wrong.