1.Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
分析:
Solution:
思路一:时间复杂度O(n),空间复杂度O(1)
bool canJump(int A[], int n)
{
int reach = 1; //最右能调到哪里
for (int i = 0; i < reach && reach < n; i++)
reach = max(reach, i+1+A[i]);
return reach >= n;
}
2.Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
Note:
You can assume that you can always reach the last index.
Solution:
时间复杂度O(n),空间复杂度O(1)
int jump(int A[], int n)
{
int step = 0; //最小步数
int left = 0;
int right = 0; //[left,right]是当前能覆盖的区间
if(n == 1)
return 0;
while (left < right)
{
//尝试从每一层跳到最远
step++;
const int old_right = right;
for (int i = 0; i <= old_right; i++)
{
int new_right = i+A[i];
if(new_right >= n-1)
return step;
if(new_right > right)
right = new_right;
}
left = old_right + 1;
}
return 0;
}
3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
分析:
Solution:
int lengthOfLongestSubstring(string s)
{
const int ASCII_MAX = 26;
int last[ASCII_MAX]; //记录字符上次出现过的位置
int start = 0; //记录当前子串的起始位置
fill(last, last+ASCII_MAX, -1); //0也是有效位置,因此初始化为-1
int max_len = 0;
for (int i = 0; i < s.size(); i++)
{
if(last[s[i]-'a'] >= start)
{
max_len = max(i-start,max_len);
start = last[s[i]-'a'] + 1;
}
last[s[i]-'a'] = i;
}
return max((int)s.size()-start, max_len); //别忘了最后一次,例如“abcd”
}
4.Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题目:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。设计一个算法找出最大利润但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。
分析:贪心法。从前向后遍历数组,只要当天的价格高于前一天的价格(即为了最大利润,只要有利润存在就利用交易次数的无限制贪心的获取),就累加到收益中。
代码:时间O(n),空间O(1)。
int maxProfit(vector<int>& prices)
{
if(prices.size() < 2)
return 0;
int profit = 0;
for(auto ite = prices.begin()+1; ite != prices.end(); ite++)
profit += max(*ite - *(ite-1),0);
return profit;
}