Skip to content

Commit 754ca6c

Browse files
add divede and conquer
1 parent c232404 commit 754ca6c

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

src/solution/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ mod s0297_serialize_and_deserialize_binary_tree;
2727
mod s0461_hamming_distance;
2828
mod s0508_most_frequent_subtree_sum;
2929
mod s1035_uncrossed_lines;
30+
mod s0169_majority_element;
31+
mod s0153_find_minimum_in_rotated_sorted_array;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* [153] Find Minimum in Rotated Sorted Array
3+
*
4+
* Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
5+
*
6+
* [4,5,6,7,0,1,2] if it was rotated 4 times.
7+
* [0,1,2,4,5,6,7] if it was rotated 7 times.
8+
*
9+
* Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
10+
* Given the sorted rotated array nums of unique elements, return the minimum element of this array.
11+
* You must write an algorithm that runs in O(log n) time.
12+
*
13+
* Example 1:
14+
*
15+
* Input: nums = [3,4,5,1,2]
16+
* Output: 1
17+
* Explanation: The original array was [1,2,3,4,5] rotated 3 times.
18+
*
19+
* Example 2:
20+
*
21+
* Input: nums = [4,5,6,7,0,1,2]
22+
* Output: 0
23+
* Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
24+
*
25+
* Example 3:
26+
*
27+
* Input: nums = [11,13,15,17]
28+
* Output: 11
29+
* Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* n == nums.length
35+
* 1 <= n <= 5000
36+
* -5000 <= nums[i] <= 5000
37+
* All the integers of nums are unique.
38+
* nums is sorted and rotated between 1 and n times.
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
44+
// discuss: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn find_min(nums: Vec<i32>) -> i32 {
50+
let mut left = 0;
51+
let mut right = nums.len() - 1;
52+
let mut mid = 0;
53+
54+
while (left < right) {
55+
mid = (right - left) / 2 + left;
56+
57+
if (nums[mid] < nums[right]) {
58+
right = mid;
59+
} else {
60+
left = mid + 1;
61+
}
62+
}
63+
return nums[left];
64+
}
65+
}
66+
67+
// submission codes end
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test_153() {
75+
assert_eq!(Solution::find_min(vec![1, 2, 3, 0]), 0);
76+
}
77+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* [169] Majority Element
4+
*
5+
* Given an array nums of size n, return the majority element.
6+
* The majority element is the element that appears more than &lfloor;n / 2&rfloor; times. You may assume that the majority element always exists in the array.
7+
*
8+
* Example 1:
9+
* Input: nums = [3,2,3]
10+
* Output: 3
11+
* Example 2:
12+
* Input: nums = [2,2,1,1,1,2,2]
13+
* Output: 2
14+
*
15+
* Constraints:
16+
*
17+
* n == nums.length
18+
* 1 <= n <= 5 * 10^4
19+
* -2^31 <= nums[i] <= 2^31 - 1
20+
*
21+
*
22+
* Follow-up: Could you solve the problem in linear time and in O(1) space?
23+
*/
24+
pub struct Solution {}
25+
26+
// problem: https://leetcode.com/problems/majority-element/
27+
// discuss: https://leetcode.com/problems/majority-element/discuss/?currentPage=1&orderBy=most_votes&query=
28+
29+
// submission codes start here
30+
31+
use std::collections::HashMap;
32+
33+
impl Solution {
34+
pub fn majority_element(nums: Vec<i32>) -> i32 {
35+
let target = nums.len() / 2;
36+
let mut h = HashMap::new();
37+
for e in nums.iter() {
38+
let count = h.entry(e).or_insert(0);
39+
*count += 1;
40+
if *count > target {
41+
return *e;
42+
}
43+
}
44+
return 0;
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
fn test_169() {
56+
assert_eq!(Solution::majority_element(vec![2,2,1,1,1,2,2]), 2);
57+
assert_eq!(Solution::majority_element(vec![2,2,3]), 2);
58+
}
59+
}

0 commit comments

Comments
 (0)