Ejemplo n.º 1
0
    public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
    {
        List <List <int> > res = new List <List <int> >();

        for (int x = 1; x <= 1000; x++)
        {
            int l = 1, h = 1000;
            while (l <= h)
            {
                int y = l + (h - l) / 2;
                if (customfunction.f(x, y) == z)
                {
                    res.Add(new List <int> {
                        x, y
                    });
                    break;
                }
                else if (customfunction.f(x, y) < z)
                {
                    l = y + 1;
                }
                else
                {
                    h = y - 1;
                }
            }
        }
        return(res.ToList <IList <int> >());
    }
Ejemplo n.º 2
0
        public IList <IList <int> > FindSolution2(CustomFunction customfunction, int z)
        {
            // Monotonicity leads to binary search.
            var result = new List <IList <int> >();

            for (int x = 1; x <= 1000; x++)
            {
                int lo = 1, hi = 1001;
                while (lo < hi)
                {
                    int y = lo + (hi - lo) / 2;
                    if (customfunction.f(x, y) > z)
                    {
                        hi = y;
                    }
                    else if (customfunction.f(x, y) < z)
                    {
                        lo = y + 1;
                    }
                    else
                    {
                        result.Add(new int[] { x, y });
                        break;
                    }
                }
            }

            return(result);
        }
    public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
    {
        IList <IList <int> > res = new List <IList <int> >();
        int left  = 1;
        int right = 1000;

        while (left <= 1000 && right >= 1)
        {
            int temp = customfunction.f(left, right);
            if (temp == z)
            {
                IList <int> list = new List <int>();
                list.Add(left);
                list.Add(right);
                res.Add(list);
                left++;
            }
            else if (temp > z)
            {
                right--;
            }
            else
            {
                left++;
            }
        }
        return(res);
    }
Ejemplo n.º 4
0
        private IList <int> BinarySearchY(CustomFunction customfunction, int x, int z)
        {
            int min = 1;
            int max = 1000;

            while (min <= max)
            {
                int mid  = (min + max) / 2;
                int fMid = customfunction.f(x, mid);
                if (fMid == z)
                {
                    return new int[] { x, mid }
                }
                ;

                if (fMid < z)
                {
                    min = mid + 1;
                }
                else
                {
                    max = mid - 1;
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        private int MaxX(CustomFunction customfunction, int z)
        {
            int min = 1;
            int max = 1000;

            while (min <= max)
            {
                int mid  = (min + max) / 2;
                int fMid = customfunction.f(mid, 1);
                if (fMid == z)
                {
                    return(mid);
                }

                if (fMid < z)
                {
                    min = mid + 1;
                }
                else
                {
                    max = mid - 1;
                }
            }
            return(max);
        }
    public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
    {
        var ret = new List <IList <int> >();
        int x = 1, y = 1000;

        while (x <= 1000 && y > 0)
        {
            var v = customfunction.f(x, y);
            if (v == z)
            {
                ret.Add(new List <int>()
                {
                    x++, y--
                });
            }
            else if (v < z)
            {
                ++x;
            }
            else
            {
                --y;
            }
        }
        return(ret);
    }
        public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
        {
            IList <IList <int> > ans = new List <IList <int> >();
            int y = z;

            for (int x = 1; x <= z && y > 0; x++)
            {
                while (y > 0)
                {
                    int zz = customfunction.f(x, y);
                    if (zz == z)
                    {
                        ans.Add(new List <int>()
                        {
                            x, y
                        });
                        break;
                    }
                    else if (zz > z)
                    {
                        y--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(ans);
        }
Ejemplo n.º 8
0
        public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
        {
            // Inspired by https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/discuss/414249/JavaC%2B%2BPython-O(X%2BY)
            // The problem is essentially the same as "240. Search a 2D Matrix II".
            int x = 1000, y = 1;
            var result = new List <IList <int> >();

            while (x >= 1 && y <= 1000)
            {
                if (z < customfunction.f(x, y))
                {
                    x--;
                }
                else if (z > customfunction.f(x, y))
                {
                    y++;
                }
                else
                {
                    result.Add(new int[] { x--, y++ });
                }
            }
            return(result);
        }
Ejemplo n.º 9
0
        public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
        {
            IList <IList <int> > ret = new List <IList <int> >();

            for (int x = 0; x <= 1000; x++)
            {
                for (int y = 0; y <= 1000; y++)
                {
                    if (customfunction.f(x, y) == z)
                    {
                        ret.Add(new List <int>()
                        {
                            x, y
                        });
                    }
                }
            }
            return(ret);
        }
        public IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
        {
            List <IList <int> > positiveSolutions = new List <IList <int> >();

            for (int i = 1; i <= z; i++)
            {
                for (int j = 1; j <= z; j++)
                {
                    if (customfunction.f(i, j) == z)
                    {
                        positiveSolutions.Add(new List <int>()
                        {
                            i, j
                        });
                    }
                }
            }

            return(positiveSolutions);
        }
Ejemplo n.º 11
0
 public IList<IList<int>> FindSolution(CustomFunction customfunction, int z)	{
             List<IList<int>> r = new List<IList<int>>();
             int i = 1000; int j = 1;
             while (i >= 1 && j <= 1000)
             {
                 int f = customfunction.f(i, j);
                 if (f < z)
                 {
                     ++j;
                 }
                 else if (f == z)
                 {
                     List<int> rr = new List<int>();
                     rr.Add(i); rr.Add(j); r.Add(rr);
                     --i;
                 }
                 else
                     --i;
             }
             return r;
      }
        /// <summary>
        /// Find Positive Integer Solution for a Given Equation (Mine)
        /// </summary>
        /// <param name="customfunction"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public static IList <IList <int> > FindSolution(CustomFunction customfunction, int z)
        {
            var res = new List <IList <int> >();
            int iMax = 1000, jMax = 1000;

            for (int i = 1; i <= iMax; i++)
            {
                for (int j = 1; j <= jMax; j++)
                {
                    if (customfunction.f(i, j) == z)
                    {
                        res.Add(new List <int>()
                        {
                            i, j
                        });
                        jMax = j - 1;
                        break;
                    }
                }
            }

            return(res);
        }