Find Positive Integer Solution for a Given Equation Solutions in C++
Number 1237
Difficulty Easy
Acceptance 69.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation//// Author : liuyubobobo/// Time : 2019-10-26#include <iostream>#include <vector>using namespace std;/// Brute Force/// Time Complexity: O(1000 * 1000)/// Space Complexity: O(1)class Solution {public:vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {vector<vector<int>> res;for(int x = 1; x <= 1000; x ++)for(int y = 1; y <= 1000; y ++)if(customfunction.f(x, y) == z) res.push_back({x, y});return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation//// Author : liuyubobobo/// Time : 2019-11-01#include <iostream>#include <vector>using namespace std;/// Binary Search/// Time Complexity: O(1000 * log(1000))/// Space Complexity: O(1)class Solution {public:vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {vector<vector<int>> res;for(int x = 1; x <= z; x ++){int l = 1, r = 1000;while(l <= r){int mid = (l + r) / 2;int v = customfunction.f(x, mid);if(v == z){res.push_back({x, mid});break;}else if(v < z) l = mid + 1;else r = mid - 1;}}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation//// Author : liuyubobobo/// Time : 2019-11-03#include <iostream>#include <vector>using namespace std;/// Two Pointers/// Time Complexity: O(1000)/// Space Complexity: O(1)class Solution {public:vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {vector<vector<int>> res;for(int x = 1, y = 1000; x <= 1000 && y >= 1;){int v = customfunction.f(x, y);if(v == z) res.push_back({x ++, y --};else if(v < z) y --;else x ++;}return res;}};int main() {return 0;}