← Home

1237. Find Positive Integer Solution for a Given Equation

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
123

The trick here is to name the state correctly, then let the implementation follow. For 1237. Find Positive Integer Solution for a Given Equation, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are f.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Find Positive Integer Solution for a Given Equation.
02//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Find Positive Integer Solution for a Given Equation.
03
04/*
05 * // This is the custom function interface.
06 * // You should not implement it, or speculate about its implementation
07 * class CustomFunction {
08 * public:
09 *     // Returns f(x, y) for any given positive integers x and y.
10 *     // Note that f(x, y) is increasing with respect to both x and y.
11 *     // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
12 *     int f(int x, int y);
13 * };
14 */
15
16class Solution {
17public:
18    vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
19        vector<vector<int>> ans;
20        
21        int x = 1, y;
22        
23        for(y = 1; customfunction.f(x, y) <= z; y++){
24            // cout << x << " " << y << " " << customfunction.f(x, y) << endl;
25            if(customfunction.f(x, y) == z){
26                ans.push_back({x, y});
27                break;
28            }
29        }
30        
31        //customfunction.f(x, 1) <= z: break if smallest y will make f(x,y) > z
32        for(x = 2; customfunction.f(x, y) >= z && customfunction.f(x, 1) <= z; x++){
33            for(; customfunction.f(x, y) >= z && y > 0; y--){
34                // cout << x << " " << y << " " << customfunction.f(x, y) << endl;
35                if(customfunction.f(x, y) == z){
36                    ans.push_back({x, y});
37                    break;
38                }
39            }
40            y++;
41        }
42        
43        return ans;
44    }
45};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.