Understand the definition of balanced, and you will find that we only need to find positions where the counts of L and R are equal.
Time complexity: O(N), Space complexity: O(1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
implSolution { pubfnbalanced_string_split(s: String) ->i32 { letmut ans = 0; letmut l = 0; letmut r = 0; forcin s.chars() { match c { 'L' => { l += 1}, 'R' => { r += 1}, _ => (), } if l == r { ans += 1; } } ans } }
1222. Queens That Can Attack the King
Although understanding the problem is slightly more complicated, the algorithm is actually very direct: search outward from the King, rather than starting from the queens.
Time complexity: O(queens.len() + chessboard.len()), space complexity: O(queens.len()).
pubfnanswer( mut memo: &mut HashMap<(i32, usize, i32), i32>, roll_max: &Vec<i32>, n: i32, previous_number: usize, previous_consecutive: i32, ) ->i32 { if n == 0 { return1; } match memo.get(&(n, previous_number, previous_consecutive)) { Some(value) => { return *value; } None => { letmut ans = 0; foriin0..6 { if i == previous_number { if previous_consecutive == roll_max[previous_number] { continue; } else { ans = (ans + Solution::answer(&mut memo, &roll_max, n - 1, i, previous_consecutive + 1)) % Solution::MOD_NUMBER; } } else { ans = (ans + Solution::answer(&mut memo, &roll_max, n - 1, i, 1)) % Solution::MOD_NUMBER; } } memo.insert((n, previous_number, previous_consecutive), ans); return ans; } } } pubfndie_simulator(n: i32, roll_max: Vec<i32>) ->i32 { letmut memo: HashMap<(i32, usize, i32), i32> = HashMap::new(); Solution::answer(&mut memo, &roll_max, n, 0, 0) } }
1224. Maximum Equal Frequency
First clarify the meaning of an array prefix of nums, namely a prefix array. Then, seeing the bolded exactly one, we know that there are two possible valid prefix arrays:
The count of the number with the highest occurrence count minus 1 equals the occurrence count of all other numbers.
One number has occurrence count 1, and all other numbers have the same occurrence count. Finally, looking at the data size 10^5, we can basically determine that an O(N * log N) or O(N) algorithm is required.
The algorithm is almost obvious. It is natural to think of using a hash table to record the occurrence count of each distinct number, and using a TreeSet to store the occurrence counts, so that occurrence counts can be updated quickly. Then one pass is enough.
Time complexity: O(N log N), space complexity: O(N).
classSolution { public: intmaxEqualFreq(vector<int>& nums){ unordered_map<int, int> count; multiset<int> order; int ans = 0; for (int x = 0; x < nums.size(); ++x) { int i = nums[x]; if (count[i] != 0) { auto it = order.find(count[i]); order.erase(it); } ++count[i]; order.insert(count[i]); // detemiate auto it1 = order.begin(); auto it2 = order.end(); --it2; int n = order.size() - 1; int length = x + 1; if (it1 == it2) { ans = x + 1; } else { if ((*it2) == (*it1) + 1) { if ( (*it2) * 1 + (*it1) * (n) == length) { ans = x + 1; } } if (*it1 == 1) { if (*it2 * n == length - 1) { ans = x + 1; } } } } return ans; } };