Skip to content

Commit 56b0de0

Browse files
Add solutions for Valid Sudoku, Minimum Number of People to Teach, Maximum Number of Processable Queries, Count Key Changes, and Maximum Length of Subset
1 parent ebfebc1 commit 56b0de0

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public bool IsValidSudoku(char[][] board) {
3+
bool[,] row = new bool[9, 9];
4+
bool[,] col = new bool[9, 9];
5+
bool[,] sub = new bool[9, 9];
6+
7+
for (int i = 0; i < 9; i++) {
8+
for (int j = 0; j < 9; j++) {
9+
char c = board[i][j];
10+
if (c == '.') {
11+
continue;
12+
}
13+
int num = c - '0' - 1;
14+
int k = (i / 3) * 3 + (j / 3);
15+
if (row[i, num] || col[j, num] || sub[k, num]) {
16+
return false;
17+
}
18+
row[i, num] = true;
19+
col[j, num] = true;
20+
sub[k, num] = true;
21+
}
22+
}
23+
return true;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
3+
let mut row = vec![vec![false; 9]; 9];
4+
let mut col = vec![vec![false; 9]; 9];
5+
let mut sub = vec![vec![false; 9]; 9];
6+
7+
for i in 0..9 {
8+
for j in 0..9 {
9+
let c = board[i][j];
10+
if c == '.' {
11+
continue;
12+
}
13+
let num = (c as u8 - b'0' - 1) as usize;
14+
let k = i / 3 * 3 + j / 3;
15+
if row[i][num] || col[j][num] || sub[k][num] {
16+
return false;
17+
}
18+
row[i][num] = true;
19+
col[j][num] = true;
20+
sub[k][num] = true;
21+
}
22+
}
23+
true
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
impl Solution {
4+
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 {
5+
fn check(u: usize, v: usize, languages: &Vec<Vec<i32>>) -> bool {
6+
for &x in &languages[u - 1] {
7+
for &y in &languages[v - 1] {
8+
if x == y {
9+
return true;
10+
}
11+
}
12+
}
13+
false
14+
}
15+
16+
let mut s: HashSet<usize> = HashSet::new();
17+
for edge in friendships.iter() {
18+
let u = edge[0] as usize;
19+
let v = edge[1] as usize;
20+
if !check(u, v, &languages) {
21+
s.insert(u);
22+
s.insert(v);
23+
}
24+
}
25+
26+
let mut cnt: HashMap<i32, i32> = HashMap::new();
27+
for &u in s.iter() {
28+
for &l in &languages[u - 1] {
29+
*cnt.entry(l).or_insert(0) += 1;
30+
}
31+
}
32+
33+
let mx = cnt.values().cloned().max().unwrap_or(0);
34+
(s.len() as i32) - mx
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function minimumTeachings(n: number, languages: number[][], friendships: number[][]): number {
2+
function check(u: number, v: number): boolean {
3+
for (const x of languages[u - 1]) {
4+
for (const y of languages[v - 1]) {
5+
if (x === y) {
6+
return true;
7+
}
8+
}
9+
}
10+
return false;
11+
}
12+
13+
const s = new Set<number>();
14+
for (const [u, v] of friendships) {
15+
if (!check(u, v)) {
16+
s.add(u);
17+
s.add(v);
18+
}
19+
}
20+
21+
const cnt = new Map<number, number>();
22+
for (const u of s) {
23+
for (const l of languages[u - 1]) {
24+
cnt.set(l, (cnt.get(l) || 0) + 1);
25+
}
26+
}
27+
28+
return s.size - Math.max(0, ...cnt.values());
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
impl Solution {
2+
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let m = queries.len();
5+
let mut f = vec![vec![0; n]; n];
6+
7+
for i in 0..n {
8+
for j in (i..n).rev() {
9+
if i > 0 {
10+
let idx = f[i - 1][j] as usize;
11+
if idx < m {
12+
f[i][j] = f[i][j]
13+
.max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 });
14+
}
15+
}
16+
if j + 1 < n {
17+
let idx = f[i][j + 1] as usize;
18+
if idx < m {
19+
f[i][j] = f[i][j]
20+
.max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 });
21+
}
22+
}
23+
if f[i][j] as usize == m {
24+
return m as i32;
25+
}
26+
}
27+
}
28+
29+
let mut ans = 0;
30+
for i in 0..n {
31+
let idx = f[i][i] as usize;
32+
if idx < m {
33+
ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 });
34+
} else {
35+
ans = ans.max(f[i][i]);
36+
}
37+
}
38+
39+
ans
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn count_key_changes(s: String) -> i32 {
3+
let s = s.to_lowercase();
4+
let bytes = s.as_bytes();
5+
let mut ans = 0;
6+
for i in 1..bytes.len() {
7+
if bytes[i] != bytes[i - 1] {
8+
ans += 1;
9+
}
10+
}
11+
ans
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
5+
let mut cnt: HashMap<i64, i32> = HashMap::new();
6+
for &x in &nums {
7+
*cnt.entry(x as i64).or_insert(0) += 1;
8+
}
9+
10+
let mut ans = 0;
11+
if let Some(t) = cnt.remove(&1) {
12+
ans = t - ((t % 2) ^ 1);
13+
}
14+
15+
for &key in cnt.keys() {
16+
let mut x = key;
17+
let mut t = 0;
18+
while *cnt.get(&x).unwrap_or(&0) > 1 {
19+
x = x * x;
20+
t += 2;
21+
}
22+
t += cnt.get(&x).unwrap_or(&-1);
23+
ans = ans.max(t);
24+
}
25+
26+
ans
27+
}
28+
}

0 commit comments

Comments
 (0)