← Static tasks

1237. Find Positive Integer Solution for a Given Equation

leetcode medium

#csharp#leetcode#medium#search

Task

Если дана вызываемая функция f(x, y) со скрытой формулой и значением z, выполните обратную разработку формулы и верните все пары целых положительных чисел x и y, в которых f(x,y) == z. Пары можно возвращать в любом порядке. Хотя точная формула скрыта, функция является монотонно возрастающей, т.е.Например: f(x, y) < f(x + 1, y) f(x, y) < f(x, y + 1) Интерфейс функции определяется следующим образом: interface CustomFunction { public: // Возвращает некоторое положительное целое число f(x, y) для двух положительных целых чисел x и y на основе формулы.

C# solution

matched/original
using System.Collections.Generic;
public class CustomFunction {
    public int f(int x, int y) {}
}
public class Solution {
    public IList<IList<int>> FindSolution(CustomFunction customfunction, int z) {
        var result = new List<IList<int>>();
        int x = 1;
        int y = 1000;
        
        while (x <= 1000 && y >= 1) {
            int value = customfunction.f(x, y);
            if (value == z) {
                result.Add(new List<int> { x, y });
                x++;
            } else if (value < z) {
                x++;
            } else {
                y--;
            }
        }
        
        return result;
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
public class CustomFunction {
    public int f(int x, int y) {}
}
class Solution {
public:
    public IList<vector<int>> FindSolution(CustomFunction customfunction, int z) {
        var result = new List<vector<int>>();
        int x = 1;
        int y = 1000;
        
        while (x <= 1000 && y >= 1) {
            int value = customfunction.f(x, y);
            if (value == z) {
                result.push_back(new List<int> { x, y });
                x++;
            } else if (value < z) {
                x++;
            } else {
                y--;
            }
        }
        
        return result;
    }
}

Java solution

matched/original
import java.util.ArrayList;
import java.util.List;

public class CustomFunction {
    public int f(int x, int y) {}
}

public class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> result = new ArrayList<>();
        int x = 1;
        int y = 1000;
        
        while (x <= 1000 && y >= 1) {
            int value = customfunction.f(x, y);
            if (value == z) {
                List<Integer> pair = new ArrayList<>();
                pair.add(x);
                pair.add(y);
                result.add(pair);
                x++;
            } else if (value < z) {
                x++;
            } else {
                y--;
            }
        }
        
        return result;
    }
}

JavaScript solution

matched/original
class CustomFunction {
    f(x, y) {}
}

var findSolution = function(customfunction, z) {
    let result = [];
    let x = 1;
    let y = 1000;
    
    while (x <= 1000 && y >= 1) {
        let value = customfunction.f(x, y);
        if (value === z) {
            result.push([x, y]);
            x++;
        } else if (value < z) {
            x++;
        } else {
            y--;
        }
    }
    
    return result;
};

Go solution

matched/original
type CustomFunction struct {}

func (cf CustomFunction) f(x, y int) int {}

func findSolution(customfunction CustomFunction, z int) [][]int {
    result := [][]int{}
    x, y := 1, 1000
    
    for x <= 1000 && y >= 1 {
        value := customfunction.f(x, y)
        if value == z {
            result = append(result, []int{x, y})
            x++
        } else if value < z {
            x++
        } else {
            y--
        }
    }
    
    return result
}

Explanation