133. Clone Graph
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
Node* clone_rec(Node* node, unordered_map<Node*, Node*>& completed_nodes) {
if (node == nullptr) {
return nullptr;
}
Node* pNew = new Node(node->val);
completed_nodes[node] = pNew;
for (auto p: node->neighbors) {
auto iter = completed_nodes.find(p);
if (iter == completed_nodes.end()) {
// not found
pNew->neighbors.push_back(clone_rec(p, completed_nodes));
} else {
pNew->neighbors.push_back(iter->second);
}
}
return pNew;
}
Node* cloneGraph(Node* node) {
unordered_map<Node*, Node*> completed_nodes;
return clone_rec(node, completed_nodes);
}
};
19. Remove Nth Node From End of List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int counter = 0;
ListNode* p = head;
while (p != nullptr) {
p = p->next;
counter++;
}
int num = counter - n - 1;
p = head;
if (num == -1) {
return head->next;
}
while (num--) {
p = p->next;
}
p->next = p->next->next;
return head;
}
};
11. Container With Most Water
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0, j = height.size() - 1;
int water = 0;
while (i < j) {
int h = min(height[i], height[j]);
water = max(water, h* (j - i));
while (height[i] <= h && i < j) i++;
while (height[j] <= h && i < j) j--;
}
return water;
}
};
Greedy 系列第一题,不难想而且好玩
45. Jump Game II
class Solution {
public:
int jump(vector<int>& nums) {
int step = 0;
int end = 0, reach = 0;
for (int i = 0; i < nums.size() - 1; i++) {
reach = max(i + nums[i], reach);
if (i == end) {
end = reach;
step++;
}
}
return step;
}
};
到达最后一个位置即可,不用继续跳了
122. Best Time to Buy and Sell Stock II
class Solution {
public:
int maxProfit(vector<int>& prices) {
// recognize increasing slope
int base = prices[0];
int sum = 0;
int cur = prices[0];
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > cur) {
cur = prices[i];
} else { // sell
sum += cur - base;
base = prices[i];
cur = prices[i];
}
}
sum += cur - base;
return sum;
}
};
121. Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int buy = prices[0];
int maxPro = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] < buy) {
buy = prices[i];
}
maxPro = max(maxPro, prices[i] - buy);
}
return maxPro;
}
};
This is not greedy but dynamic programming.
2034. Stock Price Fluctuation
class StockPrice {
public:
map<int, int> stocks;
map<int, int> prices;
int cur = 0;
StockPrice() {
}
void update(int timestamp, int price) {
if (timestamp > cur) {
cur = timestamp;
}
if (stocks.find(timestamp) != stocks.end()) {
int old_price = stocks[timestamp];
prices[old_price]--;
if (prices[old_price] == 0) {
prices.erase(old_price);
}
}
prices[price]++;
stocks[timestamp] = price;
}
int current() {
return stocks[cur];
}
int maximum() {
return prices.rbegin()->first;
}
int minimum() {
return prices.begin()->first;
}
};
/**
* Your StockPrice object will be instantiated and called as such:
* StockPrice* obj = new StockPrice();
* obj->update(timestamp,price);
* int param_2 = obj->current();
* int param_3 = obj->maximum();
* int param_4 = obj->minimum();
*/
map again…