-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::Solution; | ||
|
||
impl Solution { | ||
#[allow(dead_code)] | ||
pub fn find_content_children(g: Vec<i32>, s: Vec<i32>) -> i32 { | ||
if s.is_empty() { | ||
return -0; | ||
} | ||
|
||
let mut count: i32 = 0; | ||
|
||
let mut a: Vec<i32> = g; | ||
a.sort(); | ||
|
||
let mut b: Vec<i32> = s; | ||
b.sort(); | ||
|
||
let mut p = 0; | ||
let mut q = 0; | ||
|
||
while p < a.len() && q < b.len() { | ||
if b[q] >= a[p] { | ||
count += 1; | ||
q += 1; | ||
p += 1; | ||
} else { | ||
q += 1; | ||
} | ||
} | ||
|
||
count | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_one() { | ||
assert_eq!( | ||
Solution::find_content_children(vec![1, 2, 3], vec![1, 1]), | ||
1 | ||
); | ||
} | ||
#[test] | ||
fn test_two() { | ||
assert_eq!( | ||
Solution::find_content_children(vec![1, 2], vec![1, 2, 3]), | ||
2 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
mod assign_cookies_455; | ||
mod container_with_most_water_11; | ||
mod find_range_in_array_34; | ||
mod is_subsquence_392; | ||
mod minimum_common_value_2540; | ||
mod move_zeroes_283; | ||
mod remove_element_27; | ||
mod reverse_string_344; | ||
|
||
pub(crate) struct Solution; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Problem: https://leetcode.com/problems/assign-cookies/ | ||
* @param g - greedy array | ||
* @param s - cookie array | ||
* @returns count | ||
* | ||
*/ | ||
export const findContentChildren = (g: number[], s: number[]): number => { | ||
if (s.length === 0) return 0 | ||
|
||
let count = 0 | ||
|
||
const a = g.sort((a, b) => a - b) | ||
const b = s.sort((a, b) => a - b) | ||
|
||
let p = 0 | ||
let q = 0 | ||
|
||
while (p < a.length && q < b.length) { | ||
if (b[q] >= a[p]) { | ||
count++ | ||
p++ | ||
q++ | ||
} else { | ||
q++ | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { test, expect } = import.meta.vitest | ||
test('test one', () => { | ||
expect(findContentChildren([1, 2, 3], [1, 1])).toBe(1) | ||
}) | ||
test('test two', () => { | ||
expect(findContentChildren([1, 2], [1, 2, 3])).toBe(2) | ||
}) | ||
} |