thảo luận Leetcode mỗi ngày

Code:
import math

class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        def check(x):
            return math.sqrt(x).is_integer()
            
        def binarySearch(l , r , x):
            if not check(x): return False
            while l <= r:
                mid = (l + r) // 2
                if mid == x:
                    return True
                elif mid > x: r = mid - 1
                else: l = mid + 1
            
            return False
        
        for l in range(0 , int(math.sqrt(c) + 1)):
            if binarySearch(0 , c , c - l * l): return True
            
        return False
 
Python:
class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        a = 0
        b = int(math.sqrt(c))
        while a <= b:
            cand = a ** 2 + b ** 2
            if cand == c:
                return True
            if cand < c:
                a += 1
            else:
                b -= 1
        return False
làm xog thấy cách này hay hơn
u3720e4.png
 
Code:
fun judgeSquareSum(c: Int): Boolean {
    return (0..sqrt(c.toDouble()).toInt()).any { sqrt((c - it * it).toDouble()) % 1 == 0.0 }
}
 
Kiếm định lý toán đồ làm theo. Làm xong thấy chúng làm 2 pointers chưa được chục dòng :censored:
C#:
public class Solution
{
    public bool JudgeSquareSum(int c)
    {
        if (c == 2 || c == 1 || c == 0)
        {
            return true;
        }
        
        Dictionary<int, int> primeFactors = PrimeFactorization(c);
        foreach (var entry in primeFactors)
        {
            int prime = entry.Key;
            int exponent = entry.Value;
            if (prime % 4 == 3 && exponent % 2 != 0)
            {
                return false;
            }
        }

        return true;;
    }

    private Dictionary<int, int> PrimeFactorization(int n)
    {
        Dictionary<int, int> primeFactors = new(); // key = prime, value = exponent

        while (n % 2 == 0)
        {
            n /= 2;
            if (!primeFactors.ContainsKey(2))
            {
                primeFactors[2] = 1;
                continue;
            }
            primeFactors[2]++;
        }

        int sqrtn = (int)Math.Sqrt(n);
        for (int i = 3; i <= sqrtn; i += 2)
        {
            while (n % i == 0)
            {
                n /= i;
                if (!primeFactors.ContainsKey(i))
                {
                    primeFactors[i] = 1;
                    continue;
                }
                
                primeFactors[i]++;
            }
        }

        primeFactors[n] = 1;

        return primeFactors;
    }
}
 
Ví dụ số 8 sao lại là true được nhỉ
Java:
class Solution {
    public boolean judgeSquareSum(int c) {
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i <= Math.sqrt(c); i++) {
            set.add(i*i);
            if (set.contains(c - i * i)) return true;
        }

        return false;
    }
}
Cũng là O(n) mà sao beat có 5% à :beat_brick:
 
Last edited:
Ví dụ số 8 sao lại là true được nhỉ
Java:
class Solution {
    public boolean judgeSquareSum(int c) {
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i <= Math.sqrt(c); i++) {
            set.add(i*i);
            if (set.contains(c - i * i)) return true;
        }

        return false;
    }
}
Cũng là O(n) mà sao beat có 5% à :beat_brick:
1/ 8 true do a, b không nhất thiết là hai số phân biệt b.
2/ Chắc do solution b có space complexity > O(1)
 
Last edited:
Ví dụ số 8 sao lại là true được nhỉ
Java:
class Solution {
    public boolean judgeSquareSum(int c) {
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i <= Math.sqrt(c); i++) {
            set.add(i*i);
            if (set.contains(c - i * i)) return true;
        }

        return false;
    }
}
Cũng là O(n) mà sao beat có 5% à :beat_brick:
fence dùng set, có cả method insert và lookup thì nó chậm hơn là đúng rồi. Cách 2-pointer nó tính có mỗi 1 phép tính đại số thôi. 😌
 
C#:
public class Solution {
    public bool JudgeSquareSum(int c) {
        HashSet<int> set = new HashSet<int>();
        for(int i = 0; i<(int)(Math.Sqrt(c)) + 1; i++)
        {
            set.Add(i*i);
            if(set.Contains(c-i*i))
                return true;
        }
        return false;
    }
}
 
Ví dụ số 8 sao lại là true được nhỉ
Java:
class Solution {
    public boolean judgeSquareSum(int c) {
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i <= Math.sqrt(c); i++) {
            set.add(i*i);
            if (set.contains(c - i * i)) return true;
        }

        return false;
    }
}
Cũng là O(n) mà sao beat có 5% à :beat_brick:
nlogn rồi fen cái add Set nó logN rồi còn :beat_brick:
 
Java:
    public boolean judgeSquareSum(int c) {
        for(int i = 0; i <= Math.sqrt(c); i++){
            if(Math.sqrt(c-i*i)%1==0) return true;
        }
        return false;
    }
Dùng a^2 + b^2 = c => a = sqrt(c-b^2) với điều kiện a là số nguyên và b^2 <= c nên for loop từ 0 tới sqrt(c) nếu có 1 số thỏa thì return true :LOL:
Cảm ơn thím em làm toàn bị Time Limit Exceeded
 
Java:
class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        Map<Integer, Integer> map = new HashMap<>();
        int n = difficulty.length;
        int m = worker.length;

        for (int i = 0; i < n; i++) {
            if (!map.containsKey(difficulty[i]) || profit[i] > map.get(difficulty[i])) {
                map.put(difficulty[i], profit[i]);
            }
        }

        Arrays.sort(difficulty);

        for (int i = 0; i < n; i++) {
            profit[i] = map.get(difficulty[i]);
        }

        for (int i = 1; i < n; i++) {
            profit[i] = Math.max(profit[i], profit[i - 1]);
        }

        int ans = 0;
        Arrays.sort(worker);
        int wp = 0, dp = 0;

        while (wp < m) {
            if (worker[wp] >= difficulty[dp]) {
                if (dp == n - 1 || worker[wp] < difficulty[dp + 1]) {
                    ans += profit[dp];
                    wp++;
                } else if (worker[wp] >= difficulty[dp + 1]) {
                    dp++;
                }
            } else {
                ans += 0;
                wp++;
            }
        }

        return ans;
    }
}
 
Last edited:
Back
Top