Skip to content

Commit e5bde38

Browse files
daily
1 parent db5d393 commit e5bde38

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

src/solution/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ mod s0986_interval_list_intersections;
4343
mod s0855_exam_room;
4444
mod s0881_boats_to_save_people;
4545
mod s0016_3sum_closest;
46+
mod s0977_squares_of_a_sorted_array;
47+
mod s1646_get_maximum_in_generated_array;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* [977] Squares of a Sorted Array
3+
*
4+
* Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
5+
*
6+
* Example 1:
7+
*
8+
* Input: nums = [-4,-1,0,3,10]
9+
* Output: [0,1,9,16,100]
10+
* Explanation: After squaring, the array becomes [16,1,0,9,100].
11+
* After sorting, it becomes [0,1,9,16,100].
12+
*
13+
* Example 2:
14+
*
15+
* Input: nums = [-7,-3,2,3,11]
16+
* Output: [4,9,9,49,121]
17+
*
18+
*
19+
* Constraints:
20+
*
21+
* <span>1 <= nums.length <= </span>10^4
22+
* -10^4 <= nums[i] <= 10^4
23+
* nums is sorted in non-decreasing order.
24+
*
25+
*
26+
* Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
27+
*/
28+
pub struct Solution {}
29+
30+
// problem: https://leetcode.com/problems/squares-of-a-sorted-array/
31+
// discuss: https://leetcode.com/problems/squares-of-a-sorted-array/discuss/?currentPage=1&orderBy=most_votes&query=
32+
33+
// submission codes start here
34+
35+
impl Solution {
36+
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
37+
let mut res = vec![0; nums.len()];
38+
let mut i = 0;
39+
let mut j = nums.len() - 1;
40+
let mut k = nums.len();
41+
42+
while i < nums.len() && i <= j {
43+
k -= 1;
44+
if i32::abs(nums[i]) >= i32::abs(nums[j]) {
45+
res[k] = nums[i] * nums[i];
46+
i += 1;
47+
} else {
48+
res[k] = nums[j] * nums[j];
49+
j -= 1
50+
}
51+
}
52+
53+
res
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_977() {
65+
let nums = vec![-4];
66+
let res = Solution::sorted_squares(nums);
67+
println!("res is {:?}", res);
68+
}
69+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* [1646] Get Maximum in Generated Array
3+
*
4+
* You are given an integer n. An array nums of length n + 1 is generated in the following way:
5+
*
6+
* nums[0] = 0
7+
* nums[1] = 1
8+
* nums[2 * i] = nums[i] when 2 <= 2 * i <= n
9+
* nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n
10+
*
11+
* Return the maximum integer in the array nums​​​.
12+
*
13+
* Example 1:
14+
*
15+
* Input: n = 7
16+
* Output: 3
17+
* Explanation: According to the given rules:
18+
* nums[0] = 0
19+
* nums[1] = 1
20+
* nums[(1 * 2) = 2] = nums[1] = 1
21+
* nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
22+
* nums[(2 * 2) = 4] = nums[2] = 1
23+
* nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
24+
* nums[(3 * 2) = 6] = nums[3] = 2
25+
* nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
26+
* Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.
27+
*
28+
* Example 2:
29+
*
30+
* Input: n = 2
31+
* Output: 1
32+
* Explanation: According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.
33+
*
34+
* Example 3:
35+
*
36+
* Input: n = 3
37+
* Output: 2
38+
* Explanation: According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.
39+
*
40+
*
41+
* Constraints:
42+
*
43+
* 0 <= n <= 100
44+
*
45+
*/
46+
pub struct Solution {}
47+
48+
// problem: https://leetcode.com/problems/get-maximum-in-generated-array/
49+
// discuss: https://leetcode.com/problems/get-maximum-in-generated-array/discuss/?currentPage=1&orderBy=most_votes&query=
50+
51+
// submission codes start here
52+
53+
impl Solution {
54+
pub fn get_maximum_generated(n: i32) -> i32 {
55+
if n == 0 {
56+
return 0;
57+
}
58+
if n == 1 {
59+
return 1;
60+
}
61+
if n == 2 {
62+
return 1;
63+
}
64+
let mut arr = vec![0; n as usize + 1];
65+
arr[0] = 0;
66+
arr[1] = 1;
67+
let mut res = 0;
68+
for i in 2..=n as usize {
69+
if i % 2 == 1 {
70+
arr[i] = arr[(i - 1) / 2] + arr[(i - 1) / 2 + 1];
71+
} else {
72+
arr[i] = arr[i / 2];
73+
}
74+
res = std::cmp::max(res, arr[i]);
75+
}
76+
res
77+
}
78+
}
79+
80+
// submission codes end
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn test_1646() {
88+
assert_eq!(Solution::get_maximum_generated(7), 3);
89+
assert_eq!(Solution::get_maximum_generated(3), 2);
90+
}
91+
}

0 commit comments

Comments
 (0)