646. Maximum Length of Pair Chain/640. Solve the Equation

646. Maximum Length of Pair Chain

Problem Description

You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.

Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.

Given a set of pairs, find the length longest chain which can be formed. You needn’t use up all the given pairs. You can select pairs in any order.

Example 1:

Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]

Note:
The number of given pairs will be in the range [1, 1000].

Implementation

sort the pairs and go through the pairs with O(n^2) algorithm.

class Solution {
public:
    static bool mycompare(vector<int> a, vector<int> b) {
        if(a[0] < b[0]) {
            return true;
        } 
        else 
        if(a[0] == b[0] && a[1] < b[1]) {
            return true;
        }    
        return false;
    }

    int findLongestChain(vector<vector<int>>& pairs) {
        int chain_len = 0;
        int size = pairs.size();
        if(!size) {
            return chain_len;
        }
        std::sort (pairs.begin(), pairs.end(), Solution::mycompare);

        vector<int> res(size, 1);
        for(int idx0 = 1; idx0 < size; idx0++) {
            for(int idx1 = 0; idx1 <= idx0; idx1++) {
                if(res[idx1] >= res[idx0] && pairs[idx0][0] > pairs[idx1][1]) {
                    res[idx0] = res[idx1] + 1;
                }    
            }
        }

        return *(std::max_element(res.begin(), res.end()));
    }
};

640. Solve the Equation

Problem Description

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and its coefficient.

If there is no solution for the equation, return "No solution".

If there are infinite solutions for the equation, return "Infinite solutions".

If there is exactly one solution for the equation, we ensure that the value of x is an integer.

Example 1:
Input: "x+5-3+x=6+x-2"
Output: "x=2"

Example 2:
Input: "x=x"
Output: "Infinite solutions"

Example 3:
Input: "2x=x"
Output: "x=0"

Example 4:
Input: "2x+3x-6x=x+2"
Output: "x=-1"

Example 5:
Input: "x=x+2"
Output: "No solution"

Implementation

Use two variables to store coefficient for number and x perspectively and be careful about the sign of the coefficient.

class Solution {
public:
    string solveEquation(string equation) {
        string no_answer[2] = {"Infinite solutions", "No solution"};
        int eq_len  = equation.length();
        if(!eq_len) {
            return no_answer[1];
        }
        int x_coeff = 0;
        int result  = 0;

        bool left = true;
        bool pos  = true;
        int  num  = 0;
        int  coeff[2][2] = {{-1, 1}, {1, -1}};
        for(unsigned int idx = 0; idx < eq_len; idx++) {
            if(equation[idx] == '=') {
                left = false;
                pos  = true;
                num  = 0;
                continue;
            }
            else if(equation[idx] == '-') {
                pos = false;
                num = 0;
                continue;
            }
            else if(equation[idx] == '+') {
                pos = true;
                num = 0;
                continue;
            }
            else if(isdigit(equation[idx])) {
                num = num*10 + equation[idx] - '0';
                if(idx < eq_len - 1) {
                    if(equation[idx+1] == '=' || equation[idx+1] == '-' || equation[idx+1] == '+') {
                        result += coeff[left][pos]*num;
                        num     = 0;
                    }
                    else if(equation[idx + 1] == 'x') {
                        x_coeff += (-coeff[left][pos])*num;
                        num      = 0;
                    }
                }
                else {
                    result += coeff[left][pos]*num;
                    num     = 0;
                }

            }
            else if(equation[idx] == 'x') {
                if(idx == eq_len - 1) {
                    if(!num && (idx > 0 && !isdigit(equation[idx-1]))) {
                        x_coeff += (-coeff[left][pos]);
                    }
                    else {
                        x_coeff += num;
                    }    
                    num      = 0;
                    continue;
                }
                else if(!num && (idx == 0 || !isdigit(equation[idx-1]))) {
                    x_coeff += (-coeff[left][pos]);
                }
            }     
        }


        if(!result && !x_coeff) {
            return no_answer[0];
        }
        else if(!x_coeff) {
            return no_answer[1];
        } 

        return "x=" + to_string(result/x_coeff);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值