Skip to content

Commit bab1710

Browse files
Solutions
1 parent 2e9459d commit bab1710

File tree

74 files changed

+4949
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+4949
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public int GetDecimalValue(ListNode head) {
14+
int ans = 0;
15+
for (; head != null; head = head.next) {
16+
ans = ans << 1 | head.val;
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
3+
let m = *nums.iter().max().unwrap() as usize;
4+
let mut d = vec![0; m + 1];
5+
let n = nums.len();
6+
7+
let mut s = vec![0; n + 1];
8+
for i in 0..n {
9+
s[i + 1] = s[i] + nums[i];
10+
}
11+
12+
let mut ans = 0;
13+
let mut j = 0;
14+
for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) {
15+
j = j.max(d[v as usize]);
16+
ans = ans.max(s[i] - s[j]);
17+
d[v as usize] = i;
18+
}
19+
20+
ans
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
5+
let mut vis = HashSet::new();
6+
let (mut ans, mut s, mut i) = (0, 0, 0);
7+
8+
for &x in &nums {
9+
while vis.contains(&x) {
10+
let y = nums[i];
11+
s -= y;
12+
vis.remove(&y);
13+
i += 1;
14+
}
15+
vis.insert(x);
16+
s += x;
17+
ans = ans.max(s);
18+
}
19+
20+
ans
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int MaximumGain(string s, int x, int y) {
3+
char a = 'a', b = 'b';
4+
if (x < y) {
5+
(x, y) = (y, x);
6+
(a, b) = (b, a);
7+
}
8+
9+
int ans = 0, cnt1 = 0, cnt2 = 0;
10+
foreach (char c in s) {
11+
if (c == a) {
12+
cnt1++;
13+
} else if (c == b) {
14+
if (cnt1 > 0) {
15+
ans += x;
16+
cnt1--;
17+
} else {
18+
cnt2++;
19+
}
20+
} else {
21+
ans += Math.Min(cnt1, cnt2) * y;
22+
cnt1 = 0;
23+
cnt2 = 0;
24+
}
25+
}
26+
27+
ans += Math.Min(cnt1, cnt2) * y;
28+
return ans;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
3+
let (mut a, mut b) = ('a', 'b');
4+
if x < y {
5+
std::mem::swap(&mut x, &mut y);
6+
std::mem::swap(&mut a, &mut b);
7+
}
8+
9+
let mut ans = 0;
10+
let mut cnt1 = 0;
11+
let mut cnt2 = 0;
12+
13+
for c in s.chars() {
14+
if c == a {
15+
cnt1 += 1;
16+
} else if c == b {
17+
if cnt1 > 0 {
18+
ans += x;
19+
cnt1 -= 1;
20+
} else {
21+
cnt2 += 1;
22+
}
23+
} else {
24+
ans += cnt1.min(cnt2) * y;
25+
cnt1 = 0;
26+
cnt2 = 0;
27+
}
28+
}
29+
30+
ans += cnt1.min(cnt2) * y;
31+
ans
32+
}
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
type Trie struct {
2+
children map[string]*Trie
3+
deleted bool
4+
}
5+
6+
func NewTrie() *Trie {
7+
return &Trie{
8+
children: make(map[string]*Trie),
9+
}
10+
}
11+
12+
func deleteDuplicateFolder(paths [][]string) (ans [][]string) {
13+
root := NewTrie()
14+
for _, path := range paths {
15+
cur := root
16+
for _, name := range path {
17+
if _, exists := cur.children[name]; !exists {
18+
cur.children[name] = NewTrie()
19+
}
20+
cur = cur.children[name]
21+
}
22+
}
23+
24+
g := make(map[string]*Trie)
25+
26+
var dfs func(*Trie) string
27+
dfs = func(node *Trie) string {
28+
if len(node.children) == 0 {
29+
return ""
30+
}
31+
var subs []string
32+
for name, child := range node.children {
33+
subs = append(subs, name+"("+dfs(child)+")")
34+
}
35+
sort.Strings(subs)
36+
s := strings.Join(subs, "")
37+
if existingNode, exists := g[s]; exists {
38+
node.deleted = true
39+
existingNode.deleted = true
40+
} else {
41+
g[s] = node
42+
}
43+
return s
44+
}
45+
46+
var dfs2 func(*Trie, []string)
47+
dfs2 = func(node *Trie, path []string) {
48+
if node.deleted {
49+
return
50+
}
51+
if len(path) > 0 {
52+
ans = append(ans, append([]string{}, path...))
53+
}
54+
for name, child := range node.children {
55+
dfs2(child, append(path, name))
56+
}
57+
}
58+
59+
dfs(root)
60+
dfs2(root, []string{})
61+
return ans
62+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Trie {
2+
Map<String, Trie> children;
3+
boolean deleted;
4+
5+
public Trie() {
6+
children = new HashMap<>();
7+
deleted = false;
8+
}
9+
}
10+
11+
class Solution {
12+
public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) {
13+
Trie root = new Trie();
14+
for (List<String> path : paths) {
15+
Trie cur = root;
16+
for (String name : path) {
17+
if (!cur.children.containsKey(name)) {
18+
cur.children.put(name, new Trie());
19+
}
20+
cur = cur.children.get(name);
21+
}
22+
}
23+
24+
Map<String, Trie> g = new HashMap<>();
25+
26+
var dfs = new Function<Trie, String>() {
27+
@Override
28+
public String apply(Trie node) {
29+
if (node.children.isEmpty()) {
30+
return "";
31+
}
32+
List<String> subs = new ArrayList<>();
33+
for (var entry : node.children.entrySet()) {
34+
subs.add(entry.getKey() + "(" + apply(entry.getValue()) + ")");
35+
}
36+
Collections.sort(subs);
37+
String s = String.join("", subs);
38+
if (g.containsKey(s)) {
39+
node.deleted = true;
40+
g.get(s).deleted = true;
41+
} else {
42+
g.put(s, node);
43+
}
44+
return s;
45+
}
46+
};
47+
48+
dfs.apply(root);
49+
50+
List<List<String>> ans = new ArrayList<>();
51+
List<String> path = new ArrayList<>();
52+
53+
var dfs2 = new Function<Trie, Void>() {
54+
@Override
55+
public Void apply(Trie node) {
56+
if (node.deleted) {
57+
return null;
58+
}
59+
if (!path.isEmpty()) {
60+
ans.add(new ArrayList<>(path));
61+
}
62+
for (Map.Entry<String, Trie> entry : node.children.entrySet()) {
63+
path.add(entry.getKey());
64+
apply(entry.getValue());
65+
path.remove(path.size() - 1);
66+
}
67+
return null;
68+
}
69+
};
70+
71+
dfs2.apply(root);
72+
73+
return ans;
74+
}
75+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Trie:
2+
def __init__(self):
3+
self.children: Dict[str, "Trie"] = defaultdict(Trie)
4+
self.deleted: bool = False
5+
6+
7+
class Solution:
8+
def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:
9+
root = Trie()
10+
for path in paths:
11+
cur = root
12+
for name in path:
13+
if cur.children[name] is None:
14+
cur.children[name] = Trie()
15+
cur = cur.children[name]
16+
17+
g: Dict[str, Trie] = {}
18+
19+
def dfs(node: Trie) -> str:
20+
if not node.children:
21+
return ""
22+
subs: List[str] = []
23+
for name, child in node.children.items():
24+
subs.append(f"{name}({dfs(child)})")
25+
s = "".join(sorted(subs))
26+
if s in g:
27+
node.deleted = g[s].deleted = True
28+
else:
29+
g[s] = node
30+
return s
31+
32+
def dfs2(node: Trie) -> None:
33+
if node.deleted:
34+
return
35+
if path:
36+
ans.append(path[:])
37+
for name, child in node.children.items():
38+
path.append(name)
39+
dfs2(child)
40+
path.pop()
41+
42+
dfs(root)
43+
ans: List[List[str]] = []
44+
path: List[str] = []
45+
dfs2(root)
46+
return ans
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function deleteDuplicateFolder(paths: string[][]): string[][] {
2+
class Trie {
3+
children: { [key: string]: Trie } = {};
4+
deleted: boolean = false;
5+
}
6+
7+
const root = new Trie();
8+
9+
for (const path of paths) {
10+
let cur = root;
11+
for (const name of path) {
12+
if (!cur.children[name]) {
13+
cur.children[name] = new Trie();
14+
}
15+
cur = cur.children[name];
16+
}
17+
}
18+
19+
const g: { [key: string]: Trie } = {};
20+
21+
const dfs = (node: Trie): string => {
22+
if (Object.keys(node.children).length === 0) return '';
23+
24+
const subs: string[] = [];
25+
for (const [name, child] of Object.entries(node.children)) {
26+
subs.push(`${name}(${dfs(child)})`);
27+
}
28+
subs.sort();
29+
const s = subs.join('');
30+
31+
if (g[s]) {
32+
node.deleted = true;
33+
g[s].deleted = true;
34+
} else {
35+
g[s] = node;
36+
}
37+
return s;
38+
};
39+
40+
dfs(root);
41+
42+
const ans: string[][] = [];
43+
const path: string[] = [];
44+
45+
const dfs2 = (node: Trie): void => {
46+
if (node.deleted) return;
47+
if (path.length > 0) {
48+
ans.push([...path]);
49+
}
50+
for (const [name, child] of Object.entries(node.children)) {
51+
path.push(name);
52+
dfs2(child);
53+
path.pop();
54+
}
55+
};
56+
57+
dfs2(root);
58+
59+
return ans;
60+
}

0 commit comments

Comments
 (0)