Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int, int> hash;
const int sz = numbers.size();
for (int i = 0; i < sz; ++i)
hash[numbers[i]] = i + 1;
vector<int> ans;
for (int i = 0; i < sz; ++i) {
map<int, int>::iterator it = hash.find(target - numbers[i]);
if (it != hash.end() && it->second != i + 1) {
ans.push_back(i + 1);
ans.push_back(it->second);
}
}
return ans;
}
};
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
const int sz = triangle.size();
vector<int> dp(sz, 0);
dp[0] = triangle[0][0];
for (int i = 1; i < sz; ++i) {
dp[i] = dp[i - 1] + triangle[i][i];
for (int j = i - 1; j > 0; --j)
dp[j] = min(dp[j - 1], dp[j]) + triangle[i][j];
dp[0] += triangle[i][0];
}
int ans = INT_MAX;
for (int i = 0; i < sz; ++i)
ans = min(ans, dp[i]);
return ans;
}
};
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' '
when necessary so that each line has exactly Lcharacters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. " ]
Note: Each word is guaranteed not to exceed L in length.
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ans;
const int sz = words.size();
int i = 0;
while (i < sz) {
int tmp = 0, j = i;
while (j < sz && tmp + words[j].length() + (tmp == 0 ? 0 : 1) <= L) {
tmp += (words[j].length() + (tmp == 0 ? 0 : 1));
++j;
}
int rem = L - tmp;
if (j - i == 1) {
string str = words[i];
while (rem--)
str += " ";
ans.push_back(str);
} else {
const int a = rem / (j - i - 1), b = rem % (j - i - 1);
string str = words[i];
for (int k = i + 1; k < j; ++k) {
str += " ";
if (j < sz) {
for (int t = 0; t < a; ++t) str += " ";
if (k - i <= b) str += " ";
}
str += words[k];
}
if (j >= sz) {
while (rem--)
str += " ";
}
ans.push_back(str);
}
i = j;
}
return ans;
}
};
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
class Solution {
public:
int trap(int A[], int n) {
int ans = 0, left = 0, right = 1, h = A[left], tmp = 0;
while (right < n) {
int c = A[right];
if (c < h) {
tmp += (h - c);
} else {
ans += tmp;
tmp = 0;
left = right;
h = c;
}
++right;
}
const int end = left;
h = A[n - 1], left = n - 2, tmp = 0;
while (left >= end) {
int c = A[left];
if (c < h) {
tmp += (h - c);
} else {
ans += tmp;
tmp = 0;
h = c;
}
--left;
}
return ans;
}
};