646. Maximum Length of Pair Chain/640. Solve the Equation
来源:互联网 发布:淘宝小额借贷 编辑:程序博客网 时间:2024/05/29 17:19
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: 2Explanation: 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
- 646. Maximum Length of Pair Chain/640. Solve the Equation
- 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- [leetcode]646. Maximum Length of Pair Chain
- leetcode 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- LeetCode 646. Maximum Length of Pair Chain
- [Leetcode] 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- leetcode 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- [leetcode] 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain(M)
- [LeetCode] 646. Maximum Length of Pair Chain
- [LeetCode] 646. Maximum Length of Pair Chain
- 646. Maximum Length of Pair Chain
- window.open()打开窗口的几种方式
- ActiveMQ消息的同步和异步
- 【水题】HDU4931Happy Three Friends【BestCoder Round #4】
- 初识nodejs9
- GTK学习(2)-布局管理
- 646. Maximum Length of Pair Chain/640. Solve the Equation
- [BZOJ]4129: Haruna’s Breakfast 树上带修改莫队+分块
- 泛型 Treeset的排序
- flask学习地址
- 完美兼容各版本IE的JQuery时间插件
- 集成Android免费语音合成功能(在线、离线、离在线融合),有这一篇文章就够了(离线)
- ubuntu 拷贝文件夹下所有文件到其他文件夹操作
- Qt 多语言
- ActiveMQ消息传送机制深入