标签:integer output min think each action bec rom ons
961. N-Repeated Element in Size 2N Array
In a array A
of size 2N
, there are N+1
unique elements, and exactly one of these elements is repeated N times.
Return the element repeated N
times.
Example 1:
Input: [1,2,3,3]
Output: 3
Example 2:
Input: [2,1,2,5,3,2]
Output: 2
Example 3:
Input: [5,1,5,2,5,3,5,4]
Output: 5
Note:
4 <= A.length <= 10000
0 <= A[i] < 10000
A.length
is evenApproach #1: C++.
class Solution { public: int repeatedNTimes(vector<int>& A) { int size = A.size(); int repeatTime = size / 2; vector<int> temp(10005, 0); for (int a : A) { temp[a]++; if (temp[a] == repeatTime) return a; } } };
962. Maximum Width Ramp
Given an array A
of integers, a ramp is a tuple (i, j)
for which i < j
and A[i] <= A[j]
. The width of such a ramp is j - i
.
Find the maximum width of a ramp in A
. If one doesn‘t exist, return 0.
Example 1:
Input: [6,0,8,2,1,5]
Output: 4
Explanation:
The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5.
Example 2:
Input: [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation:
The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.
Note:
2 <= A.length <= 50000
0 <= A[i] <= 50000
Approach #2: C++.
typedef pair<int, int> pp; class Solution { public: int maxWidthRamp(vector<int>& A) { vector<pp> temp; int ans = 0; temp.push_back(make_pair(A[0], 0)); for (int i = 1; i < A.size(); ++i) { if (A[i] < temp.back().first) { temp.push_back(make_pair(A[i], i)); } else { int ramp = i - binarySearch(temp, A[i]); ans = max(ans, ramp); } } return ans; } int binarySearch(vector<pp> temp, int target) { int l = -1, r = temp.size()-1, mid; while (l+1 < r) { mid = (l + r) / 2; if (temp[mid].first > target) l = mid; else r = mid; } return temp[r].second; } };
Analysis:
we use a vector to store the decrement elements from the begin to the end of A.
963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.
If there isn‘t any rectangle, return 0.
Example 1:
Input: [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.
Example 2:
Input: [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.
Example 3:
Input: [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.
Example 4:
Input: [[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [2,1],[2,3],[3,3],[3,1], with an area of 2.
Note:
1 <= points.length <= 50
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
10^-5
of the actual value will be accepted as correct.Approach #1: C++.
Thinking..................................
964. Least Operators to Express Number
Given a single positive integer x
, we will write an expression of the form x (op1) x (op2) x (op3) x ...
where each operator op1
, op2
, etc. is either addition, subtraction, multiplication, or division (+
, -
, *
, or /)
. For example, with x = 3
, we might write 3 * 3 / 3 + 3 - 3
which is a value of 3.
When writing such an expression, we adhere to the following conventions:
/
) returns rational numbers.-
). For example, "x - x
" is a valid expression as it only uses subtraction, but "-x + x
" is not because it uses negation.We would like to write an expression with the least number of operators such that the expression equals the given target
. Return the least number of expressions used.
Example 1:
Input: x = 3, target = 19
Output: 5
Explanation: 3 * 3 + 3 * 3 + 3 / 3. The expression contains 5 operations.
Example 2:
Input: x = 5, target = 501
Output: 8
Explanation: 5 * 5 * 5 * 5 - 5 * 5 * 5 + 5 / 5. The expression contains 8 operations.
Example 3:
Input: x = 100, target = 100000000
Output: 3
Explanation: 100 * 100 * 100 * 100. The expression contains 3 operations.
Note:
2 <= x <= 100
1 <= target <= 2 * 10^8
Approach #1: C++.
Thinking....................................
标签:integer output min think each action bec rom ons
原文地址:https://www.cnblogs.com/ruruozhenhao/p/10163954.html