Minimalist Forum Reader
em ko phải cao thủ, nhưng theo kn thì để 100% nếu đã có nhiều solutions rồi thì khó (có vài trg hợp hack hoặc "max optimization" đã 100% rồi thì ko được.
Còn về impl của bác có đoạn này em hơi nghi ngờ (có thể refactor/optimize)?
Swift:
`if sum <= i {` nằm trong `if i <= sum {`
swift có cái array [index] ko bác, nếu có thì là do set.contains() chậm hơn chọc thẳng vào arr check [num]. trước cũng ham beat 100% lắm. nhưng mà ô kia muốn xem vì sao ko beat dc 100% thì mở code thằng 100% ra xem là biết r mà
IbCY3qj.png
C++:
class Solution {
 public:
  int maxCount(vector<int>& banned, int n, int maxSum) {
    auto bannedNums = unordered_set<int>(banned.begin(), banned.end()); auto count = 0;
    for (auto i = 1; maxSum >= i && i <= n; ++i) {
      if (bannedNums.count(i) > 0) continue;
      maxSum -= i; ++count;
    }
    return count;
  }
};
chấm.
JavaScript:
var maxCount = function(banned, n, maxSum) {
    let result = 0
    let sum = 0
    let banSet = new Set(banned)
    for(let i=1; i<=n; i++) {
        if(banSet.has(i)) continue
        sum +=i
        if(sum<=maxSum) {
            result++
        }
    }
    return result
};
swift có cái array [index] ko bác, nếu có thì là do set.contains() chậm hơn chọc thẳng vào arr check [num]. trước cũng ham beat 100% lắm. nhưng mà ô kia muốn xem vì sao ko beat dc 100% thì mở code thằng 100% ra xem là biết r mà
IbCY3qj.png
Swift bác lấy array[index] nó sẽ chậm hơn ấy. Swift lấy item at index là nó O(n) chứ không phải O(1)


em ko phải cao thủ, nhưng theo 1 ít kn thì để 100% nếu đã có nhiều solutions rồi thì khó
(Khi đã có vài trg hợp hack hoặc "max optimization" đã 100% rồi thì ko được.
Gần đây LC có check gì đó nếu chép sol lần 2, 3 sẽ bị chậm chừng 2 3 ms so với 1st submission).
Còn về impl của bác có đoạn này em hơi nghi ngờ (có thể refactor/optimize)?
Swift:
`if sum <= i {` nằm trong `if i <= sum {`

sum <= 1 nằm trong i <= sum để nó exit sớm scope mà bác :v sau khi check sum được trừ rồi còn gì
bác nào có premium chạy thử giùm e code này pass dc bài này phần 2 với n<10^9 ko
với m = banned.length;
Java:
class Solution {
    public int maxCount(int[] banned, int n, int maxSum) {
        Set<Integer> set = new HashSet();
        for(int num:banned){
            set.add(num);
        }
        int[] uniq_banned = new int[set.size()];
        int index =0;
        for(int num:set){
            uniq_banned[index++]=num;
        }
        Arrays.sort(uniq_banned);
        int[] prefix_sum = new int[uniq_banned.length];
        prefix_sum[0]= uniq_banned[0];
        for(int i =1;i<uniq_banned.length;i++){
            prefix_sum[i]= prefix_sum[i-1]+uniq_banned[i];
        }
        int l =1;
        int r = n;
        int ans =0;

        while(l<=r){
            int mid = l+(r-l)/2;
            int sum = ((mid+1)*mid)/2;
            int i=find_boundary(mid, uniq_banned);
            if(i!=-1){
                sum-=prefix_sum[i];
            }
            if(sum<=maxSum){
                ans =mid-(i+1);
                l= mid+1;
            }else{
                r=mid-1;
            }
        }
        return ans;
    }
    public int find_boundary(int num, int[] uniq){
        int res =-1;
        int l=0;
        int r = uniq.length-1;
        while(l<=r){
            int mid = l+(r-l)/2;
            if(uniq[mid]<=num){
                res =mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        return res;
    }
}
@freedom.9 giúp e nó check thử đi bác phi đôm
k dc mặt tiền rùi :(
sum <= 1 nằm trong i <= sum để nó exit sớm scope mà bác :v sau khi check sum được trừ rồi còn gì
understood, bác (icon :v ).

Bài hôm qua nay revisited lại: LC 2337 Java
Java:
class Solution {
  public boolean canChange(String s, String t) {
    return s.lastIndexOf('R') <= t.lastIndexOf('R') && t.lastIndexOf('L') <= s.lastIndexOf('L')
      && t.indexOf('L') <= s.indexOf('L') && s.indexOf('R') <= t.indexOf('R')
      && s.replaceAll("_+", "").equals(t.replaceAll("_+", ""));
  }
}
Java:
class Solution {
  public boolean canChange(String s, String t) {
    return !(t.indexOf('L')>s.indexOf('L') || s.indexOf('R')>t.indexOf('R') || s.lastIndexOf('R')>t.lastIndexOf('R')
      || t.lastIndexOf('L')>s.lastIndexOf('L')) && s.replace("_", "").equals(t.replace("_", ""));
  }
}
Thread này trả phí à mà được ghim :oops:
JavaScript:
var maxCount = function(banned, n, maxSum) {
    banned.sort((u, v) => u - v);
    let s = 0, ans = 0;
  
    for (let i = 1; i <= n; i++) {
        if (s + i <= maxSum && _.sortedIndexOf(banned, i) < 0) {
            s += i;
            ans++;
        }
    }
    return ans;
};
Thread nào phù hợp với F91 mà ae hoạt động sôi nổi sẽ đc ghim.
Trả tiền để ghim chủ yếu ở bên F68.
zFNuZTA.png
JavaScript:
var maxCount = function(banned, n, maxSum) {
    const isBanned = new Set(banned);
    let ans = 0;
    let sum = 0;
    for(let num = 1; num <= n; num++){
        if(isBanned.has(num)) continue;
        sum += num;
        if(sum > maxSum) return ans;
        ans++;
    }
    return ans;
};
Swift bác lấy array[index] nó sẽ chậm hơn ấy. Swift lấy item at index là nó O(n) chứ không phải O(1)




sum <= 1 nằm trong i <= sum để nó exit sớm scope mà bác :v sau khi check sum được trừ rồi còn gì
array của swift là linked list hay gì mà O(n) indexing thế ?
View attachment 2819367
Compile lỗi rồi đại ca
xin bác cái input gốc đề phần 2 đi bác. chắc maxSum là long hử
Java:
class Solution {
    public int maxCount(int[] banned, int n, long maxSum) {
        Set<Integer> set = new HashSet();
        for(int num:banned){
            set.add(num);
        }
        int[] uniq_banned = new int[set.size()];
        int index =0;
        for(int num:set){
            uniq_banned[index++]=num;
        }
        Arrays.sort(uniq_banned);
        int[] prefix_sum = new int[uniq_banned.length];
        prefix_sum[0]= uniq_banned[0];
        for(int i =1;i<uniq_banned.length;i++){
            prefix_sum[i]= prefix_sum[i-1]+uniq_banned[i];
        }
        long l =1;
        long r = n;
        long ans =0;

        while(l<=r){
            long mid = l+(r-l)/2;
            long sum = ((mid+1)*mid)/2;
            int i=find_boundary(mid, uniq_banned);
            if(i!=-1){
                sum-=prefix_sum[i];
            }
            if(sum<=maxSum){
                ans =mid-(i+1);
                l= mid+1;
            }else{
                r=mid-1;
            }
        }
        return (int)ans;
    }
    public int find_boundary(long num, int[] uniq){
        int res =-1;
        int l=0;
        int r = uniq.length-1;
        while(l<=r){
            int mid = l+(r-l)/2;
            if(uniq[mid]<=num){
                res =mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        return res;
    }
}
C#:
public class Solution {
    public int MaxCount(int[] banned, int n, int maxSum) {
        HashSet<int> set = new HashSet<int>(banned);
        int temp = 0;
        int result = 0;
        for (int i = 1; i <= n; i++)
        {
            if (temp + i <= maxSum && !set.Contains(i))
            {
                temp += i;
                result++;
            }
        }
        return result;
    }
}
class Solution { public int maxCount(int[] banned, int n, long maxSum) { Set<Integer> set = new HashSet(); for(int num:banned){ set.add(num); } int[] uniq_banned = new int[set.size()]; int index =0; for(int num:set){ uniq_banned[index++]=num; } Arrays.sort(uniq_banned); int[] prefix_sum = new int[uniq_banned.length]; prefix_sum[0]= uniq_banned[0]; for(int i =1;i<uniq_banned.length;i++){ prefix_sum= prefix_sum[i-1]+uniq_banned; } long l =1; long r = n; long ans =0; while(l<=r){ long mid = l+(r-l)/2; long sum = ((mid+1)*mid)/2; int i=find_boundary(mid, uniq_banned); if(i!=-1){ sum-=prefix_sum; } if(sum<=maxSum){ ans =mid-(i+1); l= mid+1; }else{ r=mid-1; } } return (int)ans; } public int find_boundary(long num, int[] uniq){ int res =-1; int l=0; int r = uniq.length-1; while(l<=r){ int mid = l+(r-l)/2; if(uniq[mid]<=num){ res =mid; l=mid+1; }else{ r=mid-1; } } return res; } }
1733499587320.png

Trình độ chưa đủ nhé :doubt:
Code:
func maxCount(banned []int, n int, maxSum int) int {
    bannedSet := make(map[int]bool)

    for _, elem := range banned {
        bannedSet[elem] = true
    }

    res, sum := 0, 0

    for i := 1; i <= n; i++ {
        if bannedSet[i] == true {
            continue
        }

        if sum + i > maxSum {
            return res
        }

        res++;
        sum += i
    }

    return res
}
@LmaoSuVuong bài này cũng ko có gì đặc biệt mà :doubt: đi bisearch như thế này này.
Về luyện thêm 10 bài bi search nhé
Python:
class Solution:
    def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:
        banned = sorted(set(banned))
        m = len(banned)
        sumSofar = 0
        prefixSum = [0]*m
        for i in range(m):
            sumSofar += banned[i]
            prefixSum[i] = sumSofar

        def good(target):
            totalSum = target*(target + 1)//2
            index = bisect_right(banned, target)

            if index - 1 >= 0:
                totalSum -= prefixSum[index - 1]
            return (totalSum, index)

        left = 1
        right = n
        ans = -1
        while left <= right:
            mid = left + (right - left)//2
            totalSum, bannedIndex = good(mid)
            if totalSum <= maxSum:
                left = mid + 1
                ans = mid - bannedIndex
            else:
                right = mid - 1

        return ans
Reactions: LmaoSuVuong
Python:
class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        def isGood(maximumBags):
            needed = 0
            for num in nums:
                if num <= maximumBags:
                    continue

                needed += (math.ceil(num/maximumBags) - 1)

            return needed <= maxOperations

        left = 1
        right = max(nums)
        ans = right
        while left <= right:
            mid = left + (right - left)//2
            if isGood(mid):
                ans = mid
                right = mid - 1
            else:
                left = mid + 1

        return ans
Reactions: nchhnchh
Python:
class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        def valid(t):
            return sum((num - 1) // t for num in nums) <= maxOperations
        
        l, r = 1, max(nums)
        while l <= r:
            mid = l + (r - l) // 2
            if not valid(mid):
                l = mid + 1
            else:
                result = mid
                r = mid - 1
        return result
Python:
class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        def isGood(maximumBags):
            needed = 0
            for num in nums:
                if num <= maximumBags:
                    continue

                needed += (math.ceil(num/maximumBags) - 1)

            return needed <= maxOperations

        left = 1
        right = max(nums)
        ans = right
        while left <= right:
            mid = left + (right - left)//2
            if isGood(mid):
                ans = mid
                right = mid - 1
            else:
                left = mid + 1

        return ans
code dài quá :doubt:
cái dòng chặn continue bác bỏ đi được mà.
Reactions: freedom.9