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

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
LC 432 Map
Java:
class AllOne {
    java.util.Map<String, Integer> map;
    java.util.TreeMap<Integer, java.util.Set<String>> invGrp;
    public AllOne() {
        map = new java.util.HashMap<>();
        invGrp = new java.util.TreeMap<>();
    }
    public void inc(String key) {
        Integer count = map.getOrDefault(key, 0);
        map.put(key, count + 1);
        if (count > 0) {
            removeOld(key, count);
        }
        invGrp.computeIfAbsent(count + 1, java.util.HashSet::new).add(key);
    }
    public void dec(String key) {
        Integer count = map.getOrDefault(key, 0);
        removeOld(key, count);
        if (count > 1) {
            map.put(key, count - 1);
            invGrp.computeIfAbsent(count - 1, java.util.HashSet::new).add(key);
        } else {
            map.remove(key);
        }
    }
    public String getMaxKey() {
        return java.util.Optional.ofNullable(invGrp.lastEntry()).map(x -> x.getValue())
                .orElse(java.util.Collections.emptySet()).stream().reduce((a, b) -> b).orElse("");
    }
    public String getMinKey() {
        return java.util.Optional.ofNullable(invGrp.firstEntry()).map(x -> x.getValue())
                .orElse(java.util.Collections.emptySet()).stream().findFirst().orElse("");
    }
    private void removeOld(String key, Integer count) {
        invGrp.computeIfAbsent(count, java.util.HashSet::new).remove(key);
        if (invGrp.get(count).size() == 0) {
            invGrp.remove(count);
        }
    }
}
 
Sửa lần cuối:
thấy cãi cùn với xài clone nên autoignore 🫣, cãi nhau với mấy dạng đó không ích lợi gì, không học được cái gì, chỉ rước bực vào người
Ngài lambda đang chửi tôi hả
UKiCiKh.png
Có gì tha cho tôi nha tôi có mắt như mù không thấy thái sơn
UKiCiKh.png
 
thấy cãi cùn với xài clone nên autoignore 🫣, cãi nhau với mấy dạng đó không ích lợi gì, không học được cái gì, chỉ rước bực vào người
Thanh niên ronghtieng gì gì đó hình như xưa làm freelancer rồi chửi ae đi làm cty mà, hổng kiến thức tùm lum.
Lâu rồi ko đc làm .Net ngứa nghề phết, xưa làm .Net chắc cũng phải 5 6 năm kinh qua đủ loại từ Asp Net 3.5 tới tận .Net 6 :ah:


via theNEXTvoz for iPhone
 
Python:
class CustomStack:

    def __init__(self, maxSize: int):
        self.stack = []
        self.size = 0
        self.maxSize = maxSize

    def push(self, x: int) -> None:
        if self.size < self.maxSize:
            self.stack.append(x)
            self.size += 1

    def pop(self) -> int:
        if self.size > 0:
            self.size -= 1
            return self.stack.pop()
        return -1

    def increment(self, k: int, val: int) -> None:
        for i in range(min(k, self.size)):
            self.stack[i] += val
 
Thanh niên ronghtieng gì gì đó hình như xưa làm freelancer rồi chửi ae đi làm cty mà, hổng kiến thức tùm lum.
Lâu rồi ko đc làm .Net ngứa nghề phết, xưa làm .Net chắc cũng phải 5 6 năm kinh qua đủ loại từ Asp Net 3.5 tới tận .Net 6 :ah:


via theNEXTvoz for iPhone
siêu nhưn :shame:
 
Bài này sao medium dc nhỉ @@
Java:
class CustomStack {
    int[] arr;
    int index;
    int max;

    public CustomStack(int maxSize) {
        this.arr = new int[maxSize];
        this.max = maxSize;
    }
    
    public void push(int x) {
        if (index == max)
            return;

        arr[index++] = x;
    }
    
    public int pop() {
        if (index == 0)
            return -1;
        
        return arr[--index];
    }
    
    public void increment(int k, int val) {
        for (int i = 0; i < k && i < max; i++) {
            arr[i] += val;
        }
    }
}
 
Java:
class CustomStack {

    int[] stack;
    int size;
    int index;

    public CustomStack(int maxSize) {
        size = maxSize;
        stack = new int[size];
        index = 0;
    }
    
    public void push(int x) {
        if (index >= size) return;
        stack[index++] = x;
    }
    
    public int pop() {
        if (index == 0) return -1;
        return stack[--index];
    }
    
    public void increment(int k, int val) {
        for (int i = 0; i < k; i++) {
            if (i >= size) return;
            if (stack[i] != 0) stack[i] += val;
        }
    }
}

/**
 * Your CustomStack object will be instantiated and called as such:
 * CustomStack obj = new CustomStack(maxSize);
 * obj.push(x);
 * int param_2 = obj.pop();
 * obj.increment(k,val);
 */
Bài hôm qua khó quá nên cook
XZlCqK8.png
 
Sửa lần cuối:
siêu nhưn :shame:
Già rồi đi làm từ hồi năm 3 cũng ngót nghét cũng 7 8 năm cmnr :too_sad:

via theNEXTvoz for iPhone
 
Bài này sao medium dc nhỉ @@
Java:
class CustomStack {
    int[] arr;
    int index;
    int max;

    public CustomStack(int maxSize) {
        this.arr = new int[maxSize];
        this.max = maxSize;
    }
   
    public void push(int x) {
        if (index == max)
            return;

        arr[index++] = x;
    }
   
    public int pop() {
        if (index == 0)
            return -1;
       
        return arr[--index];
    }
   
    public void increment(int k, int val) {
        for (int i = 0; i < k && i < max; i++) {
            arr[i] += val;
        }
    }
}
Bài này nếu mà optimal thì cái increment cũng phải O1, lúc đấy thì có mà kêu oai oái :D
 
Nhìn thì đơn giản mà code ăn mấy cái bug chỗ k = 0 ấy chứ, quá lừa
Python:
class CustomStack:
    def __init__(self, maxSize: int):
        self.maxSize = maxSize
        self.stack = []
        self.increments = [0]*maxSize

    def push(self, x: int) -> None:
        if len(self.stack) == self.maxSize:
            return -1

        self.stack.append(x)

    def pop(self) -> int:
        if len(self.stack) == 0:
            return -1

        index = len(self.stack) - 1
        ans = self.stack.pop(-1) + self.increments[index]
        if index > 0:
            self.increments[index - 1] += self.increments[index]
       
        self.increments[index] = 0
        return ans

    def increment(self, k: int, val: int) -> None:
        index = min(k - 1, len(self.stack) - 1)
        if index >= 0:
            self.increments[index] += val

# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)
 
Nhìn thì đơn giản mà code ăn mấy cái bug chỗ k = 0 ấy chứ, quá lừa
Python:
class CustomStack:
    def __init__(self, maxSize: int):
        self.maxSize = maxSize
        self.stack = []
        self.increments = [0]*maxSize

    def push(self, x: int) -> None:
        if len(self.stack) == self.maxSize:
            return -1

        self.stack.append(x)

    def pop(self) -> int:
        if len(self.stack) == 0:
            return -1

        index = len(self.stack) - 1
        ans = self.stack.pop(-1) + self.increments[index]
        if index > 0:
            self.increments[index - 1] += self.increments[index]
     
        self.increments[index] = 0
        return ans

    def increment(self, k: int, val: int) -> None:
        index = min(k - 1, len(self.stack) - 1)
        if index >= 0:
            self.increments[index] += val

# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)
Chính ra bài này phải nâng số lần thao tác lên 10^5 thì mới tính là medium.
 
LC 1381 List
Java:
class CustomStack {
    java.util.List<Integer> st;
    int cp;

    public CustomStack(int maxSize) {
        st = new java.util.ArrayList<>(cp = maxSize);
    }

    public void push(int x) {
        if (st.size() < cp) st.add(x);
    }

    public int pop() {
        return (st.size() == 0) ? -1 : st.remove(st.size() - 1);
    }

    public void increment(int k, int val) {
        java.util.stream.IntStream.range(0, Math.min(st.size(), k)).forEach(i -> st.set(i, st.get(i) + val));
    }
}
 
Sửa lần cuối:
Mã:
class CustomStack {
    int[] arr;
    int[] increment;
    int idx, n;
    public CustomStack(int maxSize) {
        this.n = maxSize;
        this.arr = new int[n];
        this.increment = new int[n];
        this.idx = -1;
    }
    
    public void push(int x) {
        if (idx < n - 1) {
            arr[++idx] = x;
        }
    }
    
    public int pop() {
        int res = -1;
        if (idx >= 0) {
            res = arr[idx] + increment[idx];
            if (idx > 0) {
                increment[idx - 1] += increment[idx];
            }
            increment[idx] = 0;
            idx--;
        }

        return res;
    }
    
    public void increment(int k, int val) {
        if (idx >= 0) {
            if (k - 1 < idx) {
                increment[k - 1] += val;
            } else {
                increment[idx] += val;
            }
        }
    }
}
 
Java:
class CustomStack {
    int[] arr;
    int[] inc;
    int index;
    int max;

    public CustomStack(int maxSize) {
        this.arr = new int[maxSize];
        this.inc = new int[maxSize];
        this.max = maxSize;
    }
    
    public void push(int x) {
        if (index == max)
            return;

        inc[index] = 0;
        arr[index++] = x;
    }
    
    public int pop() {
        if (index == 0)
            return -1;
        
        if (--index > 0) {
            inc[index - 1] += inc[index];
        }
        return arr[index] + inc[index];
    }
    
    public void increment(int k, int val) {
        if (k == 0 || index == 0) return;

        if (k >= index)
            inc[index - 1] += val;
 
        else
            inc[k - 1] += val;
    }
}
 
bài hôm nay là kiến thức phổ thông hay tricky nhỉ, 2 ngày hôm nay chả có tí ý tưởng O(1) nào
tf95Xbz.png
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.215.683
Quay lại
Lên đầu trang