Skip to content

Commit 91f97cf

Browse files
committed
Solve #299 & #300
1 parent a85e98d commit 91f97cf

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,5 @@ mod n0289_game_of_life;
222222
mod n0290_word_pattern;
223223
mod n0292_nim_game;
224224
mod n0295_find_median_from_data_stream;
225+
mod n0299_bulls_and_cows;
226+
mod n0300_longest_increasing_subsequence;

src/n0299_bulls_and_cows.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* [299] Bulls and Cows
3+
*
4+
* You are playing the following <a href="https://en.wikipedia.org/wiki/Bulls_and_Cows" target="_blank">Bulls and Cows</a> game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
5+
*
6+
* Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows.
7+
*
8+
* Please note that both secret number and friend's guess may contain duplicate digits.
9+
*
10+
* Example 1:
11+
*
12+
*
13+
* Input: secret = "1807", guess = "7810"
14+
*
15+
* Output: "1A3B"
16+
*
17+
* Explanation: 1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> bull and </span>3<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> cows. The bull is </span>8<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, the cows are </span>0<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, </span>1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> and </span>7<font face="sans-serif, Arial, Verdana, Trebuchet MS">.</font>
18+
*
19+
* Example 2:
20+
*
21+
*
22+
* Input: secret = "1123", guess = "0111"
23+
*
24+
* Output: "1A1B"
25+
*
26+
* Explanation: The 1st 1 <span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">in friend's guess is a bull, the 2nd or 3rd </span>1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> is a cow</span><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span>
27+
*
28+
* Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
29+
*/
30+
pub struct Solution {}
31+
32+
// submission codes start here
33+
34+
impl Solution {
35+
pub fn get_hint(secret: String, guess: String) -> String {
36+
let mut bulls = 0;
37+
let mut cows = 0;
38+
let mut s_iter = secret.chars();
39+
let mut g_iter = guess.chars();
40+
let mut bucket = vec![0; 10];
41+
let mut non_match = vec![];
42+
while let (Some(s), Some(g)) = (s_iter.next(), g_iter.next()) {
43+
if s == g {
44+
bulls += 1;
45+
} else {
46+
bucket[s.to_digit(10).unwrap() as usize] += 1;
47+
non_match.push(g.to_digit(10).unwrap() as usize);
48+
}
49+
}
50+
for &idx in non_match.iter() {
51+
if bucket[idx] > 0 {
52+
cows += 1;
53+
bucket[idx] -= 1;
54+
}
55+
}
56+
format!("{}A{}B", bulls, cows)
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_299() {
68+
assert_eq!(Solution::get_hint("1807".to_owned(), "7810".to_owned()), "1A3B".to_owned());
69+
assert_eq!(Solution::get_hint("1123".to_owned(), "0111".to_owned()), "1A1B".to_owned());
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* [300] Longest Increasing Subsequence
3+
*
4+
* Given an unsorted array of integers, find the length of longest increasing subsequence.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: [10,9,2,5,3,7,101,18]
10+
* Output: 4
11+
* Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
12+
*
13+
* Note:
14+
*
15+
*
16+
* There may be more than one LIS combination, it is only necessary for you to return the length.
17+
* Your algorithm should run in O(n^2) complexity.
18+
*
19+
*
20+
* Follow up: Could you improve it to O(n log n) time complexity?
21+
*
22+
*/
23+
pub struct Solution {}
24+
25+
// submission codes start here
26+
// N^2, DP: L[i] = max(1 + L[j]) for j in [0, i) and nums[j] < nums[i]
27+
// N * logN, kick out strategy, maintain an increasing array, new elements kick out a formal one larger than it, if the new element is largest, expand the array
28+
impl Solution {
29+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
30+
let mut incr = Vec::new();
31+
for &num in nums.iter() {
32+
if let Err(idx) = incr.binary_search(&num) {
33+
println!("{:?}", idx);
34+
if idx >= incr.len() {
35+
incr.push(num);
36+
} else {
37+
incr[idx] = num;
38+
}
39+
}
40+
}
41+
println!("{:?}", incr);
42+
incr.len() as i32
43+
}
44+
}
45+
46+
// submission codes end
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
52+
#[test]
53+
fn test_300() {
54+
assert_eq!(Solution::length_of_lis(vec![10,9,2,5,3,7,101,18]), 4);
55+
}
56+
}

0 commit comments

Comments
 (0)