id
int64 1
3.71k
| title
stringlengths 3
79
| difficulty
stringclasses 3
values | description
stringlengths 430
25.4k
| tags
stringlengths 0
131
| language
stringclasses 19
values | solution
stringlengths 47
20.6k
|
|---|---|---|---|---|---|---|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
C#
|
public class Solution {
public int MaxPotholes(string road, int budget) {
road += '.';
int n = road.Length;
int[] cnt = new int[n];
int k = 0;
foreach (char c in road) {
if (c == 'x') {
++k;
} else if (k > 0) {
++cnt[k];
k = 0;
}
}
int ans = 0;
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.Min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
}
|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
Go
|
func maxPotholes(road string, budget int) (ans int) {
road += "."
n := len(road)
cnt := make([]int, n)
k := 0
for _, c := range road {
if c == 'x' {
k++
} else if k > 0 {
cnt[k]++
k = 0
}
}
for k = n - 1; k > 0 && budget > 0; k-- {
t := min(budget/(k+1), cnt[k])
ans += t * k
budget -= t * (k + 1)
cnt[k-1] += cnt[k] - t
}
return
}
|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
Java
|
class Solution {
public int maxPotholes(String road, int budget) {
road += ".";
int n = road.length();
int[] cnt = new int[n];
int k = 0;
for (char c : road.toCharArray()) {
if (c == 'x') {
++k;
} else if (k > 0) {
++cnt[k];
k = 0;
}
}
int ans = 0;
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
}
|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
Python
|
class Solution:
def maxPotholes(self, road: str, budget: int) -> int:
road += "."
n = len(road)
cnt = [0] * n
k = 0
for c in road:
if c == "x":
k += 1
elif k:
cnt[k] += 1
k = 0
ans = 0
for k in range(n - 1, 0, -1):
if cnt[k] == 0:
continue
t = min(budget // (k + 1), cnt[k])
ans += t * k
budget -= t * (k + 1)
if budget == 0:
break
cnt[k - 1] += cnt[k] - t
return ans
|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
Rust
|
impl Solution {
pub fn max_potholes(road: String, budget: i32) -> i32 {
let mut cs: Vec<char> = road.chars().collect();
cs.push('.');
let n = cs.len();
let mut cnt: Vec<i32> = vec![0; n];
let mut k = 0;
for c in cs.iter() {
if *c == 'x' {
k += 1;
} else if k > 0 {
cnt[k] += 1;
k = 0;
}
}
let mut ans = 0;
let mut budget = budget;
for k in (1..n).rev() {
if budget == 0 {
break;
}
let t = std::cmp::min(budget / ((k as i32) + 1), cnt[k]);
ans += t * (k as i32);
budget -= t * ((k as i32) + 1);
cnt[k - 1] += cnt[k] - t;
}
ans
}
}
|
3,119
|
Maximum Number of Potholes That Can Be Fixed
|
Medium
|
<p>You are given a string <code>road</code>, consisting only of characters <code>"x"</code> and <code>"."</code>, where each <code>"x"</code> denotes a <em>pothole</em> and each <code>"."</code> denotes a smooth road, and an integer <code>budget</code>.</p>
<p>In one repair operation, you can repair <code>n</code> <strong>consecutive</strong> potholes for a price of <code>n + 1</code>.</p>
<p>Return the <strong>maximum</strong> number of potholes that can be fixed such that the sum of the prices of all of the fixes <strong>doesn't go over</strong> the given budget.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..", budget = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no potholes to be fixed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "..xxxxx", budget = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>We fix the first three potholes (they are consecutive). The budget needed for this task is <code>3 + 1 = 4</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">road = "x.x.xxx...x", budget = 14</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>We can fix all the potholes. The total cost would be <code>(1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10</code> which is within our budget of 14.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= road.length <= 10<sup>5</sup></code></li>
<li><code>1 <= budget <= 10<sup>5</sup> + 1</code></li>
<li><code>road</code> consists only of characters <code>'.'</code> and <code>'x'</code>.</li>
</ul>
|
Greedy; String; Sorting
|
TypeScript
|
function maxPotholes(road: string, budget: number): number {
road += '.';
const n = road.length;
const cnt: number[] = Array(n).fill(0);
let k = 0;
for (const c of road) {
if (c === 'x') {
++k;
} else if (k) {
++cnt[k];
k = 0;
}
}
let ans = 0;
for (k = n - 1; k && budget; --k) {
const t = Math.min(Math.floor(budget / (k + 1)), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
|
3,120
|
Count the Number of Special Characters I
|
Easy
|
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters in <code>word</code> are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No character in <code>word</code> appears in uppercase.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abBCab"</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The only special character in <code>word</code> is <code>'b'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 50</code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
C++
|
class Solution {
public:
int numberOfSpecialChars(string word) {
vector<bool> s('z' + 1);
for (char& c : word) {
s[c] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
ans += s['a' + i] && s['A' + i];
}
return ans;
}
};
|
3,120
|
Count the Number of Special Characters I
|
Easy
|
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters in <code>word</code> are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No character in <code>word</code> appears in uppercase.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abBCab"</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The only special character in <code>word</code> is <code>'b'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 50</code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Go
|
func numberOfSpecialChars(word string) (ans int) {
s := make([]bool, 'z'+1)
for _, c := range word {
s[c] = true
}
for i := 0; i < 26; i++ {
if s['a'+i] && s['A'+i] {
ans++
}
}
return
}
|
3,120
|
Count the Number of Special Characters I
|
Easy
|
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters in <code>word</code> are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No character in <code>word</code> appears in uppercase.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abBCab"</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The only special character in <code>word</code> is <code>'b'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 50</code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Java
|
class Solution {
public int numberOfSpecialChars(String word) {
boolean[] s = new boolean['z' + 1];
for (int i = 0; i < word.length(); ++i) {
s[word.charAt(i)] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (s['a' + i] && s['A' + i]) {
++ans;
}
}
return ans;
}
}
|
3,120
|
Count the Number of Special Characters I
|
Easy
|
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters in <code>word</code> are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No character in <code>word</code> appears in uppercase.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abBCab"</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The only special character in <code>word</code> is <code>'b'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 50</code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Python
|
class Solution:
def numberOfSpecialChars(self, word: str) -> int:
s = set(word)
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
|
3,120
|
Count the Number of Special Characters I
|
Easy
|
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters in <code>word</code> are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No character in <code>word</code> appears in uppercase.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abBCab"</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The only special character in <code>word</code> is <code>'b'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 50</code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
TypeScript
|
function numberOfSpecialChars(word: string): number {
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
for (let i = 0; i < word.length; ++i) {
s[word.charCodeAt(i)] = true;
}
let ans: number = 0;
for (let i = 0; i < 26; ++i) {
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
++ans;
}
}
return ans;
}
|
3,121
|
Count the Number of Special Characters II
|
Medium
|
<p>You are given a string <code>word</code>. A letter <code>c</code> is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>, and <strong>every</strong> lowercase occurrence of <code>c</code> appears before the <strong>first</strong> uppercase occurrence of <code>c</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters<em> </em>in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "AbBCab"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
C++
|
class Solution {
public:
int numberOfSpecialChars(string word) {
vector<int> first('z' + 1);
vector<int> last('z' + 1);
for (int i = 1; i <= word.size(); ++i) {
int j = word[i - 1];
if (first[j] == 0) {
first[j] = i;
}
last[j] = i;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (last['a' + i] && first['A' + i] && last['a' + i] < first['A' + i]) {
++ans;
}
}
return ans;
}
};
|
3,121
|
Count the Number of Special Characters II
|
Medium
|
<p>You are given a string <code>word</code>. A letter <code>c</code> is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>, and <strong>every</strong> lowercase occurrence of <code>c</code> appears before the <strong>first</strong> uppercase occurrence of <code>c</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters<em> </em>in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "AbBCab"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Go
|
func numberOfSpecialChars(word string) (ans int) {
first := make([]int, 'z'+1)
last := make([]int, 'z'+1)
for i, c := range word {
if first[c] == 0 {
first[c] = i + 1
}
last[c] = i + 1
}
for i := 0; i < 26; i++ {
if last['a'+i] > 0 && first['A'+i] > 0 && last['a'+i] < first['A'+i] {
ans++
}
}
return
}
|
3,121
|
Count the Number of Special Characters II
|
Medium
|
<p>You are given a string <code>word</code>. A letter <code>c</code> is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>, and <strong>every</strong> lowercase occurrence of <code>c</code> appears before the <strong>first</strong> uppercase occurrence of <code>c</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters<em> </em>in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "AbBCab"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Java
|
class Solution {
public int numberOfSpecialChars(String word) {
int[] first = new int['z' + 1];
int[] last = new int['z' + 1];
for (int i = 1; i <= word.length(); ++i) {
int j = word.charAt(i - 1);
if (first[j] == 0) {
first[j] = i;
}
last[j] = i;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (last['a' + i] > 0 && first['A' + i] > 0 && last['a' + i] < first['A' + i]) {
++ans;
}
}
return ans;
}
}
|
3,121
|
Count the Number of Special Characters II
|
Medium
|
<p>You are given a string <code>word</code>. A letter <code>c</code> is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>, and <strong>every</strong> lowercase occurrence of <code>c</code> appears before the <strong>first</strong> uppercase occurrence of <code>c</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters<em> </em>in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "AbBCab"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
Python
|
class Solution:
def numberOfSpecialChars(self, word: str) -> int:
first, last = {}, {}
for i, c in enumerate(word):
if c not in first:
first[c] = i
last[c] = i
return sum(
a in last and b in first and last[a] < first[b]
for a, b in zip(ascii_lowercase, ascii_uppercase)
)
|
3,121
|
Count the Number of Special Characters II
|
Medium
|
<p>You are given a string <code>word</code>. A letter <code>c</code> is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>, and <strong>every</strong> lowercase occurrence of <code>c</code> appears before the <strong>first</strong> uppercase occurrence of <code>c</code>.</p>
<p>Return the number of<em> </em><strong>special</strong> letters<em> </em>in<em> </em><code>word</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "aaAbcBC"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The special characters are <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "AbBCab"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no special characters in <code>word</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li>
<li><code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>
|
Hash Table; String
|
TypeScript
|
function numberOfSpecialChars(word: string): number {
const first: number[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => 0);
const last: number[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => 0);
for (let i = 0; i < word.length; ++i) {
const j = word.charCodeAt(i);
if (first[j] === 0) {
first[j] = i + 1;
}
last[j] = i + 1;
}
let ans: number = 0;
for (let i = 0; i < 26; ++i) {
if (
last['a'.charCodeAt(0) + i] &&
first['A'.charCodeAt(0) + i] &&
last['a'.charCodeAt(0) + i] < first['A'.charCodeAt(0) + i]
) {
++ans;
}
}
return ans;
}
|
3,122
|
Minimum Number of Operations to Satisfy Conditions
|
Medium
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. In one <strong>operation</strong>, you can change the value of <strong>any</strong> cell to <strong>any</strong> non-negative number. You need to perform some <strong>operations</strong> such that each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations needed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the matrix already satisfy the properties.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>The matrix becomes <code>[[1,0,1],[1,0,1]]</code> which satisfies the properties, by doing these 3 operations:</p>
<ul>
<li>Change <code>grid[1][0]</code> to 1.</li>
<li>Change <code>grid[0][1]</code> to 0.</li>
<li>Change <code>grid[1][2]</code> to 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>There is a single column. We can change the value to 1 in each cell using 2 operations.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Dynamic Programming; Matrix
|
C++
|
class Solution {
public:
int minimumOperations(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
int f[n][10];
memset(f, 0x3f, sizeof(f));
for (int i = 0; i < n; ++i) {
int cnt[10]{};
for (int j = 0; j < m; ++j) {
++cnt[grid[j][i]];
}
if (i == 0) {
for (int j = 0; j < 10; ++j) {
f[i][j] = m - cnt[j];
}
} else {
for (int j = 0; j < 10; ++j) {
for (int k = 0; k < 10; ++k) {
if (k != j) {
f[i][j] = min(f[i][j], f[i - 1][k] + m - cnt[j]);
}
}
}
}
}
return *min_element(f[n - 1], f[n - 1] + 10);
}
};
|
3,122
|
Minimum Number of Operations to Satisfy Conditions
|
Medium
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. In one <strong>operation</strong>, you can change the value of <strong>any</strong> cell to <strong>any</strong> non-negative number. You need to perform some <strong>operations</strong> such that each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations needed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the matrix already satisfy the properties.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>The matrix becomes <code>[[1,0,1],[1,0,1]]</code> which satisfies the properties, by doing these 3 operations:</p>
<ul>
<li>Change <code>grid[1][0]</code> to 1.</li>
<li>Change <code>grid[0][1]</code> to 0.</li>
<li>Change <code>grid[1][2]</code> to 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>There is a single column. We can change the value to 1 in each cell using 2 operations.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Dynamic Programming; Matrix
|
Go
|
func minimumOperations(grid [][]int) int {
m, n := len(grid), len(grid[0])
f := make([][]int, n)
for i := range f {
f[i] = make([]int, 10)
for j := range f[i] {
f[i][j] = 1 << 29
}
}
for i := 0; i < n; i++ {
cnt := [10]int{}
for j := 0; j < m; j++ {
cnt[grid[j][i]]++
}
if i == 0 {
for j := 0; j < 10; j++ {
f[i][j] = m - cnt[j]
}
} else {
for j := 0; j < 10; j++ {
for k := 0; k < 10; k++ {
if j != k {
f[i][j] = min(f[i][j], f[i-1][k]+m-cnt[j])
}
}
}
}
}
return slices.Min(f[n-1])
}
|
3,122
|
Minimum Number of Operations to Satisfy Conditions
|
Medium
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. In one <strong>operation</strong>, you can change the value of <strong>any</strong> cell to <strong>any</strong> non-negative number. You need to perform some <strong>operations</strong> such that each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations needed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the matrix already satisfy the properties.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>The matrix becomes <code>[[1,0,1],[1,0,1]]</code> which satisfies the properties, by doing these 3 operations:</p>
<ul>
<li>Change <code>grid[1][0]</code> to 1.</li>
<li>Change <code>grid[0][1]</code> to 0.</li>
<li>Change <code>grid[1][2]</code> to 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>There is a single column. We can change the value to 1 in each cell using 2 operations.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Dynamic Programming; Matrix
|
Java
|
class Solution {
public int minimumOperations(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] f = new int[n][10];
final int inf = 1 << 29;
for (var g : f) {
Arrays.fill(g, inf);
}
for (int i = 0; i < n; ++i) {
int[] cnt = new int[10];
for (int j = 0; j < m; ++j) {
++cnt[grid[j][i]];
}
if (i == 0) {
for (int j = 0; j < 10; ++j) {
f[i][j] = m - cnt[j];
}
} else {
for (int j = 0; j < 10; ++j) {
for (int k = 0; k < 10; ++k) {
if (k != j) {
f[i][j] = Math.min(f[i][j], f[i - 1][k] + m - cnt[j]);
}
}
}
}
}
int ans = inf;
for (int j = 0; j < 10; ++j) {
ans = Math.min(ans, f[n - 1][j]);
}
return ans;
}
}
|
3,122
|
Minimum Number of Operations to Satisfy Conditions
|
Medium
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. In one <strong>operation</strong>, you can change the value of <strong>any</strong> cell to <strong>any</strong> non-negative number. You need to perform some <strong>operations</strong> such that each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations needed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the matrix already satisfy the properties.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>The matrix becomes <code>[[1,0,1],[1,0,1]]</code> which satisfies the properties, by doing these 3 operations:</p>
<ul>
<li>Change <code>grid[1][0]</code> to 1.</li>
<li>Change <code>grid[0][1]</code> to 0.</li>
<li>Change <code>grid[1][2]</code> to 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>There is a single column. We can change the value to 1 in each cell using 2 operations.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Dynamic Programming; Matrix
|
Python
|
class Solution:
def minimumOperations(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
f = [[inf] * 10 for _ in range(n)]
for i in range(n):
cnt = [0] * 10
for j in range(m):
cnt[grid[j][i]] += 1
if i == 0:
for j in range(10):
f[i][j] = m - cnt[j]
else:
for j in range(10):
for k in range(10):
if k != j:
f[i][j] = min(f[i][j], f[i - 1][k] + m - cnt[j])
return min(f[-1])
|
3,122
|
Minimum Number of Operations to Satisfy Conditions
|
Medium
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. In one <strong>operation</strong>, you can change the value of <strong>any</strong> cell to <strong>any</strong> non-negative number. You need to perform some <strong>operations</strong> such that each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations needed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the matrix already satisfy the properties.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>The matrix becomes <code>[[1,0,1],[1,0,1]]</code> which satisfies the properties, by doing these 3 operations:</p>
<ul>
<li>Change <code>grid[1][0]</code> to 1.</li>
<li>Change <code>grid[0][1]</code> to 0.</li>
<li>Change <code>grid[1][2]</code> to 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3122.Minimum%20Number%20of%20Operations%20to%20Satisfy%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>There is a single column. We can change the value to 1 in each cell using 2 operations.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Dynamic Programming; Matrix
|
TypeScript
|
function minimumOperations(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const f: number[][] = Array.from({ length: n }, () =>
Array.from({ length: 10 }, () => Infinity),
);
for (let i = 0; i < n; ++i) {
const cnt: number[] = Array(10).fill(0);
for (let j = 0; j < m; ++j) {
cnt[grid[j][i]]++;
}
if (i === 0) {
for (let j = 0; j < 10; ++j) {
f[i][j] = m - cnt[j];
}
} else {
for (let j = 0; j < 10; ++j) {
for (let k = 0; k < 10; ++k) {
if (j !== k) {
f[i][j] = Math.min(f[i][j], f[i - 1][k] + m - cnt[j]);
}
}
}
}
}
return Math.min(...f[n - 1]);
}
|
3,123
|
Find Edges in Shortest Paths
|
Hard
|
<p>You are given an undirected weighted graph of <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The graph consists of <code>m</code> edges represented by a 2D array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>Consider all the shortest paths from node 0 to node <code>n - 1</code> in the graph. You need to find a <strong>boolean</strong> array <code>answer</code> where <code>answer[i]</code> is <code>true</code> if the edge <code>edges[i]</code> is part of <strong>at least</strong> one shortest path. Otherwise, <code>answer[i]</code> is <code>false</code>.</p>
<p>Return the array <code>answer</code>.</p>
<p><strong>Note</strong> that the graph may not be connected.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graph35drawio-1.png" style="height: 129px; width: 250px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,true,true,false,true,true,true,false]</span></p>
<p><strong>Explanation:</strong></p>
<p>The following are <strong>all</strong> the shortest paths between nodes 0 and 5:</p>
<ul>
<li>The path <code>0 -> 1 -> 5</code>: The sum of weights is <code>4 + 1 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 5</code>: The sum of weights is <code>1 + 1 + 3 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 1 -> 5</code>: The sum of weights is <code>1 + 1 + 2 + 1 = 5</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graphhhh.png" style="width: 185px; height: 136px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[2,0,1],[0,1,1],[0,3,4],[3,2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,false,false,true]</span></p>
<p><strong>Explanation:</strong></p>
<p>There is one shortest path between nodes 0 and 3, which is the path <code>0 -> 2 -> 3</code> with the sum of weights <code>1 + 2 = 3</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>m == edges.length</code></li>
<li><code>1 <= m <= min(5 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>1 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
<li>There are no repeated edges.</li>
</ul>
|
Depth-First Search; Breadth-First Search; Graph; Shortest Path; Heap (Priority Queue)
|
C++
|
class Solution {
public:
vector<bool> findAnswer(int n, vector<vector<int>>& edges) {
vector<vector<array<int, 3>>> g(n);
int m = edges.size();
for (int i = 0; i < m; ++i) {
auto e = edges[i];
int a = e[0], b = e[1], w = e[2];
g[a].push_back({b, w, i});
g[b].push_back({a, w, i});
}
const int inf = 1 << 30;
vector<int> dist(n, inf);
dist[0] = 0;
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, 0});
while (!pq.empty()) {
auto [da, a] = pq.top();
pq.pop();
if (da > dist[a]) {
continue;
}
for (auto [b, w, _] : g[a]) {
if (dist[b] > dist[a] + w) {
dist[b] = dist[a] + w;
pq.push({dist[b], b});
}
}
}
vector<bool> ans(m);
if (dist[n - 1] == inf) {
return ans;
}
queue<int> q{{n - 1}};
while (!q.empty()) {
int a = q.front();
q.pop();
for (auto [b, w, i] : g[a]) {
if (dist[a] == dist[b] + w) {
ans[i] = true;
q.push(b);
}
}
}
return ans;
}
};
|
3,123
|
Find Edges in Shortest Paths
|
Hard
|
<p>You are given an undirected weighted graph of <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The graph consists of <code>m</code> edges represented by a 2D array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>Consider all the shortest paths from node 0 to node <code>n - 1</code> in the graph. You need to find a <strong>boolean</strong> array <code>answer</code> where <code>answer[i]</code> is <code>true</code> if the edge <code>edges[i]</code> is part of <strong>at least</strong> one shortest path. Otherwise, <code>answer[i]</code> is <code>false</code>.</p>
<p>Return the array <code>answer</code>.</p>
<p><strong>Note</strong> that the graph may not be connected.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graph35drawio-1.png" style="height: 129px; width: 250px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,true,true,false,true,true,true,false]</span></p>
<p><strong>Explanation:</strong></p>
<p>The following are <strong>all</strong> the shortest paths between nodes 0 and 5:</p>
<ul>
<li>The path <code>0 -> 1 -> 5</code>: The sum of weights is <code>4 + 1 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 5</code>: The sum of weights is <code>1 + 1 + 3 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 1 -> 5</code>: The sum of weights is <code>1 + 1 + 2 + 1 = 5</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graphhhh.png" style="width: 185px; height: 136px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[2,0,1],[0,1,1],[0,3,4],[3,2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,false,false,true]</span></p>
<p><strong>Explanation:</strong></p>
<p>There is one shortest path between nodes 0 and 3, which is the path <code>0 -> 2 -> 3</code> with the sum of weights <code>1 + 2 = 3</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>m == edges.length</code></li>
<li><code>1 <= m <= min(5 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>1 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
<li>There are no repeated edges.</li>
</ul>
|
Depth-First Search; Breadth-First Search; Graph; Shortest Path; Heap (Priority Queue)
|
Go
|
func findAnswer(n int, edges [][]int) []bool {
g := make([][][3]int, n)
for i, e := range edges {
a, b, w := e[0], e[1], e[2]
g[a] = append(g[a], [3]int{b, w, i})
g[b] = append(g[b], [3]int{a, w, i})
}
dist := make([]int, n)
const inf int = 1 << 30
for i := range dist {
dist[i] = inf
}
dist[0] = 0
pq := hp{{0, 0}}
for len(pq) > 0 {
p := heap.Pop(&pq).(pair)
da, a := p.dis, p.u
if da > dist[a] {
continue
}
for _, e := range g[a] {
b, w := e[0], e[1]
if dist[b] > dist[a]+w {
dist[b] = dist[a] + w
heap.Push(&pq, pair{dist[b], b})
}
}
}
ans := make([]bool, len(edges))
if dist[n-1] == inf {
return ans
}
q := []int{n - 1}
for len(q) > 0 {
a := q[0]
q = q[1:]
for _, e := range g[a] {
b, w, i := e[0], e[1], e[2]
if dist[a] == dist[b]+w {
ans[i] = true
q = append(q, b)
}
}
}
return ans
}
type pair struct{ dis, u int }
type hp []pair
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].dis < h[j].dis }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
|
3,123
|
Find Edges in Shortest Paths
|
Hard
|
<p>You are given an undirected weighted graph of <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The graph consists of <code>m</code> edges represented by a 2D array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>Consider all the shortest paths from node 0 to node <code>n - 1</code> in the graph. You need to find a <strong>boolean</strong> array <code>answer</code> where <code>answer[i]</code> is <code>true</code> if the edge <code>edges[i]</code> is part of <strong>at least</strong> one shortest path. Otherwise, <code>answer[i]</code> is <code>false</code>.</p>
<p>Return the array <code>answer</code>.</p>
<p><strong>Note</strong> that the graph may not be connected.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graph35drawio-1.png" style="height: 129px; width: 250px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,true,true,false,true,true,true,false]</span></p>
<p><strong>Explanation:</strong></p>
<p>The following are <strong>all</strong> the shortest paths between nodes 0 and 5:</p>
<ul>
<li>The path <code>0 -> 1 -> 5</code>: The sum of weights is <code>4 + 1 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 5</code>: The sum of weights is <code>1 + 1 + 3 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 1 -> 5</code>: The sum of weights is <code>1 + 1 + 2 + 1 = 5</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graphhhh.png" style="width: 185px; height: 136px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[2,0,1],[0,1,1],[0,3,4],[3,2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,false,false,true]</span></p>
<p><strong>Explanation:</strong></p>
<p>There is one shortest path between nodes 0 and 3, which is the path <code>0 -> 2 -> 3</code> with the sum of weights <code>1 + 2 = 3</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>m == edges.length</code></li>
<li><code>1 <= m <= min(5 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>1 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
<li>There are no repeated edges.</li>
</ul>
|
Depth-First Search; Breadth-First Search; Graph; Shortest Path; Heap (Priority Queue)
|
Java
|
class Solution {
public boolean[] findAnswer(int n, int[][] edges) {
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int m = edges.length;
for (int i = 0; i < m; ++i) {
int a = edges[i][0], b = edges[i][1], w = edges[i][2];
g[a].add(new int[] {b, w, i});
g[b].add(new int[] {a, w, i});
}
int[] dist = new int[n];
final int inf = 1 << 30;
Arrays.fill(dist, inf);
dist[0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
pq.offer(new int[] {0, 0});
while (!pq.isEmpty()) {
var p = pq.poll();
int da = p[0], a = p[1];
if (da > dist[a]) {
continue;
}
for (var e : g[a]) {
int b = e[0], w = e[1];
if (dist[b] > dist[a] + w) {
dist[b] = dist[a] + w;
pq.offer(new int[] {dist[b], b});
}
}
}
boolean[] ans = new boolean[m];
if (dist[n - 1] == inf) {
return ans;
}
Deque<Integer> q = new ArrayDeque<>();
q.offer(n - 1);
while (!q.isEmpty()) {
int a = q.poll();
for (var e : g[a]) {
int b = e[0], w = e[1], i = e[2];
if (dist[a] == dist[b] + w) {
ans[i] = true;
q.offer(b);
}
}
}
return ans;
}
}
|
3,123
|
Find Edges in Shortest Paths
|
Hard
|
<p>You are given an undirected weighted graph of <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The graph consists of <code>m</code> edges represented by a 2D array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>Consider all the shortest paths from node 0 to node <code>n - 1</code> in the graph. You need to find a <strong>boolean</strong> array <code>answer</code> where <code>answer[i]</code> is <code>true</code> if the edge <code>edges[i]</code> is part of <strong>at least</strong> one shortest path. Otherwise, <code>answer[i]</code> is <code>false</code>.</p>
<p>Return the array <code>answer</code>.</p>
<p><strong>Note</strong> that the graph may not be connected.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graph35drawio-1.png" style="height: 129px; width: 250px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,true,true,false,true,true,true,false]</span></p>
<p><strong>Explanation:</strong></p>
<p>The following are <strong>all</strong> the shortest paths between nodes 0 and 5:</p>
<ul>
<li>The path <code>0 -> 1 -> 5</code>: The sum of weights is <code>4 + 1 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 5</code>: The sum of weights is <code>1 + 1 + 3 = 5</code>.</li>
<li>The path <code>0 -> 2 -> 3 -> 1 -> 5</code>: The sum of weights is <code>1 + 1 + 2 + 1 = 5</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3123.Find%20Edges%20in%20Shortest%20Paths/images/graphhhh.png" style="width: 185px; height: 136px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[2,0,1],[0,1,1],[0,3,4],[3,2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,false,false,true]</span></p>
<p><strong>Explanation:</strong></p>
<p>There is one shortest path between nodes 0 and 3, which is the path <code>0 -> 2 -> 3</code> with the sum of weights <code>1 + 2 = 3</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>m == edges.length</code></li>
<li><code>1 <= m <= min(5 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>1 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
<li>There are no repeated edges.</li>
</ul>
|
Depth-First Search; Breadth-First Search; Graph; Shortest Path; Heap (Priority Queue)
|
Python
|
class Solution:
def findAnswer(self, n: int, edges: List[List[int]]) -> List[bool]:
g = defaultdict(list)
for i, (a, b, w) in enumerate(edges):
g[a].append((b, w, i))
g[b].append((a, w, i))
dist = [inf] * n
dist[0] = 0
q = [(0, 0)]
while q:
da, a = heappop(q)
if da > dist[a]:
continue
for b, w, _ in g[a]:
if dist[b] > dist[a] + w:
dist[b] = dist[a] + w
heappush(q, (dist[b], b))
m = len(edges)
ans = [False] * m
if dist[n - 1] == inf:
return ans
q = deque([n - 1])
while q:
a = q.popleft()
for b, w, i in g[a]:
if dist[a] == dist[b] + w:
ans[i] = True
q.append(b)
return ans
|
3,124
|
Find Longest Calls
|
Medium
|
<p>Table: <code>Contacts</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| first_name | varchar |
| last_name | varchar |
+-------------+---------+
id is the primary key (column with unique values) of this table.
id is a foreign key (reference column) to <code>Calls</code> table.
Each row of this table contains id, first_name, and last_name.
</pre>
<p>Table: <code>Calls</code></p>
<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| contact_id | int |
| type | enum |
| duration | int |
+-------------+------+
(contact_id, type, duration) is the primary key (column with unique values) of this table.
type is an ENUM (category) type of ('incoming', 'outgoing').
Each row of this table contains information about calls, comprising of contact_id, type, and duration in seconds.
</pre>
<p>Write a solution to find the <b>three longest </b><strong>incoming</strong> and <strong>outgoing</strong> calls.</p>
<p>Return t<em>he result table ordered by</em> <code>type</code>, <code>duration</code>, and<code> first_name</code> <em>in <strong>descending </strong>order and <code>duration</code> must be formatted as <strong>HH:MM:SS</strong>.</em></p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>Contacts table:</p>
<pre class="example-io">
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | John | Doe |
| 2 | Jane | Smith |
| 3 | Alice | Johnson |
| 4 | Michael | Brown |
| 5 | Emily | Davis |
+----+------------+-----------+
</pre>
<p>Calls table:</p>
<pre class="example-io">
+------------+----------+----------+
| contact_id | type | duration |
+------------+----------+----------+
| 1 | incoming | 120 |
| 1 | outgoing | 180 |
| 2 | incoming | 300 |
| 2 | outgoing | 240 |
| 3 | incoming | 150 |
| 3 | outgoing | 360 |
| 4 | incoming | 420 |
| 4 | outgoing | 200 |
| 5 | incoming | 180 |
| 5 | outgoing | 280 |
+------------+----------+----------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-----------+----------+-------------------+
| first_name| type | duration_formatted|
+-----------+----------+-------------------+
| Alice | outgoing | 00:06:00 |
| Emily | outgoing | 00:04:40 |
| Jane | outgoing | 00:04:00 |
| Michael | incoming | 00:07:00 |
| Jane | incoming | 00:05:00 |
| Emily | incoming | 00:03:00 |
+-----------+----------+-------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Alice had an outgoing call lasting 6 minutes.</li>
<li>Emily had an outgoing call lasting 4 minutes and 40 seconds.</li>
<li>Jane had an outgoing call lasting 4 minutes.</li>
<li>Michael had an incoming call lasting 7 minutes.</li>
<li>Jane had an incoming call lasting 5 minutes.</li>
<li>Emily had an incoming call lasting 3 minutes.</li>
</ul>
<p><b>Note:</b> Output table is sorted by type, duration, and first_name in descending order.</p>
</div>
|
Database
|
Python
|
import pandas as pd
def find_longest_calls(contacts: pd.DataFrame, calls: pd.DataFrame) -> pd.DataFrame:
merged_data = calls.merge(contacts, left_on="contact_id", right_on="id")
merged_data["duration_formatted"] = (
merged_data["duration"] // 3600 * 10000
+ merged_data["duration"] % 3600 // 60 * 100
+ merged_data["duration"] % 60
).apply(lambda x: "{:02}:{:02}:{:02}".format(x // 10000, x // 100 % 100, x % 100))
merged_data["rk"] = merged_data.groupby("type")["duration"].rank(
method="dense", ascending=False
)
result = merged_data[merged_data["rk"] <= 3][
["first_name", "type", "duration_formatted"]
]
result = result.sort_values(
by=["type", "duration_formatted", "first_name"], ascending=[True, False, False]
)
return result
|
3,124
|
Find Longest Calls
|
Medium
|
<p>Table: <code>Contacts</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| first_name | varchar |
| last_name | varchar |
+-------------+---------+
id is the primary key (column with unique values) of this table.
id is a foreign key (reference column) to <code>Calls</code> table.
Each row of this table contains id, first_name, and last_name.
</pre>
<p>Table: <code>Calls</code></p>
<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| contact_id | int |
| type | enum |
| duration | int |
+-------------+------+
(contact_id, type, duration) is the primary key (column with unique values) of this table.
type is an ENUM (category) type of ('incoming', 'outgoing').
Each row of this table contains information about calls, comprising of contact_id, type, and duration in seconds.
</pre>
<p>Write a solution to find the <b>three longest </b><strong>incoming</strong> and <strong>outgoing</strong> calls.</p>
<p>Return t<em>he result table ordered by</em> <code>type</code>, <code>duration</code>, and<code> first_name</code> <em>in <strong>descending </strong>order and <code>duration</code> must be formatted as <strong>HH:MM:SS</strong>.</em></p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>Contacts table:</p>
<pre class="example-io">
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | John | Doe |
| 2 | Jane | Smith |
| 3 | Alice | Johnson |
| 4 | Michael | Brown |
| 5 | Emily | Davis |
+----+------------+-----------+
</pre>
<p>Calls table:</p>
<pre class="example-io">
+------------+----------+----------+
| contact_id | type | duration |
+------------+----------+----------+
| 1 | incoming | 120 |
| 1 | outgoing | 180 |
| 2 | incoming | 300 |
| 2 | outgoing | 240 |
| 3 | incoming | 150 |
| 3 | outgoing | 360 |
| 4 | incoming | 420 |
| 4 | outgoing | 200 |
| 5 | incoming | 180 |
| 5 | outgoing | 280 |
+------------+----------+----------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-----------+----------+-------------------+
| first_name| type | duration_formatted|
+-----------+----------+-------------------+
| Alice | outgoing | 00:06:00 |
| Emily | outgoing | 00:04:40 |
| Jane | outgoing | 00:04:00 |
| Michael | incoming | 00:07:00 |
| Jane | incoming | 00:05:00 |
| Emily | incoming | 00:03:00 |
+-----------+----------+-------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Alice had an outgoing call lasting 6 minutes.</li>
<li>Emily had an outgoing call lasting 4 minutes and 40 seconds.</li>
<li>Jane had an outgoing call lasting 4 minutes.</li>
<li>Michael had an incoming call lasting 7 minutes.</li>
<li>Jane had an incoming call lasting 5 minutes.</li>
<li>Emily had an incoming call lasting 3 minutes.</li>
</ul>
<p><b>Note:</b> Output table is sorted by type, duration, and first_name in descending order.</p>
</div>
|
Database
|
SQL
|
WITH
T AS (
SELECT
first_name,
type,
DATE_FORMAT(SEC_TO_TIME(duration), "%H:%i:%s") AS duration_formatted,
RANK() OVER (
PARTITION BY type
ORDER BY duration DESC
) AS rk
FROM
Calls AS c1
JOIN Contacts AS c2 ON c1.contact_id = c2.id
)
SELECT
first_name,
type,
duration_formatted
FROM T
WHERE rk <= 3
ORDER BY 2, 3 DESC, 1 DESC;
|
3,125
|
Maximum Number That Makes Result of Bitwise AND Zero
|
Medium
|
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x <= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>15</sup></code></li>
</ul>
|
Greedy; String; Sorting
|
C++
|
class Solution {
public:
long long maxNumber(long long n) {
return (1LL << (63 - __builtin_clzll(n))) - 1;
}
};
|
3,125
|
Maximum Number That Makes Result of Bitwise AND Zero
|
Medium
|
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x <= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>15</sup></code></li>
</ul>
|
Greedy; String; Sorting
|
Go
|
func maxNumber(n int64) int64 {
return int64(1<<(bits.Len64(uint64(n))-1)) - 1
}
|
3,125
|
Maximum Number That Makes Result of Bitwise AND Zero
|
Medium
|
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x <= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>15</sup></code></li>
</ul>
|
Greedy; String; Sorting
|
Java
|
class Solution {
public long maxNumber(long n) {
return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1;
}
}
|
3,125
|
Maximum Number That Makes Result of Bitwise AND Zero
|
Medium
|
Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x <= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br />
The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br />
The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 17</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>15</sup></code></li>
</ul>
|
Greedy; String; Sorting
|
Python
|
class Solution:
def maxNumber(self, n: int) -> int:
return (1 << (n.bit_length() - 1)) - 1
|
3,126
|
Server Utilization Time
|
Medium
|
<p>Table: <code>Servers</code></p>
<pre>
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| server_id | int |
| status_time | datetime |
| session_status | enum |
+----------------+----------+
(server_id, status_time, session_status) is the primary key (combination of columns with unique values) for this table.
session_status is an ENUM (category) type of ('start', 'stop').
Each row of this table contains server_id, status_time, and session_status.
</pre>
<p>Write a solution to find the <strong>total time</strong> when servers were <strong>running</strong>. The output should be rounded down to the nearest number of <strong>full days</strong>.</p>
<p>Return <em>the result table in <strong>any</strong></em><em> order.</em></p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>Servers table:</p>
<pre class="example-io">
+-----------+---------------------+----------------+
| server_id | status_time | session_status |
+-----------+---------------------+----------------+
| 3 | 2023-11-04 16:29:47 | start |
| 3 | 2023-11-05 01:49:47 | stop |
| 3 | 2023-11-25 01:37:08 | start |
| 3 | 2023-11-25 03:50:08 | stop |
| 1 | 2023-11-13 03:05:31 | start |
| 1 | 2023-11-13 11:10:31 | stop |
| 4 | 2023-11-29 15:11:17 | start |
| 4 | 2023-11-29 15:42:17 | stop |
| 4 | 2023-11-20 00:31:44 | start |
| 4 | 2023-11-20 07:03:44 | stop |
| 1 | 2023-11-20 00:27:11 | start |
| 1 | 2023-11-20 01:41:11 | stop |
| 3 | 2023-11-04 23:16:48 | start |
| 3 | 2023-11-05 01:15:48 | stop |
| 4 | 2023-11-30 15:09:18 | start |
| 4 | 2023-11-30 20:48:18 | stop |
| 4 | 2023-11-25 21:09:06 | start |
| 4 | 2023-11-26 04:58:06 | stop |
| 5 | 2023-11-16 19:42:22 | start |
| 5 | 2023-11-16 21:08:22 | stop |
+-----------+---------------------+----------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------------+
| total_uptime_days |
+-------------------+
| 1 |
+-------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>For server ID 3:
<ul>
<li>From 2023-11-04 16:29:47 to 2023-11-05 01:49:47: ~9.3 hours</li>
<li>From 2023-11-25 01:37:08 to 2023-11-25 03:50:08: ~2.2 hours</li>
<li>From 2023-11-04 23:16:48 to 2023-11-05 01:15:48: ~1.98 hours</li>
</ul>
Total for server 3: ~13.48 hours</li>
<li>For server ID 1:
<ul>
<li>From 2023-11-13 03:05:31 to 2023-11-13 11:10:31: ~8 hours</li>
<li>From 2023-11-20 00:27:11 to 2023-11-20 01:41:11: ~1.23 hours</li>
</ul>
Total for server 1: ~9.23 hours</li>
<li>For server ID 4:
<ul>
<li>From 2023-11-29 15:11:17 to 2023-11-29 15:42:17: ~0.52 hours</li>
<li>From 2023-11-20 00:31:44 to 2023-11-20 07:03:44: ~6.53 hours</li>
<li>From 2023-11-30 15:09:18 to 2023-11-30 20:48:18: ~5.65 hours</li>
<li>From 2023-11-25 21:09:06 to 2023-11-26 04:58:06: ~7.82 hours</li>
</ul>
Total for server 4: ~20.52 hours</li>
<li>For server ID 5:
<ul>
<li>From 2023-11-16 19:42:22 to 2023-11-16 21:08:22: ~1.43 hours</li>
</ul>
Total for server 5: ~1.43 hours</li>
</ul>
The accumulated runtime for all servers totals approximately 44.46 hours, equivalent to one full day plus some additional hours. However, since we consider only full days, the final output is rounded to 1 full day.</div>
|
Database
|
SQL
|
# Write your MySQL query statement below
WITH
T AS (
SELECT
session_status,
status_time,
LEAD(status_time) OVER (
PARTITION BY server_id
ORDER BY status_time
) AS next_status_time
FROM Servers
)
SELECT FLOOR(SUM(TIMESTAMPDIFF(SECOND, status_time, next_status_time)) / 86400) AS total_uptime_days
FROM T
WHERE session_status = 'start';
|
3,127
|
Make a Square with the Same Color
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->.</p>
<p>Your task is to change the color of <strong>at most one</strong> cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --></p>
<p>Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>.</p>
<p> </p>
<style type="text/css">.grid-container {
display: grid;
grid-template-columns: 30px 30px 30px;
padding: 10px;
}
.grid-item {
background-color: black;
border: 1px solid gray;
height: 30px;
font-size: 30px;
text-align: center;
}
.grid-item-white {
background-color: white;
}
</style>
<style class="darkreader darkreader--sync" media="screen" type="text/css">
</style>
<p><strong class="example">Example 1:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>It can be done by changing the color of the <code>grid[0][2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>It cannot be done by changing at most one cell.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 3</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
</ul>
|
Array; Enumeration; Matrix
|
C++
|
class Solution {
public:
bool canMakeSquare(vector<vector<char>>& grid) {
int dirs[5] = {0, 0, 1, 1, 0};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
int cnt1 = 0, cnt2 = 0;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
cnt1 += grid[x][y] == 'W';
cnt2 += grid[x][y] == 'B';
}
if (cnt1 != cnt2) {
return true;
}
}
}
return false;
}
};
|
3,127
|
Make a Square with the Same Color
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->.</p>
<p>Your task is to change the color of <strong>at most one</strong> cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --></p>
<p>Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>.</p>
<p> </p>
<style type="text/css">.grid-container {
display: grid;
grid-template-columns: 30px 30px 30px;
padding: 10px;
}
.grid-item {
background-color: black;
border: 1px solid gray;
height: 30px;
font-size: 30px;
text-align: center;
}
.grid-item-white {
background-color: white;
}
</style>
<style class="darkreader darkreader--sync" media="screen" type="text/css">
</style>
<p><strong class="example">Example 1:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>It can be done by changing the color of the <code>grid[0][2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>It cannot be done by changing at most one cell.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 3</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
</ul>
|
Array; Enumeration; Matrix
|
Go
|
func canMakeSquare(grid [][]byte) bool {
dirs := [5]int{0, 0, 1, 1, 0}
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
cnt1, cnt2 := 0, 0
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if grid[x][y] == 'W' {
cnt1++
} else {
cnt2++
}
}
if cnt1 != cnt2 {
return true
}
}
}
return false
}
|
3,127
|
Make a Square with the Same Color
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->.</p>
<p>Your task is to change the color of <strong>at most one</strong> cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --></p>
<p>Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>.</p>
<p> </p>
<style type="text/css">.grid-container {
display: grid;
grid-template-columns: 30px 30px 30px;
padding: 10px;
}
.grid-item {
background-color: black;
border: 1px solid gray;
height: 30px;
font-size: 30px;
text-align: center;
}
.grid-item-white {
background-color: white;
}
</style>
<style class="darkreader darkreader--sync" media="screen" type="text/css">
</style>
<p><strong class="example">Example 1:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>It can be done by changing the color of the <code>grid[0][2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>It cannot be done by changing at most one cell.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 3</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
</ul>
|
Array; Enumeration; Matrix
|
Java
|
class Solution {
public boolean canMakeSquare(char[][] grid) {
final int[] dirs = {0, 0, 1, 1, 0};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
int cnt1 = 0, cnt2 = 0;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
cnt1 += grid[x][y] == 'W' ? 1 : 0;
cnt2 += grid[x][y] == 'B' ? 1 : 0;
}
if (cnt1 != cnt2) {
return true;
}
}
}
return false;
}
}
|
3,127
|
Make a Square with the Same Color
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->.</p>
<p>Your task is to change the color of <strong>at most one</strong> cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --></p>
<p>Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>.</p>
<p> </p>
<style type="text/css">.grid-container {
display: grid;
grid-template-columns: 30px 30px 30px;
padding: 10px;
}
.grid-item {
background-color: black;
border: 1px solid gray;
height: 30px;
font-size: 30px;
text-align: center;
}
.grid-item-white {
background-color: white;
}
</style>
<style class="darkreader darkreader--sync" media="screen" type="text/css">
</style>
<p><strong class="example">Example 1:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>It can be done by changing the color of the <code>grid[0][2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>It cannot be done by changing at most one cell.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 3</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
</ul>
|
Array; Enumeration; Matrix
|
Python
|
class Solution:
def canMakeSquare(self, grid: List[List[str]]) -> bool:
for i in range(0, 2):
for j in range(0, 2):
cnt1 = cnt2 = 0
for a, b in pairwise((0, 0, 1, 1, 0)):
x, y = i + a, j + b
cnt1 += grid[x][y] == "W"
cnt2 += grid[x][y] == "B"
if cnt1 != cnt2:
return True
return False
|
3,127
|
Make a Square with the Same Color
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->.</p>
<p>Your task is to change the color of <strong>at most one</strong> cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --></p>
<p>Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>.</p>
<p> </p>
<style type="text/css">.grid-container {
display: grid;
grid-template-columns: 30px 30px 30px;
padding: 10px;
}
.grid-item {
background-color: black;
border: 1px solid gray;
height: 30px;
font-size: 30px;
text-align: center;
}
.grid-item-white {
background-color: white;
}
</style>
<style class="darkreader darkreader--sync" media="screen" type="text/css">
</style>
<p><strong class="example">Example 1:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>It can be done by changing the color of the <code>grid[0][2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>It cannot be done by changing at most one cell.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item"> </div>
<div class="grid-item grid-item-white"> </div>
<div class="grid-item grid-item-white"> </div>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 3</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
</ul>
|
Array; Enumeration; Matrix
|
TypeScript
|
function canMakeSquare(grid: string[][]): boolean {
const dirs: number[] = [0, 0, 1, 1, 0];
for (let i = 0; i < 2; ++i) {
for (let j = 0; j < 2; ++j) {
let [cnt1, cnt2] = [0, 0];
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (grid[x][y] === 'W') {
++cnt1;
} else {
++cnt2;
}
}
if (cnt1 !== cnt2) {
return true;
}
}
}
return false;
}
|
3,128
|
Right Triangles
|
Medium
|
<p>You are given a 2D boolean matrix <code>grid</code>.</p>
<p>A collection of 3 elements of <code>grid</code> is a <strong>right triangle</strong> if one of its elements is in the <strong>same row</strong> with another element and in the <strong>same column</strong> with the third element. The 3 elements may <strong>not</strong> be next to each other.</p>
<p>Return an integer that is the number of <strong>right triangles</strong> that can be made with 3 elements of <code>grid</code> such that <strong>all</strong> of them have a value of 1.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[0,1,1],[0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1. Notice that the blue ones do <strong>not </strong>form a right triangle because the 3 elements are in the same column.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no right triangles with elements of the value 1. Notice that the blue ones do <strong>not</strong> form a right triangle.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,0,0],[1,0,0]]</span></p>
<p><strong>Output: </strong>2</p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 1000</code></li>
<li><code>1 <= grid[i].length <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 1</code></li>
</ul>
|
Array; Hash Table; Math; Combinatorics; Counting
|
C++
|
class Solution {
public:
long long numberOfRightTriangles(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> rows(m);
vector<int> cols(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rows[i] += grid[i][j];
cols[j] += grid[i][j];
}
}
long long ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans += (rows[i] - 1) * (cols[j] - 1);
}
}
}
return ans;
}
};
|
3,128
|
Right Triangles
|
Medium
|
<p>You are given a 2D boolean matrix <code>grid</code>.</p>
<p>A collection of 3 elements of <code>grid</code> is a <strong>right triangle</strong> if one of its elements is in the <strong>same row</strong> with another element and in the <strong>same column</strong> with the third element. The 3 elements may <strong>not</strong> be next to each other.</p>
<p>Return an integer that is the number of <strong>right triangles</strong> that can be made with 3 elements of <code>grid</code> such that <strong>all</strong> of them have a value of 1.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[0,1,1],[0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1. Notice that the blue ones do <strong>not </strong>form a right triangle because the 3 elements are in the same column.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no right triangles with elements of the value 1. Notice that the blue ones do <strong>not</strong> form a right triangle.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,0,0],[1,0,0]]</span></p>
<p><strong>Output: </strong>2</p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 1000</code></li>
<li><code>1 <= grid[i].length <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 1</code></li>
</ul>
|
Array; Hash Table; Math; Combinatorics; Counting
|
Go
|
func numberOfRightTriangles(grid [][]int) (ans int64) {
m, n := len(grid), len(grid[0])
rows := make([]int, m)
cols := make([]int, n)
for i, row := range grid {
for j, x := range row {
rows[i] += x
cols[j] += x
}
}
for i, row := range grid {
for j, x := range row {
if x == 1 {
ans += int64((rows[i] - 1) * (cols[j] - 1))
}
}
}
return
}
|
3,128
|
Right Triangles
|
Medium
|
<p>You are given a 2D boolean matrix <code>grid</code>.</p>
<p>A collection of 3 elements of <code>grid</code> is a <strong>right triangle</strong> if one of its elements is in the <strong>same row</strong> with another element and in the <strong>same column</strong> with the third element. The 3 elements may <strong>not</strong> be next to each other.</p>
<p>Return an integer that is the number of <strong>right triangles</strong> that can be made with 3 elements of <code>grid</code> such that <strong>all</strong> of them have a value of 1.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[0,1,1],[0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1. Notice that the blue ones do <strong>not </strong>form a right triangle because the 3 elements are in the same column.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no right triangles with elements of the value 1. Notice that the blue ones do <strong>not</strong> form a right triangle.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,0,0],[1,0,0]]</span></p>
<p><strong>Output: </strong>2</p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 1000</code></li>
<li><code>1 <= grid[i].length <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 1</code></li>
</ul>
|
Array; Hash Table; Math; Combinatorics; Counting
|
Java
|
class Solution {
public long numberOfRightTriangles(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] rows = new int[m];
int[] cols = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rows[i] += grid[i][j];
cols[j] += grid[i][j];
}
}
long ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans += (rows[i] - 1) * (cols[j] - 1);
}
}
}
return ans;
}
}
|
3,128
|
Right Triangles
|
Medium
|
<p>You are given a 2D boolean matrix <code>grid</code>.</p>
<p>A collection of 3 elements of <code>grid</code> is a <strong>right triangle</strong> if one of its elements is in the <strong>same row</strong> with another element and in the <strong>same column</strong> with the third element. The 3 elements may <strong>not</strong> be next to each other.</p>
<p>Return an integer that is the number of <strong>right triangles</strong> that can be made with 3 elements of <code>grid</code> such that <strong>all</strong> of them have a value of 1.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[0,1,1],[0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1. Notice that the blue ones do <strong>not </strong>form a right triangle because the 3 elements are in the same column.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no right triangles with elements of the value 1. Notice that the blue ones do <strong>not</strong> form a right triangle.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,0,0],[1,0,0]]</span></p>
<p><strong>Output: </strong>2</p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 1000</code></li>
<li><code>1 <= grid[i].length <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 1</code></li>
</ul>
|
Array; Hash Table; Math; Combinatorics; Counting
|
Python
|
class Solution:
def numberOfRightTriangles(self, grid: List[List[int]]) -> int:
rows = [0] * len(grid)
cols = [0] * len(grid[0])
for i, row in enumerate(grid):
for j, x in enumerate(row):
rows[i] += x
cols[j] += x
ans = 0
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x:
ans += (rows[i] - 1) * (cols[j] - 1)
return ans
|
3,128
|
Right Triangles
|
Medium
|
<p>You are given a 2D boolean matrix <code>grid</code>.</p>
<p>A collection of 3 elements of <code>grid</code> is a <strong>right triangle</strong> if one of its elements is in the <strong>same row</strong> with another element and in the <strong>same column</strong> with the third element. The 3 elements may <strong>not</strong> be next to each other.</p>
<p>Return an integer that is the number of <strong>right triangles</strong> that can be made with 3 elements of <code>grid</code> such that <strong>all</strong> of them have a value of 1.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[0,1,1],[0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1. Notice that the blue ones do <strong>not </strong>form a right triangle because the 3 elements are in the same column.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid blue; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no right triangles with elements of the value 1. Notice that the blue ones do <strong>not</strong> form a right triangle.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div style="display:flex; gap: 12px;">
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
<tbody>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
<tr>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
<td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid silver; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td>
</tr>
</tbody>
</table>
</div>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,0,0],[1,0,0]]</span></p>
<p><strong>Output: </strong>2</p>
<p><strong>Explanation:</strong></p>
<p>There are two right triangles with elements of the value 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length <= 1000</code></li>
<li><code>1 <= grid[i].length <= 1000</code></li>
<li><code>0 <= grid[i][j] <= 1</code></li>
</ul>
|
Array; Hash Table; Math; Combinatorics; Counting
|
TypeScript
|
function numberOfRightTriangles(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const rows: number[] = Array(m).fill(0);
const cols: number[] = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
rows[i] += grid[i][j];
cols[j] += grid[i][j];
}
}
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
ans += (rows[i] - 1) * (cols[j] - 1);
}
}
}
return ans;
}
|
3,129
|
Find All Possible Stable Binary Arrays I
|
Medium
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
<p>Note that the binary arrays <code>[1,1,0]</code> and <code>[0,1,1]</code> have subarrays of length 2 with identical elements, hence, they are not stable.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 200</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
C++
|
class Solution {
public:
int numberOfStableArrays(int zero, int one, int limit) {
const int mod = 1e9 + 7;
using ll = long long;
vector<vector<array<ll, 2>>> f = vector<vector<array<ll, 2>>>(zero + 1, vector<array<ll, 2>>(one + 1, {-1, -1}));
auto dfs = [&](this auto&& dfs, int i, int j, int k) -> ll {
if (i < 0 || j < 0) {
return 0;
}
if (i == 0) {
return k == 1 && j <= limit;
}
if (j == 0) {
return k == 0 && i <= limit;
}
ll& res = f[i][j][k];
if (res != -1) {
return res;
}
if (k == 0) {
res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod;
} else {
res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod;
}
return res;
};
return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod;
}
};
|
3,129
|
Find All Possible Stable Binary Arrays I
|
Medium
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
<p>Note that the binary arrays <code>[1,1,0]</code> and <code>[0,1,1]</code> have subarrays of length 2 with identical elements, hence, they are not stable.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 200</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Go
|
func numberOfStableArrays(zero int, one int, limit int) int {
const mod int = 1e9 + 7
f := make([][][2]int, zero+1)
for i := range f {
f[i] = make([][2]int, one+1)
for j := range f[i] {
f[i][j] = [2]int{-1, -1}
}
}
var dfs func(i, j, k int) int
dfs = func(i, j, k int) int {
if i < 0 || j < 0 {
return 0
}
if i == 0 {
if k == 1 && j <= limit {
return 1
}
return 0
}
if j == 0 {
if k == 0 && i <= limit {
return 1
}
return 0
}
res := &f[i][j][k]
if *res != -1 {
return *res
}
if k == 0 {
*res = (dfs(i-1, j, 0) + dfs(i-1, j, 1) - dfs(i-limit-1, j, 1) + mod) % mod
} else {
*res = (dfs(i, j-1, 0) + dfs(i, j-1, 1) - dfs(i, j-limit-1, 0) + mod) % mod
}
return *res
}
return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod
}
|
3,129
|
Find All Possible Stable Binary Arrays I
|
Medium
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
<p>Note that the binary arrays <code>[1,1,0]</code> and <code>[0,1,1]</code> have subarrays of length 2 with identical elements, hence, they are not stable.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 200</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Java
|
class Solution {
private final int mod = (int) 1e9 + 7;
private Long[][][] f;
private int limit;
public int numberOfStableArrays(int zero, int one, int limit) {
f = new Long[zero + 1][one + 1][2];
this.limit = limit;
return (int) ((dfs(zero, one, 0) + dfs(zero, one, 1)) % mod);
}
private long dfs(int i, int j, int k) {
if (i < 0 || j < 0) {
return 0;
}
if (i == 0) {
return k == 1 && j <= limit ? 1 : 0;
}
if (j == 0) {
return k == 0 && i <= limit ? 1 : 0;
}
if (f[i][j][k] != null) {
return f[i][j][k];
}
if (k == 0) {
f[i][j][k]
= (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod;
} else {
f[i][j][k]
= (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod;
}
return f[i][j][k];
}
}
|
3,129
|
Find All Possible Stable Binary Arrays I
|
Medium
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
<p>Note that the binary arrays <code>[1,1,0]</code> and <code>[0,1,1]</code> have subarrays of length 2 with identical elements, hence, they are not stable.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 200</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Python
|
class Solution:
def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
@cache
def dfs(i: int, j: int, k: int) -> int:
if i == 0:
return int(k == 1 and j <= limit)
if j == 0:
return int(k == 0 and i <= limit)
if k == 0:
return (
dfs(i - 1, j, 0)
+ dfs(i - 1, j, 1)
- (0 if i - limit - 1 < 0 else dfs(i - limit - 1, j, 1))
)
return (
dfs(i, j - 1, 0)
+ dfs(i, j - 1, 1)
- (0 if j - limit - 1 < 0 else dfs(i, j - limit - 1, 0))
)
mod = 10**9 + 7
ans = (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod
dfs.cache_clear()
return ans
|
3,129
|
Find All Possible Stable Binary Arrays I
|
Medium
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
<p>Note that the binary arrays <code>[1,1,0]</code> and <code>[0,1,1]</code> have subarrays of length 2 with identical elements, hence, they are not stable.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 200</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
TypeScript
|
function numberOfStableArrays(zero: number, one: number, limit: number): number {
const mod = 1e9 + 7;
const f: number[][][] = Array.from({ length: zero + 1 }, () =>
Array.from({ length: one + 1 }, () => [-1, -1]),
);
const dfs = (i: number, j: number, k: number): number => {
if (i < 0 || j < 0) {
return 0;
}
if (i === 0) {
return k === 1 && j <= limit ? 1 : 0;
}
if (j === 0) {
return k === 0 && i <= limit ? 1 : 0;
}
let res = f[i][j][k];
if (res !== -1) {
return res;
}
if (k === 0) {
res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod;
} else {
res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod;
}
return (f[i][j][k] = res);
};
return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod;
}
|
3,130
|
Find All Possible Stable Binary Arrays II
|
Hard
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 1000</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
C++
|
class Solution {
public:
int numberOfStableArrays(int zero, int one, int limit) {
const int mod = 1e9 + 7;
using ll = long long;
vector<vector<array<ll, 2>>> f = vector<vector<array<ll, 2>>>(zero + 1, vector<array<ll, 2>>(one + 1, {-1, -1}));
auto dfs = [&](this auto&& dfs, int i, int j, int k) -> ll {
if (i < 0 || j < 0) {
return 0;
}
if (i == 0) {
return k == 1 && j <= limit;
}
if (j == 0) {
return k == 0 && i <= limit;
}
ll& res = f[i][j][k];
if (res != -1) {
return res;
}
if (k == 0) {
res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod;
} else {
res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod;
}
return res;
};
return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod;
}
};
|
3,130
|
Find All Possible Stable Binary Arrays II
|
Hard
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 1000</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Go
|
func numberOfStableArrays(zero int, one int, limit int) int {
const mod int = 1e9 + 7
f := make([][][2]int, zero+1)
for i := range f {
f[i] = make([][2]int, one+1)
for j := range f[i] {
f[i][j] = [2]int{-1, -1}
}
}
var dfs func(i, j, k int) int
dfs = func(i, j, k int) int {
if i < 0 || j < 0 {
return 0
}
if i == 0 {
if k == 1 && j <= limit {
return 1
}
return 0
}
if j == 0 {
if k == 0 && i <= limit {
return 1
}
return 0
}
res := &f[i][j][k]
if *res != -1 {
return *res
}
if k == 0 {
*res = (dfs(i-1, j, 0) + dfs(i-1, j, 1) - dfs(i-limit-1, j, 1) + mod) % mod
} else {
*res = (dfs(i, j-1, 0) + dfs(i, j-1, 1) - dfs(i, j-limit-1, 0) + mod) % mod
}
return *res
}
return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod
}
|
3,130
|
Find All Possible Stable Binary Arrays II
|
Hard
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 1000</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Java
|
class Solution {
private final int mod = (int) 1e9 + 7;
private Long[][][] f;
private int limit;
public int numberOfStableArrays(int zero, int one, int limit) {
f = new Long[zero + 1][one + 1][2];
this.limit = limit;
return (int) ((dfs(zero, one, 0) + dfs(zero, one, 1)) % mod);
}
private long dfs(int i, int j, int k) {
if (i < 0 || j < 0) {
return 0;
}
if (i == 0) {
return k == 1 && j <= limit ? 1 : 0;
}
if (j == 0) {
return k == 0 && i <= limit ? 1 : 0;
}
if (f[i][j][k] != null) {
return f[i][j][k];
}
if (k == 0) {
f[i][j][k]
= (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod;
} else {
f[i][j][k]
= (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod;
}
return f[i][j][k];
}
}
|
3,130
|
Find All Possible Stable Binary Arrays II
|
Hard
|
<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>
<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>
<ul>
<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>
<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= zero, one, limit <= 1000</code></li>
</ul>
|
Dynamic Programming; Prefix Sum
|
Python
|
class Solution:
def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
@cache
def dfs(i: int, j: int, k: int) -> int:
if i == 0:
return int(k == 1 and j <= limit)
if j == 0:
return int(k == 0 and i <= limit)
if k == 0:
return (
dfs(i - 1, j, 0)
+ dfs(i - 1, j, 1)
- (0 if i - limit - 1 < 0 else dfs(i - limit - 1, j, 1))
)
return (
dfs(i, j - 1, 0)
+ dfs(i, j - 1, 1)
- (0 if j - limit - 1 < 0 else dfs(i, j - limit - 1, 0))
)
mod = 10**9 + 7
ans = (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod
dfs.cache_clear()
return ans
|
3,131
|
Find the Integer Added to Array I
|
Easy
|
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the integer <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [10], nums2 = [5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-5</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is -5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length <= 100</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array
|
C++
|
class Solution {
public:
int addedInteger(vector<int>& nums1, vector<int>& nums2) {
return *min_element(nums2.begin(), nums2.end()) - *min_element(nums1.begin(), nums1.end());
}
};
|
3,131
|
Find the Integer Added to Array I
|
Easy
|
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the integer <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [10], nums2 = [5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-5</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is -5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length <= 100</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array
|
Go
|
func addedInteger(nums1 []int, nums2 []int) int {
return slices.Min(nums2) - slices.Min(nums1)
}
|
3,131
|
Find the Integer Added to Array I
|
Easy
|
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the integer <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [10], nums2 = [5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-5</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is -5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length <= 100</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array
|
Java
|
class Solution {
public int addedInteger(int[] nums1, int[] nums2) {
return Arrays.stream(nums2).min().getAsInt() - Arrays.stream(nums1).min().getAsInt();
}
}
|
3,131
|
Find the Integer Added to Array I
|
Easy
|
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the integer <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [10], nums2 = [5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-5</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is -5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length <= 100</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array
|
Python
|
class Solution:
def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
return min(nums2) - min(nums1)
|
3,131
|
Find the Integer Added to Array I
|
Easy
|
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the integer <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [10], nums2 = [5]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-5</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is -5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The integer added to each element of <code>nums1</code> is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length <= 100</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array
|
TypeScript
|
function addedInteger(nums1: number[], nums2: number[]): number {
return Math.min(...nums2) - Math.min(...nums1);
}
|
3,132
|
Find the Integer Added to Array II
|
Medium
|
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>.</p>
<p>From <code>nums1</code> two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the <strong>minimum</strong> possible integer<em> </em><code>x</code><em> </em>that achieves this equivalence.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [4,20,16,12,8], nums2 = [14,18,10]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,4]</code> and adding -2, <code>nums1</code> becomes <code>[18,14,10]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [3,5,5,3], nums2 = [7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,3]</code> and adding 2, <code>nums1</code> becomes <code>[7,7]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums1.length <= 200</code></li>
<li><code>nums2.length == nums1.length - 2</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by removing two elements and adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array; Two Pointers; Enumeration; Sorting
|
C++
|
class Solution {
public:
int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int ans = 1 << 30;
auto f = [&](int x) {
int i = 0, j = 0, cnt = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums2[j] - nums1[i] != x) {
++cnt;
} else {
++j;
}
++i;
}
return cnt <= 2;
};
for (int i = 0; i < 3; ++i) {
int x = nums2[0] - nums1[i];
if (f(x)) {
ans = min(ans, x);
}
}
return ans;
}
};
|
3,132
|
Find the Integer Added to Array II
|
Medium
|
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>.</p>
<p>From <code>nums1</code> two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the <strong>minimum</strong> possible integer<em> </em><code>x</code><em> </em>that achieves this equivalence.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [4,20,16,12,8], nums2 = [14,18,10]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,4]</code> and adding -2, <code>nums1</code> becomes <code>[18,14,10]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [3,5,5,3], nums2 = [7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,3]</code> and adding 2, <code>nums1</code> becomes <code>[7,7]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums1.length <= 200</code></li>
<li><code>nums2.length == nums1.length - 2</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by removing two elements and adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array; Two Pointers; Enumeration; Sorting
|
Go
|
func minimumAddedInteger(nums1 []int, nums2 []int) int {
sort.Ints(nums1)
sort.Ints(nums2)
ans := 1 << 30
f := func(x int) bool {
i, j, cnt := 0, 0, 0
for i < len(nums1) && j < len(nums2) {
if nums2[j]-nums1[i] != x {
cnt++
} else {
j++
}
i++
}
return cnt <= 2
}
for _, a := range nums1[:3] {
x := nums2[0] - a
if f(x) {
ans = min(ans, x)
}
}
return ans
}
|
3,132
|
Find the Integer Added to Array II
|
Medium
|
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>.</p>
<p>From <code>nums1</code> two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the <strong>minimum</strong> possible integer<em> </em><code>x</code><em> </em>that achieves this equivalence.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [4,20,16,12,8], nums2 = [14,18,10]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,4]</code> and adding -2, <code>nums1</code> becomes <code>[18,14,10]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [3,5,5,3], nums2 = [7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,3]</code> and adding 2, <code>nums1</code> becomes <code>[7,7]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums1.length <= 200</code></li>
<li><code>nums2.length == nums1.length - 2</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by removing two elements and adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array; Two Pointers; Enumeration; Sorting
|
Java
|
class Solution {
public int minimumAddedInteger(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int ans = 1 << 30;
for (int i = 0; i < 3; ++i) {
int x = nums2[0] - nums1[i];
if (f(nums1, nums2, x)) {
ans = Math.min(ans, x);
}
}
return ans;
}
private boolean f(int[] nums1, int[] nums2, int x) {
int i = 0, j = 0, cnt = 0;
while (i < nums1.length && j < nums2.length) {
if (nums2[j] - nums1[i] != x) {
++cnt;
} else {
++j;
}
++i;
}
return cnt <= 2;
}
}
|
3,132
|
Find the Integer Added to Array II
|
Medium
|
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>.</p>
<p>From <code>nums1</code> two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the <strong>minimum</strong> possible integer<em> </em><code>x</code><em> </em>that achieves this equivalence.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [4,20,16,12,8], nums2 = [14,18,10]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,4]</code> and adding -2, <code>nums1</code> becomes <code>[18,14,10]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [3,5,5,3], nums2 = [7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,3]</code> and adding 2, <code>nums1</code> becomes <code>[7,7]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums1.length <= 200</code></li>
<li><code>nums2.length == nums1.length - 2</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by removing two elements and adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array; Two Pointers; Enumeration; Sorting
|
Python
|
class Solution:
def minimumAddedInteger(self, nums1: List[int], nums2: List[int]) -> int:
def f(x: int) -> bool:
i = j = cnt = 0
while i < len(nums1) and j < len(nums2):
if nums2[j] - nums1[i] != x:
cnt += 1
else:
j += 1
i += 1
return cnt <= 2
nums1.sort()
nums2.sort()
ans = inf
for i in range(3):
x = nums2[0] - nums1[i]
if f(x):
ans = min(ans, x)
return ans
|
3,132
|
Find the Integer Added to Array II
|
Medium
|
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>.</p>
<p>From <code>nums1</code> two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
<p>Return the <strong>minimum</strong> possible integer<em> </em><code>x</code><em> </em>that achieves this equivalence.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [4,20,16,12,8], nums2 = [14,18,10]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">-2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,4]</code> and adding -2, <code>nums1</code> becomes <code>[18,14,10]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">nums1 = [3,5,5,3], nums2 = [7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing elements at indices <code>[0,3]</code> and adding 2, <code>nums1</code> becomes <code>[7,7]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums1.length <= 200</code></li>
<li><code>nums2.length == nums1.length - 2</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 1000</code></li>
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by removing two elements and adding <code>x</code> to each element of <code>nums1</code>.</li>
</ul>
|
Array; Two Pointers; Enumeration; Sorting
|
TypeScript
|
function minimumAddedInteger(nums1: number[], nums2: number[]): number {
nums1.sort((a, b) => a - b);
nums2.sort((a, b) => a - b);
const f = (x: number): boolean => {
let [i, j, cnt] = [0, 0, 0];
while (i < nums1.length && j < nums2.length) {
if (nums2[j] - nums1[i] !== x) {
++cnt;
} else {
++j;
}
++i;
}
return cnt <= 2;
};
let ans = Infinity;
for (let i = 0; i < 3; ++i) {
const x = nums2[0] - nums1[i];
if (f(x)) {
ans = Math.min(ans, x);
}
}
return ans;
}
|
3,133
|
Minimum Array End
|
Medium
|
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 <= i < n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result of the bitwise <code>AND</code> operation between all elements of <code>nums</code> is <code>x</code>.</p>
<p>Return the <strong>minimum</strong> possible value of <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, x = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[4,5,6]</code> and its last element is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, x = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[7,15]</code> and its last element is 15.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, x <= 10<sup>8</sup></code></li>
</ul>
|
Bit Manipulation
|
C++
|
class Solution {
public:
long long minEnd(int n, int x) {
--n;
long long ans = x;
for (int i = 0; i < 31; ++i) {
if (x >> i & 1 ^ 1) {
ans |= (n & 1) << i;
n >>= 1;
}
}
ans |= (1LL * n) << 31;
return ans;
}
};
|
3,133
|
Minimum Array End
|
Medium
|
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 <= i < n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result of the bitwise <code>AND</code> operation between all elements of <code>nums</code> is <code>x</code>.</p>
<p>Return the <strong>minimum</strong> possible value of <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, x = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[4,5,6]</code> and its last element is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, x = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[7,15]</code> and its last element is 15.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, x <= 10<sup>8</sup></code></li>
</ul>
|
Bit Manipulation
|
Go
|
func minEnd(n int, x int) (ans int64) {
n--
ans = int64(x)
for i := 0; i < 31; i++ {
if x>>i&1 == 0 {
ans |= int64((n & 1) << i)
n >>= 1
}
}
ans |= int64(n) << 31
return
}
|
3,133
|
Minimum Array End
|
Medium
|
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 <= i < n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result of the bitwise <code>AND</code> operation between all elements of <code>nums</code> is <code>x</code>.</p>
<p>Return the <strong>minimum</strong> possible value of <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, x = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[4,5,6]</code> and its last element is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, x = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[7,15]</code> and its last element is 15.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, x <= 10<sup>8</sup></code></li>
</ul>
|
Bit Manipulation
|
Java
|
class Solution {
public long minEnd(int n, int x) {
--n;
long ans = x;
for (int i = 0; i < 31; ++i) {
if ((x >> i & 1) == 0) {
ans |= (n & 1) << i;
n >>= 1;
}
}
ans |= (long) n << 31;
return ans;
}
}
|
3,133
|
Minimum Array End
|
Medium
|
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 <= i < n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result of the bitwise <code>AND</code> operation between all elements of <code>nums</code> is <code>x</code>.</p>
<p>Return the <strong>minimum</strong> possible value of <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, x = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[4,5,6]</code> and its last element is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, x = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[7,15]</code> and its last element is 15.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, x <= 10<sup>8</sup></code></li>
</ul>
|
Bit Manipulation
|
Python
|
class Solution:
def minEnd(self, n: int, x: int) -> int:
n -= 1
ans = x
for i in range(31):
if x >> i & 1 ^ 1:
ans |= (n & 1) << i
n >>= 1
ans |= n << 31
return ans
|
3,133
|
Minimum Array End
|
Medium
|
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 <= i < n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result of the bitwise <code>AND</code> operation between all elements of <code>nums</code> is <code>x</code>.</p>
<p>Return the <strong>minimum</strong> possible value of <code>nums[n - 1]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, x = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[4,5,6]</code> and its last element is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, x = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p><code>nums</code> can be <code>[7,15]</code> and its last element is 15.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, x <= 10<sup>8</sup></code></li>
</ul>
|
Bit Manipulation
|
TypeScript
|
function minEnd(n: number, x: number): number {
--n;
let ans: bigint = BigInt(x);
for (let i = 0; i < 31; ++i) {
if (((x >> i) & 1) ^ 1) {
ans |= BigInt(n & 1) << BigInt(i);
n >>= 1;
}
}
ans |= BigInt(n) << BigInt(31);
return Number(ans);
}
|
3,134
|
Find the Median of the Uniqueness Array
|
Hard
|
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <code>distinct(nums[i..j])</code>, for all <code>0 <= i <= j < nums.length</code>.</p>
<p>Here, <code>distinct(nums[i..j])</code> denotes the number of distinct elements in the subarray that starts at index <code>i</code> and ends at index <code>j</code>.</p>
<p>Return the <strong>median</strong> of the <strong>uniqueness array</strong> of <code>nums</code>.</p>
<p><strong>Note</strong> that the <strong>median</strong> of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the <strong>smaller</strong> of the two values is taken.<!-- notionvc: 7e0f5178-4273-4a82-95ce-3395297921dc --></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]</code> which is equal to <code>[1, 1, 1, 2, 2, 3]</code>. The uniqueness array has a median of 1. Therefore, the answer is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
|
Array; Hash Table; Binary Search; Sliding Window
|
C++
|
class Solution {
public:
int medianOfUniquenessArray(vector<int>& nums) {
int n = nums.size();
using ll = long long;
ll m = (1LL + n) * n / 2;
int l = 0, r = n;
auto check = [&](int mx) -> bool {
unordered_map<int, int> cnt;
ll k = 0;
for (int l = 0, r = 0; r < n; ++r) {
int x = nums[r];
++cnt[x];
while (cnt.size() > mx) {
int y = nums[l++];
if (--cnt[y] == 0) {
cnt.erase(y);
}
}
k += r - l + 1;
if (k >= (m + 1) / 2) {
return true;
}
}
return false;
};
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
};
|
3,134
|
Find the Median of the Uniqueness Array
|
Hard
|
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <code>distinct(nums[i..j])</code>, for all <code>0 <= i <= j < nums.length</code>.</p>
<p>Here, <code>distinct(nums[i..j])</code> denotes the number of distinct elements in the subarray that starts at index <code>i</code> and ends at index <code>j</code>.</p>
<p>Return the <strong>median</strong> of the <strong>uniqueness array</strong> of <code>nums</code>.</p>
<p><strong>Note</strong> that the <strong>median</strong> of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the <strong>smaller</strong> of the two values is taken.<!-- notionvc: 7e0f5178-4273-4a82-95ce-3395297921dc --></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]</code> which is equal to <code>[1, 1, 1, 2, 2, 3]</code>. The uniqueness array has a median of 1. Therefore, the answer is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
|
Array; Hash Table; Binary Search; Sliding Window
|
Go
|
func medianOfUniquenessArray(nums []int) int {
n := len(nums)
m := (1 + n) * n / 2
return sort.Search(n, func(mx int) bool {
cnt := map[int]int{}
l, k := 0, 0
for r, x := range nums {
cnt[x]++
for len(cnt) > mx {
y := nums[l]
cnt[y]--
if cnt[y] == 0 {
delete(cnt, y)
}
l++
}
k += r - l + 1
if k >= (m+1)/2 {
return true
}
}
return false
})
}
|
3,134
|
Find the Median of the Uniqueness Array
|
Hard
|
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <code>distinct(nums[i..j])</code>, for all <code>0 <= i <= j < nums.length</code>.</p>
<p>Here, <code>distinct(nums[i..j])</code> denotes the number of distinct elements in the subarray that starts at index <code>i</code> and ends at index <code>j</code>.</p>
<p>Return the <strong>median</strong> of the <strong>uniqueness array</strong> of <code>nums</code>.</p>
<p><strong>Note</strong> that the <strong>median</strong> of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the <strong>smaller</strong> of the two values is taken.<!-- notionvc: 7e0f5178-4273-4a82-95ce-3395297921dc --></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]</code> which is equal to <code>[1, 1, 1, 2, 2, 3]</code>. The uniqueness array has a median of 1. Therefore, the answer is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
|
Array; Hash Table; Binary Search; Sliding Window
|
Java
|
class Solution {
private long m;
private int[] nums;
public int medianOfUniquenessArray(int[] nums) {
int n = nums.length;
this.nums = nums;
m = (1L + n) * n / 2;
int l = 0, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
private boolean check(int mx) {
Map<Integer, Integer> cnt = new HashMap<>();
long k = 0;
for (int l = 0, r = 0; r < nums.length; ++r) {
int x = nums[r];
cnt.merge(x, 1, Integer::sum);
while (cnt.size() > mx) {
int y = nums[l++];
if (cnt.merge(y, -1, Integer::sum) == 0) {
cnt.remove(y);
}
}
k += r - l + 1;
if (k >= (m + 1) / 2) {
return true;
}
}
return false;
}
}
|
3,134
|
Find the Median of the Uniqueness Array
|
Hard
|
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <code>distinct(nums[i..j])</code>, for all <code>0 <= i <= j < nums.length</code>.</p>
<p>Here, <code>distinct(nums[i..j])</code> denotes the number of distinct elements in the subarray that starts at index <code>i</code> and ends at index <code>j</code>.</p>
<p>Return the <strong>median</strong> of the <strong>uniqueness array</strong> of <code>nums</code>.</p>
<p><strong>Note</strong> that the <strong>median</strong> of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the <strong>smaller</strong> of the two values is taken.<!-- notionvc: 7e0f5178-4273-4a82-95ce-3395297921dc --></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]</code> which is equal to <code>[1, 1, 1, 2, 2, 3]</code>. The uniqueness array has a median of 1. Therefore, the answer is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
|
Array; Hash Table; Binary Search; Sliding Window
|
Python
|
class Solution:
def medianOfUniquenessArray(self, nums: List[int]) -> int:
def check(mx: int) -> bool:
cnt = defaultdict(int)
k = l = 0
for r, x in enumerate(nums):
cnt[x] += 1
while len(cnt) > mx:
y = nums[l]
cnt[y] -= 1
if cnt[y] == 0:
cnt.pop(y)
l += 1
k += r - l + 1
if k >= (m + 1) // 2:
return True
return False
n = len(nums)
m = (1 + n) * n // 2
return bisect_left(range(n), True, key=check)
|
3,134
|
Find the Median of the Uniqueness Array
|
Hard
|
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <code>distinct(nums[i..j])</code>, for all <code>0 <= i <= j < nums.length</code>.</p>
<p>Here, <code>distinct(nums[i..j])</code> denotes the number of distinct elements in the subarray that starts at index <code>i</code> and ends at index <code>j</code>.</p>
<p>Return the <strong>median</strong> of the <strong>uniqueness array</strong> of <code>nums</code>.</p>
<p><strong>Note</strong> that the <strong>median</strong> of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the <strong>smaller</strong> of the two values is taken.<!-- notionvc: 7e0f5178-4273-4a82-95ce-3395297921dc --></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]</code> which is equal to <code>[1, 1, 1, 2, 2, 3]</code>. The uniqueness array has a median of 1. Therefore, the answer is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The uniqueness array of <code>nums</code> is <code>[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]</code>. The uniqueness array has a median of 2. Therefore, the answer is 2.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
|
Array; Hash Table; Binary Search; Sliding Window
|
TypeScript
|
function medianOfUniquenessArray(nums: number[]): number {
const n = nums.length;
const m = Math.floor(((1 + n) * n) / 2);
let [l, r] = [0, n];
const check = (mx: number): boolean => {
const cnt = new Map<number, number>();
let [l, k] = [0, 0];
for (let r = 0; r < n; ++r) {
const x = nums[r];
cnt.set(x, (cnt.get(x) || 0) + 1);
while (cnt.size > mx) {
const y = nums[l++];
cnt.set(y, cnt.get(y)! - 1);
if (cnt.get(y) === 0) {
cnt.delete(y);
}
}
k += r - l + 1;
if (k >= Math.floor((m + 1) / 2)) {
return true;
}
}
return false;
};
while (l < r) {
const mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
|
3,135
|
Equalize Strings by Adding or Removing Characters at Ends
|
Medium
|
<p>Given two strings <code>initial</code> and <code>target</code>, your task is to modify <code>initial</code> by performing a series of operations to make it equal to <code>target</code>.</p>
<p>In one operation, you can add or remove <strong>one character</strong> only at the <em>beginning</em> or the <em>end</em> of the string <code>initial</code>.</p>
<p>Return the <strong>minimum</strong> number of operations required to <em>transform</em> <code>initial</code> into <code>target</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "abcde", target = "cdef"</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Remove <code>'a'</code> and <code>'b'</code> from the beginning of <code>initial</code>, then add <code>'f'</code> to the end.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "axxy", target = "yabx"</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<table border="1">
<tbody>
<tr>
<th>Operation</th>
<th>Resulting String</th>
</tr>
<tr>
<td>Add <code>'y'</code> to the beginning</td>
<td><code>"yaxxy"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yaxx"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yax"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"ya"</code></td>
</tr>
<tr>
<td>Add <code>'b'</code> to the end</td>
<td><code>"yab"</code></td>
</tr>
<tr>
<td>Add <code>'x'</code> to the end</td>
<td><code>"yabx"</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "xyz", target = "xyz"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No operations are needed as the strings are already equal.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= initial.length, target.length <= 1000</code></li>
<li><code>initial</code> and <code>target</code> consist only of lowercase English letters.</li>
</ul>
|
String; Binary Search; Dynamic Programming; Sliding Window; Hash Function
|
C++
|
class Solution {
public:
int minOperations(string initial, string target) {
int m = initial.size(), n = target.size();
int f[m + 1][n + 1];
memset(f, 0, sizeof(f));
int mx = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (initial[i - 1] == target[j - 1]) {
f[i][j] = f[i - 1][j - 1] + 1;
mx = max(mx, f[i][j]);
}
}
}
return m + n - 2 * mx;
}
};
|
3,135
|
Equalize Strings by Adding or Removing Characters at Ends
|
Medium
|
<p>Given two strings <code>initial</code> and <code>target</code>, your task is to modify <code>initial</code> by performing a series of operations to make it equal to <code>target</code>.</p>
<p>In one operation, you can add or remove <strong>one character</strong> only at the <em>beginning</em> or the <em>end</em> of the string <code>initial</code>.</p>
<p>Return the <strong>minimum</strong> number of operations required to <em>transform</em> <code>initial</code> into <code>target</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "abcde", target = "cdef"</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Remove <code>'a'</code> and <code>'b'</code> from the beginning of <code>initial</code>, then add <code>'f'</code> to the end.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "axxy", target = "yabx"</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<table border="1">
<tbody>
<tr>
<th>Operation</th>
<th>Resulting String</th>
</tr>
<tr>
<td>Add <code>'y'</code> to the beginning</td>
<td><code>"yaxxy"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yaxx"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yax"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"ya"</code></td>
</tr>
<tr>
<td>Add <code>'b'</code> to the end</td>
<td><code>"yab"</code></td>
</tr>
<tr>
<td>Add <code>'x'</code> to the end</td>
<td><code>"yabx"</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "xyz", target = "xyz"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No operations are needed as the strings are already equal.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= initial.length, target.length <= 1000</code></li>
<li><code>initial</code> and <code>target</code> consist only of lowercase English letters.</li>
</ul>
|
String; Binary Search; Dynamic Programming; Sliding Window; Hash Function
|
Go
|
func minOperations(initial string, target string) int {
m, n := len(initial), len(target)
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
mx := 0
for i, a := range initial {
for j, b := range target {
if a == b {
f[i+1][j+1] = f[i][j] + 1
mx = max(mx, f[i+1][j+1])
}
}
}
return m + n - 2*mx
}
|
3,135
|
Equalize Strings by Adding or Removing Characters at Ends
|
Medium
|
<p>Given two strings <code>initial</code> and <code>target</code>, your task is to modify <code>initial</code> by performing a series of operations to make it equal to <code>target</code>.</p>
<p>In one operation, you can add or remove <strong>one character</strong> only at the <em>beginning</em> or the <em>end</em> of the string <code>initial</code>.</p>
<p>Return the <strong>minimum</strong> number of operations required to <em>transform</em> <code>initial</code> into <code>target</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "abcde", target = "cdef"</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Remove <code>'a'</code> and <code>'b'</code> from the beginning of <code>initial</code>, then add <code>'f'</code> to the end.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "axxy", target = "yabx"</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<table border="1">
<tbody>
<tr>
<th>Operation</th>
<th>Resulting String</th>
</tr>
<tr>
<td>Add <code>'y'</code> to the beginning</td>
<td><code>"yaxxy"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yaxx"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yax"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"ya"</code></td>
</tr>
<tr>
<td>Add <code>'b'</code> to the end</td>
<td><code>"yab"</code></td>
</tr>
<tr>
<td>Add <code>'x'</code> to the end</td>
<td><code>"yabx"</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "xyz", target = "xyz"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No operations are needed as the strings are already equal.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= initial.length, target.length <= 1000</code></li>
<li><code>initial</code> and <code>target</code> consist only of lowercase English letters.</li>
</ul>
|
String; Binary Search; Dynamic Programming; Sliding Window; Hash Function
|
Java
|
class Solution {
public int minOperations(String initial, String target) {
int m = initial.length(), n = target.length();
int[][] f = new int[m + 1][n + 1];
int mx = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (initial.charAt(i - 1) == target.charAt(j - 1)) {
f[i][j] = f[i - 1][j - 1] + 1;
mx = Math.max(mx, f[i][j]);
}
}
}
return m + n - 2 * mx;
}
}
|
3,135
|
Equalize Strings by Adding or Removing Characters at Ends
|
Medium
|
<p>Given two strings <code>initial</code> and <code>target</code>, your task is to modify <code>initial</code> by performing a series of operations to make it equal to <code>target</code>.</p>
<p>In one operation, you can add or remove <strong>one character</strong> only at the <em>beginning</em> or the <em>end</em> of the string <code>initial</code>.</p>
<p>Return the <strong>minimum</strong> number of operations required to <em>transform</em> <code>initial</code> into <code>target</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "abcde", target = "cdef"</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Remove <code>'a'</code> and <code>'b'</code> from the beginning of <code>initial</code>, then add <code>'f'</code> to the end.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "axxy", target = "yabx"</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<table border="1">
<tbody>
<tr>
<th>Operation</th>
<th>Resulting String</th>
</tr>
<tr>
<td>Add <code>'y'</code> to the beginning</td>
<td><code>"yaxxy"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yaxx"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yax"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"ya"</code></td>
</tr>
<tr>
<td>Add <code>'b'</code> to the end</td>
<td><code>"yab"</code></td>
</tr>
<tr>
<td>Add <code>'x'</code> to the end</td>
<td><code>"yabx"</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "xyz", target = "xyz"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No operations are needed as the strings are already equal.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= initial.length, target.length <= 1000</code></li>
<li><code>initial</code> and <code>target</code> consist only of lowercase English letters.</li>
</ul>
|
String; Binary Search; Dynamic Programming; Sliding Window; Hash Function
|
Python
|
class Solution:
def minOperations(self, initial: str, target: str) -> int:
m, n = len(initial), len(target)
f = [[0] * (n + 1) for _ in range(m + 1)]
mx = 0
for i, a in enumerate(initial, 1):
for j, b in enumerate(target, 1):
if a == b:
f[i][j] = f[i - 1][j - 1] + 1
mx = max(mx, f[i][j])
return m + n - mx * 2
|
3,135
|
Equalize Strings by Adding or Removing Characters at Ends
|
Medium
|
<p>Given two strings <code>initial</code> and <code>target</code>, your task is to modify <code>initial</code> by performing a series of operations to make it equal to <code>target</code>.</p>
<p>In one operation, you can add or remove <strong>one character</strong> only at the <em>beginning</em> or the <em>end</em> of the string <code>initial</code>.</p>
<p>Return the <strong>minimum</strong> number of operations required to <em>transform</em> <code>initial</code> into <code>target</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "abcde", target = "cdef"</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Remove <code>'a'</code> and <code>'b'</code> from the beginning of <code>initial</code>, then add <code>'f'</code> to the end.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "axxy", target = "yabx"</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<table border="1">
<tbody>
<tr>
<th>Operation</th>
<th>Resulting String</th>
</tr>
<tr>
<td>Add <code>'y'</code> to the beginning</td>
<td><code>"yaxxy"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yaxx"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"yax"</code></td>
</tr>
<tr>
<td>Remove from end</td>
<td><code>"ya"</code></td>
</tr>
<tr>
<td>Add <code>'b'</code> to the end</td>
<td><code>"yab"</code></td>
</tr>
<tr>
<td>Add <code>'x'</code> to the end</td>
<td><code>"yabx"</code></td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">initial = "xyz", target = "xyz"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No operations are needed as the strings are already equal.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= initial.length, target.length <= 1000</code></li>
<li><code>initial</code> and <code>target</code> consist only of lowercase English letters.</li>
</ul>
|
String; Binary Search; Dynamic Programming; Sliding Window; Hash Function
|
TypeScript
|
function minOperations(initial: string, target: string): number {
const m = initial.length;
const n = target.length;
const f: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
let mx: number = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
if (initial[i - 1] === target[j - 1]) {
f[i][j] = f[i - 1][j - 1] + 1;
mx = Math.max(mx, f[i][j]);
}
}
}
return m + n - 2 * mx;
}
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
C++
|
class Solution {
public:
bool isValid(string word) {
if (word.size() < 3) {
return false;
}
bool has_vowel = false, has_consonant = false;
bool vs[26]{};
string vowels = "aeiou";
for (char c : vowels) {
vs[c - 'a'] = true;
}
for (char c : word) {
if (isalpha(c)) {
if (vs[tolower(c) - 'a']) {
has_vowel = true;
} else {
has_consonant = true;
}
} else if (!isdigit(c)) {
return false;
}
}
return has_vowel && has_consonant;
}
};
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
C#
|
public class Solution {
public bool IsValid(string word) {
if (word.Length < 3) {
return false;
}
bool hasVowel = false, hasConsonant = false;
bool[] vs = new bool[26];
foreach (char c in "aeiou") {
vs[c - 'a'] = true;
}
foreach (char c in word) {
if (char.IsLetter(c)) {
char lower = char.ToLower(c);
if (vs[lower - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!char.IsDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
Go
|
func isValid(word string) bool {
if len(word) < 3 {
return false
}
hasVowel := false
hasConsonant := false
vs := make([]bool, 26)
for _, c := range "aeiou" {
vs[c-'a'] = true
}
for _, c := range word {
if unicode.IsLetter(c) {
if vs[unicode.ToLower(c)-'a'] {
hasVowel = true
} else {
hasConsonant = true
}
} else if !unicode.IsDigit(c) {
return false
}
}
return hasVowel && hasConsonant
}
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
Java
|
class Solution {
public boolean isValid(String word) {
if (word.length() < 3) {
return false;
}
boolean hasVowel = false, hasConsonant = false;
boolean[] vs = new boolean[26];
for (char c : "aeiou".toCharArray()) {
vs[c - 'a'] = true;
}
for (char c : word.toCharArray()) {
if (Character.isAlphabetic(c)) {
if (vs[Character.toLowerCase(c) - 'a']) {
hasVowel = true;
} else {
hasConsonant = true;
}
} else if (!Character.isDigit(c)) {
return false;
}
}
return hasVowel && hasConsonant;
}
}
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
Python
|
class Solution:
def isValid(self, word: str) -> bool:
if len(word) < 3:
return False
has_vowel = has_consonant = False
vs = set("aeiouAEIOU")
for c in word:
if not c.isalnum():
return False
if c.isalpha():
if c in vs:
has_vowel = True
else:
has_consonant = True
return has_vowel and has_consonant
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
Rust
|
impl Solution {
pub fn is_valid(word: String) -> bool {
if word.len() < 3 {
return false;
}
let mut has_vowel = false;
let mut has_consonant = false;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for c in word.chars() {
if !c.is_alphanumeric() {
return false;
}
if c.is_alphabetic() {
let lower_c = c.to_ascii_lowercase();
if vowels.contains(&lower_c) {
has_vowel = true;
} else {
has_consonant = true;
}
}
}
has_vowel && has_consonant
}
}
|
3,136
|
Valid Word
|
Easy
|
<p>A word is considered <strong>valid</strong> if:</p>
<ul>
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
</ul>
<p>You are given a string <code>word</code>.</p>
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li><code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, and their uppercases are <strong>vowels</strong>.</li>
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "234Adas"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>This word satisfies the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "b3"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word = "a3$e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>This word contains a <code>'$'</code> character and does not have a consonant.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 20</code></li>
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>'@'</code>, <code>'#'</code>, and <code>'$'</code>.</li>
</ul>
|
String
|
TypeScript
|
function isValid(word: string): boolean {
if (word.length < 3) {
return false;
}
let hasVowel: boolean = false;
let hasConsonant: boolean = false;
const vowels: Set<string> = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
for (const c of word) {
if (!c.match(/[a-zA-Z0-9]/)) {
return false;
}
if (/[a-zA-Z]/.test(c)) {
if (vowels.has(c)) {
hasVowel = true;
} else {
hasConsonant = true;
}
}
}
return hasVowel && hasConsonant;
}
|
3,137
|
Minimum Number of Operations to Make Word K-Periodic
|
Medium
|
<p>You are given a string <code>word</code> of size <code>n</code>, and an integer <code>k</code> such that <code>k</code> divides <code>n</code>.</p>
<p>In one operation, you can pick any two indices <code>i</code> and <code>j</code>, that are divisible by <code>k</code>, then replace the <span data-keyword="substring">substring</span> of length <code>k</code> starting at <code>i</code> with the substring of length <code>k</code> starting at <code>j</code>. That is, replace the substring <code>word[i..i + k - 1]</code> with the substring <code>word[j..j + k - 1]</code>.<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 --></p>
<p>Return <em>the <strong>minimum</strong> number of operations required to make</em> <code>word</code> <em><strong>k-periodic</strong></em>.</p>
<p>We say that <code>word</code> is <strong>k-periodic</strong> if there is some string <code>s</code> of length <code>k</code> such that <code>word</code> can be obtained by concatenating <code>s</code> an arbitrary number of times. For example, if <code>word == “ababab”</code>, then <code>word</code> is 2-periodic for <code>s = "ab"</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "leetcodeleet", k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">1</span></p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "</span>leetcoleet<span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">", k = 2</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 2-periodic string by applying the operations in the table below.</p>
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
<tbody>
<tr>
<th>i</th>
<th>j</th>
<th>word</th>
</tr>
<tr>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">2</td>
<td style="padding: 5px 15px;">etetcoleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">4</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">6</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetetet</td>
</tr>
</tbody>
</table>
</div>
<div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
<div class="gtx-trans-icon"> </div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == word.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= word.length</code></li>
<li><code>k</code> divides <code>word.length</code>.</li>
<li><code>word</code> consists only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
C++
|
class Solution {
public:
int minimumOperationsToMakeKPeriodic(string word, int k) {
unordered_map<string, int> cnt;
int n = word.size();
int mx = 0;
for (int i = 0; i < n; i += k) {
mx = max(mx, ++cnt[word.substr(i, k)]);
}
return n / k - mx;
}
};
|
3,137
|
Minimum Number of Operations to Make Word K-Periodic
|
Medium
|
<p>You are given a string <code>word</code> of size <code>n</code>, and an integer <code>k</code> such that <code>k</code> divides <code>n</code>.</p>
<p>In one operation, you can pick any two indices <code>i</code> and <code>j</code>, that are divisible by <code>k</code>, then replace the <span data-keyword="substring">substring</span> of length <code>k</code> starting at <code>i</code> with the substring of length <code>k</code> starting at <code>j</code>. That is, replace the substring <code>word[i..i + k - 1]</code> with the substring <code>word[j..j + k - 1]</code>.<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 --></p>
<p>Return <em>the <strong>minimum</strong> number of operations required to make</em> <code>word</code> <em><strong>k-periodic</strong></em>.</p>
<p>We say that <code>word</code> is <strong>k-periodic</strong> if there is some string <code>s</code> of length <code>k</code> such that <code>word</code> can be obtained by concatenating <code>s</code> an arbitrary number of times. For example, if <code>word == “ababab”</code>, then <code>word</code> is 2-periodic for <code>s = "ab"</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "leetcodeleet", k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">1</span></p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "</span>leetcoleet<span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">", k = 2</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 2-periodic string by applying the operations in the table below.</p>
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
<tbody>
<tr>
<th>i</th>
<th>j</th>
<th>word</th>
</tr>
<tr>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">2</td>
<td style="padding: 5px 15px;">etetcoleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">4</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">6</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetetet</td>
</tr>
</tbody>
</table>
</div>
<div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
<div class="gtx-trans-icon"> </div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == word.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= word.length</code></li>
<li><code>k</code> divides <code>word.length</code>.</li>
<li><code>word</code> consists only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Go
|
func minimumOperationsToMakeKPeriodic(word string, k int) int {
cnt := map[string]int{}
n := len(word)
mx := 0
for i := 0; i < n; i += k {
s := word[i : i+k]
cnt[s]++
mx = max(mx, cnt[s])
}
return n/k - mx
}
|
3,137
|
Minimum Number of Operations to Make Word K-Periodic
|
Medium
|
<p>You are given a string <code>word</code> of size <code>n</code>, and an integer <code>k</code> such that <code>k</code> divides <code>n</code>.</p>
<p>In one operation, you can pick any two indices <code>i</code> and <code>j</code>, that are divisible by <code>k</code>, then replace the <span data-keyword="substring">substring</span> of length <code>k</code> starting at <code>i</code> with the substring of length <code>k</code> starting at <code>j</code>. That is, replace the substring <code>word[i..i + k - 1]</code> with the substring <code>word[j..j + k - 1]</code>.<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 --></p>
<p>Return <em>the <strong>minimum</strong> number of operations required to make</em> <code>word</code> <em><strong>k-periodic</strong></em>.</p>
<p>We say that <code>word</code> is <strong>k-periodic</strong> if there is some string <code>s</code> of length <code>k</code> such that <code>word</code> can be obtained by concatenating <code>s</code> an arbitrary number of times. For example, if <code>word == “ababab”</code>, then <code>word</code> is 2-periodic for <code>s = "ab"</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "leetcodeleet", k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">1</span></p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "</span>leetcoleet<span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">", k = 2</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 2-periodic string by applying the operations in the table below.</p>
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
<tbody>
<tr>
<th>i</th>
<th>j</th>
<th>word</th>
</tr>
<tr>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">2</td>
<td style="padding: 5px 15px;">etetcoleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">4</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">6</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetetet</td>
</tr>
</tbody>
</table>
</div>
<div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
<div class="gtx-trans-icon"> </div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == word.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= word.length</code></li>
<li><code>k</code> divides <code>word.length</code>.</li>
<li><code>word</code> consists only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Java
|
class Solution {
public int minimumOperationsToMakeKPeriodic(String word, int k) {
Map<String, Integer> cnt = new HashMap<>();
int n = word.length();
int mx = 0;
for (int i = 0; i < n; i += k) {
mx = Math.max(mx, cnt.merge(word.substring(i, i + k), 1, Integer::sum));
}
return n / k - mx;
}
}
|
3,137
|
Minimum Number of Operations to Make Word K-Periodic
|
Medium
|
<p>You are given a string <code>word</code> of size <code>n</code>, and an integer <code>k</code> such that <code>k</code> divides <code>n</code>.</p>
<p>In one operation, you can pick any two indices <code>i</code> and <code>j</code>, that are divisible by <code>k</code>, then replace the <span data-keyword="substring">substring</span> of length <code>k</code> starting at <code>i</code> with the substring of length <code>k</code> starting at <code>j</code>. That is, replace the substring <code>word[i..i + k - 1]</code> with the substring <code>word[j..j + k - 1]</code>.<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 --></p>
<p>Return <em>the <strong>minimum</strong> number of operations required to make</em> <code>word</code> <em><strong>k-periodic</strong></em>.</p>
<p>We say that <code>word</code> is <strong>k-periodic</strong> if there is some string <code>s</code> of length <code>k</code> such that <code>word</code> can be obtained by concatenating <code>s</code> an arbitrary number of times. For example, if <code>word == “ababab”</code>, then <code>word</code> is 2-periodic for <code>s = "ab"</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "leetcodeleet", k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">1</span></p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "</span>leetcoleet<span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">", k = 2</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 2-periodic string by applying the operations in the table below.</p>
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
<tbody>
<tr>
<th>i</th>
<th>j</th>
<th>word</th>
</tr>
<tr>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">2</td>
<td style="padding: 5px 15px;">etetcoleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">4</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">6</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetetet</td>
</tr>
</tbody>
</table>
</div>
<div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
<div class="gtx-trans-icon"> </div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == word.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= word.length</code></li>
<li><code>k</code> divides <code>word.length</code>.</li>
<li><code>word</code> consists only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Python
|
class Solution:
def minimumOperationsToMakeKPeriodic(self, word: str, k: int) -> int:
n = len(word)
return n // k - max(Counter(word[i : i + k] for i in range(0, n, k)).values())
|
3,137
|
Minimum Number of Operations to Make Word K-Periodic
|
Medium
|
<p>You are given a string <code>word</code> of size <code>n</code>, and an integer <code>k</code> such that <code>k</code> divides <code>n</code>.</p>
<p>In one operation, you can pick any two indices <code>i</code> and <code>j</code>, that are divisible by <code>k</code>, then replace the <span data-keyword="substring">substring</span> of length <code>k</code> starting at <code>i</code> with the substring of length <code>k</code> starting at <code>j</code>. That is, replace the substring <code>word[i..i + k - 1]</code> with the substring <code>word[j..j + k - 1]</code>.<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 --></p>
<p>Return <em>the <strong>minimum</strong> number of operations required to make</em> <code>word</code> <em><strong>k-periodic</strong></em>.</p>
<p>We say that <code>word</code> is <strong>k-periodic</strong> if there is some string <code>s</code> of length <code>k</code> such that <code>word</code> can be obtained by concatenating <code>s</code> an arbitrary number of times. For example, if <code>word == “ababab”</code>, then <code>word</code> is 2-periodic for <code>s = "ab"</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "leetcodeleet", k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">1</span></p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">word = "</span>leetcoleet<span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">", k = 2</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can obtain a 2-periodic string by applying the operations in the table below.</p>
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
<tbody>
<tr>
<th>i</th>
<th>j</th>
<th>word</th>
</tr>
<tr>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">2</td>
<td style="padding: 5px 15px;">etetcoleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">4</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetleet</td>
</tr>
<tr>
<td style="padding: 5px 15px;">6</td>
<td style="padding: 5px 15px;">0</td>
<td style="padding: 5px 15px;">etetetetet</td>
</tr>
</tbody>
</table>
</div>
<div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
<div class="gtx-trans-icon"> </div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == word.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= word.length</code></li>
<li><code>k</code> divides <code>word.length</code>.</li>
<li><code>word</code> consists only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
TypeScript
|
function minimumOperationsToMakeKPeriodic(word: string, k: number): number {
const cnt: Map<string, number> = new Map();
const n: number = word.length;
let mx: number = 0;
for (let i = 0; i < n; i += k) {
const s = word.slice(i, i + k);
cnt.set(s, (cnt.get(s) || 0) + 1);
mx = Math.max(mx, cnt.get(s)!);
}
return Math.floor(n / k) - mx;
}
|
3,138
|
Minimum Length of Anagram Concatenation
|
Medium
|
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p>
<p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p>
<p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abba"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"ba"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cdef"</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"cdef"</code>, notice that <code>t</code> can be equal to <code>s</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcbcacabbaccba"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consist only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
C++
|
class Solution {
public:
int minAnagramLength(string s) {
int n = s.size();
int cnt[26]{};
for (char c : s) {
cnt[c - 'a']++;
}
auto check = [&](int k) {
for (int i = 0; i < n; i += k) {
int cnt1[26]{};
for (int j = i; j < i + k; ++j) {
cnt1[s[j] - 'a']++;
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
};
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}
};
|
3,138
|
Minimum Length of Anagram Concatenation
|
Medium
|
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p>
<p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p>
<p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abba"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"ba"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cdef"</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"cdef"</code>, notice that <code>t</code> can be equal to <code>s</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcbcacabbaccba"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consist only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Go
|
func minAnagramLength(s string) int {
n := len(s)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
check := func(k int) bool {
for i := 0; i < n; i += k {
cnt1 := [26]int{}
for j := i; j < i+k; j++ {
cnt1[s[j]-'a']++
}
for j, v := range cnt {
if cnt1[j]*(n/k) != v {
return false
}
}
}
return true
}
for i := 1; ; i++ {
if n%i == 0 && check(i) {
return i
}
}
}
|
3,138
|
Minimum Length of Anagram Concatenation
|
Medium
|
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p>
<p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p>
<p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abba"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"ba"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cdef"</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"cdef"</code>, notice that <code>t</code> can be equal to <code>s</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcbcacabbaccba"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consist only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Java
|
class Solution {
private int n;
private char[] s;
private int[] cnt = new int[26];
public int minAnagramLength(String s) {
n = s.length();
this.s = s.toCharArray();
for (int i = 0; i < n; ++i) {
++cnt[this.s[i] - 'a'];
}
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}
private boolean check(int k) {
for (int i = 0; i < n; i += k) {
int[] cnt1 = new int[26];
for (int j = i; j < i + k; ++j) {
++cnt1[s[j] - 'a'];
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
}
}
|
3,138
|
Minimum Length of Anagram Concatenation
|
Medium
|
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p>
<p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p>
<p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abba"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"ba"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cdef"</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"cdef"</code>, notice that <code>t</code> can be equal to <code>s</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcbcacabbaccba"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consist only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
Python
|
class Solution:
def minAnagramLength(self, s: str) -> int:
def check(k: int) -> bool:
for i in range(0, n, k):
cnt1 = Counter(s[i : i + k])
for c, v in cnt.items():
if cnt1[c] * (n // k) != v:
return False
return True
cnt = Counter(s)
n = len(s)
for i in range(1, n + 1):
if n % i == 0 and check(i):
return i
|
3,138
|
Minimum Length of Anagram Concatenation
|
Medium
|
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p>
<p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p>
<p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abba"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"ba"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cdef"</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible string <code>t</code> could be <code>"cdef"</code>, notice that <code>t</code> can be equal to <code>s</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcbcacabbaccba"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consist only of lowercase English letters.</li>
</ul>
|
Hash Table; String; Counting
|
TypeScript
|
function minAnagramLength(s: string): number {
const n = s.length;
const cnt: Record<string, number> = {};
for (let i = 0; i < n; i++) {
cnt[s[i]] = (cnt[s[i]] || 0) + 1;
}
const check = (k: number): boolean => {
for (let i = 0; i < n; i += k) {
const cnt1: Record<string, number> = {};
for (let j = i; j < i + k; j++) {
cnt1[s[j]] = (cnt1[s[j]] || 0) + 1;
}
for (const [c, v] of Object.entries(cnt)) {
if (cnt1[c] * ((n / k) | 0) !== v) {
return false;
}
}
}
return true;
};
for (let i = 1; ; ++i) {
if (n % i === 0 && check(i)) {
return i;
}
}
}
|
3,140
|
Consecutive Available Seats II
|
Medium
|
<p>Table: <code>Cinema</code></p>
<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| seat_id | int |
| free | bool |
+-------------+------+
seat_id is an auto-increment column for this table.
Each row of this table indicates whether the i<sup>th</sup> seat is free or not. 1 means free while 0 means occupied.
</pre>
<p>Write a solution to find the <strong>length</strong> of <strong>longest consecutive sequence</strong> of <strong>available</strong> seats in the cinema.</p>
<p>Note:</p>
<ul>
<li>There will always be <strong>at most</strong> <strong>one</strong> longest consecutive sequence.</li>
<li>If there are <strong>multiple</strong> consecutive sequences with the <strong>same length</strong>, include all of them in the output.</li>
</ul>
<p>Return <em>the result table <strong>ordered</strong> by</em> <code>first_seat_id</code> <em><strong>in ascending order</strong></em>.</p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong>Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>Cinema table:</p>
<pre class="example-io">
+---------+------+
| seat_id | free |
+---------+------+
| 1 | 1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
+---------+------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-----------------+----------------+-----------------------+
| first_seat_id | last_seat_id | consecutive_seats_len |
+-----------------+----------------+-----------------------+
| 3 | 5 | 3 |
+-----------------+----------------+-----------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Longest consecutive sequence of available seats starts from seat 3 and ends at seat 5 with a length of 3.</li>
</ul>
Output table is ordered by first_seat_id in ascending order.</div>
|
Database
|
SQL
|
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
seat_id - (RANK() OVER (ORDER BY seat_id)) AS gid
FROM Cinema
WHERE free = 1
),
P AS (
SELECT
MIN(seat_id) AS first_seat_id,
MAX(seat_id) AS last_seat_id,
COUNT(1) AS consecutive_seats_len,
RANK() OVER (ORDER BY COUNT(1) DESC) AS rk
FROM T
GROUP BY gid
)
SELECT first_seat_id, last_seat_id, consecutive_seats_len
FROM P
WHERE rk = 1
ORDER BY 1;
|
3,141
|
Maximum Hamming Distances
|
Hard
|
<p>Given an array <code>nums</code> and an integer <code>m</code>, with each element <code>nums[i]</code> satisfying <code>0 <= nums[i] < 2<sup>m</sup></code>, return an array <code>answer</code>. The <code>answer</code> array should be of the same length as <code>nums</code>, where each element <code>answer[i]</code> represents the <em>maximum</em> <strong>Hamming distance </strong>between <code>nums[i]</code> and any other element <code>nums[j]</code> in the array.</p>
<p>The <strong>Hamming distance</strong> between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,12,9,11], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [1001,1100,1001,1011]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[1]</code>: 1100 and 1011 have a distance of 3.</li>
<li><code>nums[2]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[3]</code>: 1011 and 1100 have a distance of 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,6,10], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [0011,0100,0110,1010]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 0011 and 0100 have a distance of 3.</li>
<li><code>nums[1]</code>: 0100 and 0011 have a distance of 3.</li>
<li><code>nums[2]</code>: 0110 and 1010 have a distance of 2.</li>
<li><code>nums[3]</code>: 1010 and 0100 have a distance of 3.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= m <= 17</code></li>
<li><code>2 <= nums.length <= 2<sup>m</sup></code></li>
<li><code>0 <= nums[i] < 2<sup>m</sup></code></li>
</ul>
|
Bit Manipulation; Breadth-First Search; Array
|
C++
|
class Solution {
public:
vector<int> maxHammingDistances(vector<int>& nums, int m) {
int dist[1 << m];
memset(dist, -1, sizeof(dist));
queue<int> q;
for (int x : nums) {
dist[x] = 0;
q.push(x);
}
for (int k = 1; q.size(); ++k) {
for (int t = q.size(); t; --t) {
int x = q.front();
q.pop();
for (int i = 0; i < m; ++i) {
int y = x ^ (1 << i);
if (dist[y] == -1) {
dist[y] = k;
q.push(y);
}
}
}
}
for (int& x : nums) {
x = m - dist[x ^ ((1 << m) - 1)];
}
return nums;
}
};
|
3,141
|
Maximum Hamming Distances
|
Hard
|
<p>Given an array <code>nums</code> and an integer <code>m</code>, with each element <code>nums[i]</code> satisfying <code>0 <= nums[i] < 2<sup>m</sup></code>, return an array <code>answer</code>. The <code>answer</code> array should be of the same length as <code>nums</code>, where each element <code>answer[i]</code> represents the <em>maximum</em> <strong>Hamming distance </strong>between <code>nums[i]</code> and any other element <code>nums[j]</code> in the array.</p>
<p>The <strong>Hamming distance</strong> between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,12,9,11], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [1001,1100,1001,1011]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[1]</code>: 1100 and 1011 have a distance of 3.</li>
<li><code>nums[2]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[3]</code>: 1011 and 1100 have a distance of 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,6,10], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [0011,0100,0110,1010]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 0011 and 0100 have a distance of 3.</li>
<li><code>nums[1]</code>: 0100 and 0011 have a distance of 3.</li>
<li><code>nums[2]</code>: 0110 and 1010 have a distance of 2.</li>
<li><code>nums[3]</code>: 1010 and 0100 have a distance of 3.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= m <= 17</code></li>
<li><code>2 <= nums.length <= 2<sup>m</sup></code></li>
<li><code>0 <= nums[i] < 2<sup>m</sup></code></li>
</ul>
|
Bit Manipulation; Breadth-First Search; Array
|
Go
|
func maxHammingDistances(nums []int, m int) []int {
dist := make([]int, 1<<m)
for i := range dist {
dist[i] = -1
}
q := []int{}
for _, x := range nums {
dist[x] = 0
q = append(q, x)
}
for k := 1; len(q) > 0; k++ {
t := []int{}
for _, x := range q {
for i := 0; i < m; i++ {
y := x ^ (1 << i)
if dist[y] == -1 {
dist[y] = k
t = append(t, y)
}
}
}
q = t
}
for i, x := range nums {
nums[i] = m - dist[x^(1<<m-1)]
}
return nums
}
|
3,141
|
Maximum Hamming Distances
|
Hard
|
<p>Given an array <code>nums</code> and an integer <code>m</code>, with each element <code>nums[i]</code> satisfying <code>0 <= nums[i] < 2<sup>m</sup></code>, return an array <code>answer</code>. The <code>answer</code> array should be of the same length as <code>nums</code>, where each element <code>answer[i]</code> represents the <em>maximum</em> <strong>Hamming distance </strong>between <code>nums[i]</code> and any other element <code>nums[j]</code> in the array.</p>
<p>The <strong>Hamming distance</strong> between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,12,9,11], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [1001,1100,1001,1011]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[1]</code>: 1100 and 1011 have a distance of 3.</li>
<li><code>nums[2]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[3]</code>: 1011 and 1100 have a distance of 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,6,10], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [0011,0100,0110,1010]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 0011 and 0100 have a distance of 3.</li>
<li><code>nums[1]</code>: 0100 and 0011 have a distance of 3.</li>
<li><code>nums[2]</code>: 0110 and 1010 have a distance of 2.</li>
<li><code>nums[3]</code>: 1010 and 0100 have a distance of 3.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= m <= 17</code></li>
<li><code>2 <= nums.length <= 2<sup>m</sup></code></li>
<li><code>0 <= nums[i] < 2<sup>m</sup></code></li>
</ul>
|
Bit Manipulation; Breadth-First Search; Array
|
Java
|
class Solution {
public int[] maxHammingDistances(int[] nums, int m) {
int[] dist = new int[1 << m];
Arrays.fill(dist, -1);
Deque<Integer> q = new ArrayDeque<>();
for (int x : nums) {
dist[x] = 0;
q.offer(x);
}
for (int k = 1; !q.isEmpty(); ++k) {
for (int t = q.size(); t > 0; --t) {
int x = q.poll();
for (int i = 0; i < m; ++i) {
int y = x ^ (1 << i);
if (dist[y] == -1) {
q.offer(y);
dist[y] = k;
}
}
}
}
for (int i = 0; i < nums.length; ++i) {
nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
}
return nums;
}
}
|
3,141
|
Maximum Hamming Distances
|
Hard
|
<p>Given an array <code>nums</code> and an integer <code>m</code>, with each element <code>nums[i]</code> satisfying <code>0 <= nums[i] < 2<sup>m</sup></code>, return an array <code>answer</code>. The <code>answer</code> array should be of the same length as <code>nums</code>, where each element <code>answer[i]</code> represents the <em>maximum</em> <strong>Hamming distance </strong>between <code>nums[i]</code> and any other element <code>nums[j]</code> in the array.</p>
<p>The <strong>Hamming distance</strong> between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,12,9,11], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [1001,1100,1001,1011]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[1]</code>: 1100 and 1011 have a distance of 3.</li>
<li><code>nums[2]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[3]</code>: 1011 and 1100 have a distance of 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,6,10], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [0011,0100,0110,1010]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 0011 and 0100 have a distance of 3.</li>
<li><code>nums[1]</code>: 0100 and 0011 have a distance of 3.</li>
<li><code>nums[2]</code>: 0110 and 1010 have a distance of 2.</li>
<li><code>nums[3]</code>: 1010 and 0100 have a distance of 3.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= m <= 17</code></li>
<li><code>2 <= nums.length <= 2<sup>m</sup></code></li>
<li><code>0 <= nums[i] < 2<sup>m</sup></code></li>
</ul>
|
Bit Manipulation; Breadth-First Search; Array
|
Python
|
class Solution:
def maxHammingDistances(self, nums: List[int], m: int) -> List[int]:
dist = [-1] * (1 << m)
for x in nums:
dist[x] = 0
q = nums
k = 1
while q:
t = []
for x in q:
for i in range(m):
y = x ^ (1 << i)
if dist[y] == -1:
t.append(y)
dist[y] = k
q = t
k += 1
return [m - dist[x ^ ((1 << m) - 1)] for x in nums]
|
3,141
|
Maximum Hamming Distances
|
Hard
|
<p>Given an array <code>nums</code> and an integer <code>m</code>, with each element <code>nums[i]</code> satisfying <code>0 <= nums[i] < 2<sup>m</sup></code>, return an array <code>answer</code>. The <code>answer</code> array should be of the same length as <code>nums</code>, where each element <code>answer[i]</code> represents the <em>maximum</em> <strong>Hamming distance </strong>between <code>nums[i]</code> and any other element <code>nums[j]</code> in the array.</p>
<p>The <strong>Hamming distance</strong> between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,12,9,11], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [1001,1100,1001,1011]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[1]</code>: 1100 and 1011 have a distance of 3.</li>
<li><code>nums[2]</code>: 1001 and 1100 have a distance of 2.</li>
<li><code>nums[3]</code>: 1011 and 1100 have a distance of 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,4,6,10], m = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of <code>nums = [0011,0100,0110,1010]</code>.</p>
<p>The maximum hamming distances for each index are:</p>
<ul>
<li><code>nums[0]</code>: 0011 and 0100 have a distance of 3.</li>
<li><code>nums[1]</code>: 0100 and 0011 have a distance of 3.</li>
<li><code>nums[2]</code>: 0110 and 1010 have a distance of 2.</li>
<li><code>nums[3]</code>: 1010 and 0100 have a distance of 3.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= m <= 17</code></li>
<li><code>2 <= nums.length <= 2<sup>m</sup></code></li>
<li><code>0 <= nums[i] < 2<sup>m</sup></code></li>
</ul>
|
Bit Manipulation; Breadth-First Search; Array
|
TypeScript
|
function maxHammingDistances(nums: number[], m: number): number[] {
const dist: number[] = Array.from({ length: 1 << m }, () => -1);
const q: number[] = [];
for (const x of nums) {
dist[x] = 0;
q.push(x);
}
for (let k = 1; q.length; ++k) {
const t: number[] = [];
for (const x of q) {
for (let i = 0; i < m; ++i) {
const y = x ^ (1 << i);
if (dist[y] === -1) {
dist[y] = k;
t.push(y);
}
}
}
q.splice(0, q.length, ...t);
}
for (let i = 0; i < nums.length; ++i) {
nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
}
return nums;
}
|
3,142
|
Check if Grid Satisfies Conditions
|
Easy
|
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. You need to check if each cell <code>grid[i][j]</code> is:</p>
<ul>
<li>Equal to the cell below it, i.e. <code>grid[i][j] == grid[i + 1][j]</code> (if it exists).</li>
<li>Different from the cell to its right, i.e. <code>grid[i][j] != grid[i][j + 1]</code> (if it exists).</li>
</ul>
<p>Return <code>true</code> if <strong>all</strong> the cells satisfy these conditions, otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,2],[1,0,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3142.Check%20if%20Grid%20Satisfies%20Conditions/images/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All the cells in the grid satisfy the conditions.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[0,0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3142.Check%20if%20Grid%20Satisfies%20Conditions/images/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
<p>All cells in the first row are equal.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[2],[3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3142.Check%20if%20Grid%20Satisfies%20Conditions/images/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
<p>Cells in the first column have different values.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n, m <= 10</code></li>
<li><code>0 <= grid[i][j] <= 9</code></li>
</ul>
|
Array; Matrix
|
C++
|
class Solution {
public:
bool satisfiesConditions(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
return false;
}
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
return false;
}
}
}
return true;
}
};
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.