Skip to content

Commit 4bbe31b

Browse files
committed
May-28
1 parent e24713f commit 4bbe31b

20 files changed

+1308
-27
lines changed

README.md

Lines changed: 36 additions & 16 deletions
Large diffs are not rendered by default.

leetcode/1089.duplicate-zeros.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Tag: Array, Two Pointers
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
8+
// Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: arr = [1,0,2,3,0,4,5,0]
13+
// Output: [1,0,0,2,3,0,0,4]
14+
// Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
15+
//
16+
// Example 2:
17+
//
18+
// Input: arr = [1,2,3]
19+
// Output: [1,2,3]
20+
// Explanation: After calling your function, the input array is modified to: [1,2,3]
21+
//
22+
//  
23+
// Constraints:
24+
//
25+
// 1 <= arr.length <= 104
26+
// 0 <= arr[i] <= 9
27+
//
28+
//
29+
30+
class Solution {
31+
public:
32+
void duplicateZeros(vector<int>& arr) {
33+
int n = arr.size();
34+
int shift = 0;
35+
int i = 0;
36+
for (; i + shift < n; i++) {
37+
shift += arr[i] == 0;
38+
}
39+
40+
for (int j = i - 1; shift > 0; j--) {
41+
if (j + shift < n) {
42+
arr[j + shift] = arr[j];
43+
}
44+
45+
if (arr[j] == 0) {
46+
arr[j + shift - 1] = arr[j];
47+
shift -= 1;
48+
}
49+
50+
}
51+
52+
53+
}
54+
};

leetcode/1089.duplicate-zeros.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Tag: Array, Two Pointers
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
8+
# Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: arr = [1,0,2,3,0,4,5,0]
13+
# Output: [1,0,0,2,3,0,0,4]
14+
# Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
15+
#
16+
# Example 2:
17+
#
18+
# Input: arr = [1,2,3]
19+
# Output: [1,2,3]
20+
# Explanation: After calling your function, the input array is modified to: [1,2,3]
21+
#
22+
#  
23+
# Constraints:
24+
#
25+
# 1 <= arr.length <= 104
26+
# 0 <= arr[i] <= 9
27+
#
28+
#
29+
30+
class Solution:
31+
def duplicateZeros(self, arr: List[int]) -> None:
32+
"""
33+
Do not return anything, modify arr in-place instead.
34+
"""
35+
n = len(arr)
36+
shift = 0
37+
i = 0
38+
while i + shift < n:
39+
shift += arr[i] == 0
40+
i += 1
41+
42+
for j in range(i - 1, -1, -1):
43+
if j + shift < n:
44+
arr[j + shift] = arr[j]
45+
46+
if arr[j] == 0:
47+
arr[j + shift - 1] = arr[j]
48+
shift -= 1
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Tag: Hash Table, Dynamic Programming, Graph, Topological Sort, Memoization, Counting
2+
// Time: O(N + M)
3+
// Space: O(N + M)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/_zRcPgszGK8
7+
8+
// There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.
9+
// You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates that there is a directed edge from node aj to node bj.
10+
// A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk such that there is a directed edge from xi to xi+1 for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
11+
// Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.
12+
//  
13+
// Example 1:
14+
//
15+
//
16+
// Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
17+
// Output: 3
18+
// Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
19+
//
20+
// Example 2:
21+
//
22+
//
23+
// Input: colors = "a", edges = [[0,0]]
24+
// Output: -1
25+
// Explanation: There is a cycle from 0 to 0.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// n == colors.length
31+
// m == edges.length
32+
// 1 <= n <= 105
33+
// 0 <= m <= 105
34+
// colors consists of lowercase English letters.
35+
// 0 <= aj, bj < n
36+
//
37+
38+
class Solution {
39+
public:
40+
int largestPathValue(string colors, vector<vector<int>>& edges) {
41+
int n = colors.size();
42+
int res = 0;
43+
vector<vector<int>>dp(n, vector<int>(26, 0));
44+
unordered_map<int, vector<int>> graph;
45+
vector<int> indegree(n, 0);
46+
for (auto &e: edges) {
47+
graph[e[0]].push_back(e[1]);
48+
indegree[e[1]] += 1;
49+
}
50+
51+
queue<int> q;
52+
for (int i = 0; i < n; i++) {
53+
if (indegree[i] == 0) {
54+
q.push(i);
55+
}
56+
}
57+
58+
int visited = 0;
59+
while (!q.empty()) {
60+
int i = q.front();
61+
q.pop();
62+
visited += 1;
63+
dp[i][colors[i] - 'a'] += 1;
64+
res = max(res, dp[i][colors[i] - 'a']);
65+
66+
for (auto &j: graph[i]) {
67+
for (int c = 0; c < 26; c++) {
68+
dp[j][c] = max(dp[j][c], dp[i][c]);
69+
}
70+
indegree[j] -= 1;
71+
if (indegree[j] == 0) {
72+
q.push(j);
73+
}
74+
}
75+
}
76+
return visited == n ? res : -1;
77+
}
78+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Tag: Hash Table, Dynamic Programming, Graph, Topological Sort, Memoization, Counting
2+
# Time: O(N + M)
3+
# Space: O(N + M)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/_zRcPgszGK8
7+
8+
# There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.
9+
# You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates that there is a directed edge from node aj to node bj.
10+
# A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk such that there is a directed edge from xi to xi+1 for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
11+
# Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.
12+
#  
13+
# Example 1:
14+
#
15+
#
16+
# Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
17+
# Output: 3
18+
# Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
19+
#
20+
# Example 2:
21+
#
22+
#
23+
# Input: colors = "a", edges = [[0,0]]
24+
# Output: -1
25+
# Explanation: There is a cycle from 0 to 0.
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# n == colors.length
31+
# m == edges.length
32+
# 1 <= n <= 105
33+
# 0 <= m <= 105
34+
# colors consists of lowercase English letters.
35+
# 0 <= aj, bj < n
36+
#
37+
38+
from collections import defaultdict, deque
39+
class Solution:
40+
def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
41+
n = len(colors)
42+
graph = defaultdict(list)
43+
indegree = defaultdict(int)
44+
for s, e in edges:
45+
graph[s].append(e)
46+
indegree[e] += 1
47+
48+
q = deque()
49+
dp = [[0] * 26 for i in range(n)]
50+
res = 0
51+
for i in range(n):
52+
if indegree[i] == 0:
53+
q.append(i)
54+
55+
visited = 0
56+
while len(q) > 0:
57+
i = q.popleft()
58+
visited += 1
59+
k = ord(colors[i]) - ord('a')
60+
dp[i][k] += 1
61+
res = max(res, dp[i][k])
62+
63+
for j in graph[i]:
64+
for c in range(26):
65+
dp[j][c] = max(dp[j][c], dp[i][c])
66+
67+
indegree[j] -= 1
68+
if indegree[j] == 0:
69+
q.append(j)
70+
71+
return res if visited == n else -1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Tag: Array, Hash Table, String, Greedy, Counting
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/NJQJtJu8drg
7+
8+
// You are given an array of strings words. Each element of words consists of two lowercase English letters.
9+
// Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected at most once.
10+
// Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.
11+
// A palindrome is a string that reads the same forward and backward.
12+
//  
13+
// Example 1:
14+
//
15+
// Input: words = ["lc","cl","gg"]
16+
// Output: 6
17+
// Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
18+
// Note that "clgglc" is another longest palindrome that can be created.
19+
//
20+
// Example 2:
21+
//
22+
// Input: words = ["ab","ty","yt","lc","cl","ab"]
23+
// Output: 8
24+
// Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
25+
// Note that "lcyttycl" is another longest palindrome that can be created.
26+
//
27+
// Example 3:
28+
//
29+
// Input: words = ["cc","ll","xx"]
30+
// Output: 2
31+
// Explanation: One longest palindrome is "cc", of length 2.
32+
// Note that "ll" is another longest palindrome that can be created, and so is "xx".
33+
//
34+
//  
35+
// Constraints:
36+
//
37+
// 1 <= words.length <= 105
38+
// words[i].length == 2
39+
// words[i] consists of lowercase English letters.
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
int longestPalindrome(vector<string>& words) {
46+
unordered_map<string, int> counter;
47+
for (auto &s: words) {
48+
counter[s] += 1;
49+
}
50+
51+
int res = 0;
52+
int center = 0;
53+
for (auto &[w, count]: counter) {
54+
auto reversed = string(w.rbegin(), w.rend());
55+
if (w == reversed) {
56+
res += 2 * (count / 2) ;
57+
center = max(center, count % 2);
58+
} else {
59+
auto it = counter.find(reversed);
60+
if (it != counter.end()) {
61+
res += min(count, it->second);
62+
}
63+
}
64+
}
65+
66+
return (res + center) * 2;
67+
}
68+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Tag: Array, Hash Table, String, Greedy, Counting
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/NJQJtJu8drg
7+
8+
# You are given an array of strings words. Each element of words consists of two lowercase English letters.
9+
# Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected at most once.
10+
# Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.
11+
# A palindrome is a string that reads the same forward and backward.
12+
#  
13+
# Example 1:
14+
#
15+
# Input: words = ["lc","cl","gg"]
16+
# Output: 6
17+
# Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
18+
# Note that "clgglc" is another longest palindrome that can be created.
19+
#
20+
# Example 2:
21+
#
22+
# Input: words = ["ab","ty","yt","lc","cl","ab"]
23+
# Output: 8
24+
# Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
25+
# Note that "lcyttycl" is another longest palindrome that can be created.
26+
#
27+
# Example 3:
28+
#
29+
# Input: words = ["cc","ll","xx"]
30+
# Output: 2
31+
# Explanation: One longest palindrome is "cc", of length 2.
32+
# Note that "ll" is another longest palindrome that can be created, and so is "xx".
33+
#
34+
#  
35+
# Constraints:
36+
#
37+
# 1 <= words.length <= 105
38+
# words[i].length == 2
39+
# words[i] consists of lowercase English letters.
40+
#
41+
#
42+
43+
from collections import Counter
44+
class Solution:
45+
def longestPalindrome(self, words: List[str]) -> int:
46+
counter = Counter(words)
47+
res = 0
48+
center = 0
49+
50+
for w in counter:
51+
p = w[::-1]
52+
if w == p:
53+
count = counter[w]
54+
res += 2 * (count // 2)
55+
center = max(center, count % 2)
56+
elif p in counter:
57+
count = min(counter[w], counter[p])
58+
res += count
59+
60+
return (res + center) * 2

0 commit comments

Comments
 (0)