Minimalist Forum Reader
C++:
class Solution {
public:
    int maximumLength(string s) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        map<char, vector<int>> hash;
        int n=s.size();
        vector<int> count(n, 0);
        for (int i=0; i<n; i++) {
            if (i>0 && s[i]==s[i-1]) {
                count[i]=count[i-1]+1;
            } else {
                count[i]=1;
            }
            hash[s[i]].push_back(count[i]);
        }
        for (char c='a'; c<='z'; c++) {
            sort(hash[c].begin(), hash[c].end());
        }
        int res=-1;
        for (char c='a'; c<='z'; c++) {
            int tempSize=hash[c].size();
            if (tempSize<3) continue;
            else {
                res=max(res, min(hash[c][tempSize-1], min(hash[c][tempSize-2], hash[c][tempSize-3])));
            }
        }
        return res;
    }
};
bác chỉ cần maintain 3 thằng lớn nhất của mỗi kí tự thôi, chứ bác tìm xong hết độ dài của từng kí tự rồi mới sort lại thì NlogN đúng r :oops:
Reactions: humanversion9x
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        lines = defaultdict(int)
        for num in nums:
            lines[num - k] += 1
            lines[num + k + 1] -=1

        lines = sorted(lines.items())
        sumSofar = 0
        ans = 0
        for key, val in lines:
            sumSofar += val
            ans = max(ans, sumSofar)

        return ans
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:

        line = defaultdict(int)
        for num in nums:
            line[num - k] += 1
            line[num + k + 1] -= 1
        
        sorted_keys = sorted(line)
        result, curr = 0, 0

        for key in sorted_keys:
            curr += line[key]
            result = max(result, curr)

        return result
Reactions: freedom.9
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        nums.sort()
        n = len(nums)
        l = 1
        r = n
        def can_match(m):
            for i in range(n - m):
                if nums[i] + k >= nums[i + m] - k:
                    return True
            return False
        while l <= r:
            mid = (l + r) // 2
            if can_match(mid):
                l = mid + 1
            else:
                r = mid - 1
        return l

Còn cách nữa prefix sum On ảo lòi, méo thấy trong tag :beat_brick:
Kế hoạch bài này: Chuyền nums -> intervals -> tìm overlaps :misdoubt:
Làm 3 lần tốc độ tăng dần
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    const intervals = nums.map(num => [num - k, num + k]);
    const arr: [number, number][] = [];
    for (const [s, e] of intervals) {
        arr.push([s, 1]);
        arr.push([e + 1, -1]);
    }
    arr.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
    let res = 0, cur = 0;
    for (const [, num] of arr) {
        cur += num;
        res = Math.max(res, cur);
    }
    return res;
}
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    nums.sort((a, b) => a - b);
    let r = 0, res = 0, n = nums.length;

    for (let l = 0; l < n; l++) {
        while (r < n && nums[r] - nums[l] <= 2 * k) r++;
        res = Math.max(res, r - l);
    }
    return res;
}
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    const max = Math.max(...nums) + k * 2 + 2;
    const arr = Array(max).fill(0);
    for (const x of nums) {
        arr[x]++;
        arr[x + k * 2 + 1]--;
    }
    let res = 0, cur = 0
    for (const x of arr) {
        cur += x;
        res = Math.max(res, cur);
    }
    return res;
}
Reactions: Mỹ Chu Lang, nchhnchh and Phó GOAT
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        nums = sorted(nums)
        l = 0
        ans = 0
        for r in range(0, len(nums)):
            while (nums[r] - nums[l]) > (k * 2):
                l += 1
            ans = max(ans, r - l + 1)
        return ans
Code:
class Solution {
    public int maximumBeauty(int[] nums, int k) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for(int num:nums){
            map.put(num-k, map.getOrDefault(num-k,0)+1);
            map.put(num+k+1, map.getOrDefault(num+k+1,0)-1);
        }
        int ans =0;
        int lines= 0;
        for(int key:map.keySet()){
            lines+=map.get(key);
            ans=Math.max(ans, lines);
        }
        return ans;
    }
    
}
Kế hoạch bài này: Chuyền nums -> intervals -> tìm overlaps :misdoubt:
Làm 3 lần tốc độ tăng dần
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    const intervals = nums.map(num => [num - k, num + k]);
    const arr: [number, number][] = [];
    for (const [s, e] of intervals) {
        arr.push([s, 1]);
        arr.push([e + 1, -1]);
    }
    arr.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
    let res = 0, cur = 0;
    for (const [, num] of arr) {
        cur += num;
        res = Math.max(res, cur);
    }
    return res;
}
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    nums.sort((a, b) => a - b);
    let r = 0, res = 0, n = nums.length;

    for (let l = 0; l < n; l++) {
        while (r < n && nums[r] - nums[l] <= 2 * k) r++;
        res = Math.max(res, r - l);
    }
    return res;
}
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    const max = Math.max(...nums) + k * 2 + 2;
    const arr = Array(max).fill(0);
    for (const x of nums) {
        arr[x]++;
        arr[x + k * 2 + 1]--;
    }
    let res = 0, cur = 0
    for (const x of arr) {
        cur += x;
        res = Math.max(res, cur);
    }
    return res;
}
cách cuối cũng là linesweep mà. cho nums<=10^9 oẳng space limit. cho vào array thì dc cái couting sort sẵn
Reactions: freedom.9
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        d = defaultdict(int)
        for num in nums:
            d[num - k] += 1
            d[num + k + 1] -= 1
        return max(accumulate(d[k] for k in sorted(d.keys())))

Python:
class Solution:
  def maximumBeauty(self, nums: List[int], k: int) -> int:
    nums.sort()
    l = 0
    for n in nums:
      if nums[l] + 2 * k < n: l += 1
    return len(nums) - l

C++:
class Solution {
public:
  int maximumBeauty(vector<int>& nums, int k) {
    sort(nums.begin(), nums.end());
    int l = 0;
    for (int n: nums) {
        if(nums[l] + 2 * k < n) l++;
    }
    return nums.size() - l;
  }
};
cách cuối cũng là linesweep mà. cho nums<=10^9 oẳng space limit. cho vào array thì dc cái couting sort sẵn
Nãy giờ chỉ có sư đệ nhìn ra cái đấy là line sweep nhỉ, sư đệ nay đã lớn khôn :shame: +1 respect.
Cách đấy On mà là On giả cầy thôi, đấy là cách bruteforce fake thôi.


via theNEXTvoz for iPhone
Reactions: LmaoSuVuong
JavaScript:
function maximumBeauty(nums: number[], k: number): number {
    nums.sort((a, b) => a - b)

    let l = 0
    let result = 0
    for (let r = 0; r < nums.length; r++) {
        if (nums[r] - nums[l] <= 2 * k) {
            result = Math.max(result, r - l + 1)
        } else {
            l++
        }
    }

    return result
};
JavaScript:
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var maximumBeauty = function (nums, k) {
    nums.sort((u, v) => u - v);
    const n = nums.length;
    let ans = 0;
    for (let i = 0, j = 0; i < n; i++) {
        while (nums[j] + k < nums[i] - k) {
            j++;
        }
        ans = Math.max(ans, i - j + 1);
    }
    return ans;
};
C++:
class Solution {
public:
    int maximumBeauty(vector<int>& nums, int k) {
        int res = 0;
        sort(nums.begin(), nums.end());
        for (auto it = begin(nums); it != end(nums); ++it)
            res = max(res, (int)distance(it, upper_bound(it, end(nums), *it + 2 * k)));
        return res;
    }
};
đọc hint mới biết làm :beat_brick:
Java:
class Solution {
    public int maximumBeauty(int[] nums, int k) {
        Arrays.sort(nums);
        int right = 0;
        int result = 0;
        for(int i = 0;i<nums.length;i++){

            while(right < nums.length && nums[right] - nums[i] <= 2*k){
                right++;
            }

            result = Math.max(result, right - i);
        }

        return result;
    }
}
Swift:
class Solution {
    func maximumBeauty(_ nums: [Int], _ k: Int) -> Int {
        let nums = nums.sorted()
        var (k, left, result) = (k*2, 0, 1)
        for (right, num) in nums.enumerated() {
            while num - nums[left] > k {
                left += 1
            }
            result = max(result, right-left+1)
        }
        return result
    }
}
giờ thớt này đc ghim luôn à, xịn nhỉ :beauty:
Java:
class Solution {
    public int maximumBeauty(int[] nums, int k) {
        Arrays.sort(nums);
        int l=0, r=0, res = 0, n = nums.length;
        while(r < n){
            if(nums[r] - nums[l] <= 2*k)
                r++;
            else{
                res = Math.max(res, r-l);
                while(nums[r] - nums[l] > 2*k)
                    l++;
            }
            if(r == n)
                res = Math.max(res, r-l);
        }
        return res;
    }
}
Python:
class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = 0
        for num in nums:
            if num - nums[left] > 2 * k:
                left += 1
        return len(nums) - left
Noob question, poorly written.
Python:
class Solution:
    def pickGifts(self, gifts: List[int], k: int) -> int:
        heap = []
        for gift in gifts:
            heapq.heappush(heap, -1*gift)
        
        while k:
            item = heapq.heappop(heap)
            item*=-1
            heapq.heappush(heap, -1*floor(sqrt(item)))
            k-=1

        return sum(heap)*-1
Java:
class Solution {
    public long pickGifts(int[] gifts, int k) {
        long ans = 0l;
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
        for (int gift : gifts) {
            pq.offer(gift);
        }
        while (k > 0) {
            int curr = pq.poll();
            double left = Math.sqrt((double) curr);
            pq.offer((int) left);
            k--;
        }
        while (!pq.isEmpty()) {
            ans += pq.poll();
        }
        return ans;
    }
}
Python:
class Solution:
    def pickGifts(self, gifts: List[int], k: int) -> int:
        heap = [-g for g in gifts]
        heapq.heapify(heap)

        for _ in range(k):
            maxGift = -heapq.heappop(heap)
            heapq.heappush(heap, -int(maxGift ** 0.5))
        
        return -sum(heap)