C++ 刷题记录 No.4 (Greedy in, dp, tricky maps)

11 篇文章 0 订阅
6 篇文章 0 订阅

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…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值