Let’s draw some pictures to understand this quesiton. If there are 4 piles of bananas like below, the given array would be [3, 2, 4, 1].
Then if Koko can eat 3 bananas per hour, it will takes her 5 hours to eat all the bananas like blow.
Now, if the limit hours h is 5 then 3 bananas per hour satisfies. However, if h, for example, is 3, then Koko need to SPEEDS UP to finish all the bananas. On the other hand, if h is 10 which means we still have time right? So Koko should SLOW DOWN to certain speed to make herself chill but still can finish all the bananas in time.
So what is the best eating speed is the question!
Thoughts
The maximum of speed should equals to the number of the bananas of the tallest pile
The minimun of speed should equals to 1
Which means we are trying to find the best from 1 ~ piles.max(). Searching a value from a sorted array… maybe binary search is a soslution.
// find the matched speed or continue go down to sub tree match time.cmp(&h) { // spend too much time to eat, speed up Ordering::Greater => Solution::min_eating_speed_inner(piles, h, mid + 1, max), // match, try to find a better (slower) speed _ => { letnew_speed = Solution::min_eating_speed_inner(piles, h, min, mid - 1);
if new_speed > 0 { return new_speed; } mid } } }
fncount_eating_time(piles: &Vec<i32>, speed: i32) ->i32 { // count the time to eat each pile and return sum letsum = piles .into_iter() .map(|pile| { if pile % speed > 0 { return pile / speed + 1 } return pile / speed }) .sum();
The core concept is to find the target from a sorted array which is quite suitable to use binary serach I think.
Here are my instructions:
Get the middle element, mid
Compare the mid with the target. there will be 3 branches: a. target < mid: continue to find the target from the LEFT side of the mid b. target > mid: continue to find the target from the RIGHT side of the mid c. target = mid: In this case, [index_of_mid, index_of_mid] will be our base answer so keep it. Then based on this answer, we need to go further to the both left and right to seek if there is other elements match to the search target.
For example, if the given array is [1, 2, 3, 4, 4, 4, 4, 6, 9], and the search target is 4.
Then we are trying to get this green range.
When we go through the instructions, we will get the middle element, and hit the c case.
Then base on this position, we seek towar left and right to get the final range.
To prevent it, we can use the combo of checked_sub and unwrap_or. checked_sub will return Option<self> which means if there is no overflow occurs, we get Some(substraction_result), else we will get None. unwarp_or can then give the result a default value if we get None or just unwrap Some(substraction_result) to return substraction_result. Let’s look at the code.
num_i32 = num_i32.checked_sub(1).unwrap_or(100); // 100 // Since it overflows, it will return the default value // that we give to unwrap_or which is 100 for this example
$ spl-token mint 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR 1
注意,這裏其實你也可以 mint 1 個以上,也可以成功,但這樣這個 token 總量就會有 1 個以上,就不是所謂的 NFT 了。
成功 mint 1 個代幣後,我們馬上關閉該代幣的鑄造功能。
這個關閉事不能再打開的,如此就達成意義上的 NFT 了,每個 token 都是獨一無二。
命令:
1
$ spl-token authorize <代幣 ID> mint --disable
執行結果:
1 2 3 4 5 6
$ spl-token authorize 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR mint --disable Updating 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR Current mint authority: 46hytJBhguswo6S8fCcVtR85HEnb9nd1hwMxFWYnSHXc New mint authority: disabled
You may want to delegate stake to additional validators while your existing stake is not eligible for withdrawal. It might not be eligible because it is currently staked, cooling down, or locked up. To transfer tokens from an existing stake account to a new one
// Push string str.push_str("ust"); // capacity is doubled to 12 println!("Now str: {}", str);
// Check if empty println!("Is empty: {}", str.is_empty());
// Check if contains println!("Contains 'PHP'? {}", str.contains("PHP"));
// Replace substring with println!("Replace output: {}", str.replace("Rust", "Solana")); // replace is not a in-place operation // so value of str is still "Hello Rust"
// Iterate a string by word (whitespace) forwordinstr.split_whitespace() { println!("{}", word); }
// Create a string with capacity letstr_with_cap_10 = String::with_capacity(10); println!("Length: {} Capacity: {}", str_with_cap_10.len(), str_with_cap_10.capacity()); }
The above outputs the following:
1 2 3 4 5 6 7 8 9 10
Now str: Hello Length: 6 Capacity: 6 Now str: Hello R Now str: Hello Rust Is empty: false Contains 'PHP'? false Replace output: Hello Solana Hello Rust Length: 0 Capacity: 10
Use “+” to append string
1 2 3 4
lets1 = String::from("Hello"); lets2 = String::from(" Rust"); lets3 = s1 + &s2; // Note: s1 is no longer valid since it's ownership is moved to s3 // s3 = "Hello Rust"
// Placeholder for debug trait println!("{:?}", (100, false, "Rust")); }
The above outputs the following:
1 2 3 4 5 6
Hello Rust Rust is rusty Rust is a programming language, and Rust is efficient Rust supports concurrent programming Binary: 1010 Hex: a Octal: 12 (100, false, "Rust")