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.
Chắc mò lên MIT với tải sách về cày lại, má 05 năm ko làm giờ đụng vào chắc thua thằng sinh viên năm hai quá :eek:
thua thằng học sinh cấp 2 fen
qZV215Z.png
 
Chắc mò lên MIT với tải sách về cày lại, má 05 năm ko làm giờ đụng vào chắc thua thằng sinh viên năm hai quá :eek:
Bảo thua sinh viên năm 2 lại khinh vozers quá, toy biết có mấy sinh viên năm 2 trên này rating phải 2k3 đổ lên
zFNuZTA.gif

Mua premium tranh thủ đi đang có discount
via theNEXTvoz for iPhone
 
Sửa lần cuối:
Java:
class Solution {
    public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
       
        int n = nums.length;
        int ans =0;
        int f_sum = max(nums,0, firstLen,firstLen );
        int s_sum =max(nums,firstLen, n,secondLen);
        ans=f_sum+s_sum;
        for(int i = firstLen;i<n;i++){
            f_sum-=nums[i-firstLen];
            f_sum+=nums[i];
            s_sum=0;
            if(i+1>=firstLen+secondLen){
                s_sum = Math.max(s_sum,max(nums,0,i+1-firstLen,secondLen));
            }
            if(i<n-secondLen){
                s_sum = Math.max(s_sum,max(nums,i+1,n,secondLen));
            }
            ans=Math.max(ans, f_sum+s_sum);
        }
        return ans;
    }
    public int max(int[]nums, int from, int to, int len){
        int max =0;
        for(int i =from ; i <from+len;i++){
            max+=nums[i];
        }
        int cur_sum = max;
        for(int i =from+len;i<to;i++){
            cur_sum-=nums[i-len];
            cur_sum+=nums[i];
            max=Math.max(max,cur_sum);
        }
        return max;
    }
}
 
:D Các em giờ đỉnh quá, giờ mà tôi đi phỏng vấn khéo còn thua các em đấy ấy haha.

Đã mua premium, mà chưa biết bắt đầu từ đâu đây.
Còn phải nói, ko học là bị các em đá đít ngay
zFNuZTA.gif

Mình có thằng cháu đang học cuối năm 2 FU mà môn nào cũng 9 10, English bắn ầm ầm giờ còn đang học algorithm giải nhoay nhoáy
4gmOAMB.gif

via theNEXTvoz for iPhone
 
:D Ngồi từ 7h tối cơm cháo xong, nghịch tí mấy bài easy ngó lên đã 1r sáng cmnr. Hơi đuối,nhớ ngày xưa try hard cùng anh em bạn bè có những hôm ngồi 30-36 tiếng cày ko biết mệt, kỉ niệm vch, giờ ngồi có mấy tiếng là oải cmnr.

Đúng là nghiệp thợ code, mình ngày xưa ngu toán với ngu thuật vcl.Đi thi toàn ae đọc thuật cho ngồi code,đến giờ càng ngu.
 
:D Ngồi từ 7h tối cơm cháo xong, nghịch tí mấy bài easy ngó lên đã 1r sáng cmnr. Hơi đuối,nhớ ngày xưa try hard cùng anh em bạn bè có những hôm ngồi 30-36 tiếng cày ko biết mệt, kỉ niệm vch, giờ ngồi có mấy tiếng là oải cmnr.

Đúng là nghiệp thợ code, mình ngày xưa ngu toán với ngu thuật vcl.Đi thi toàn ae đọc thuật cho ngồi code,đến giờ càng ngu.
sáng mai 9h30 sáng chủ nhật lên làm contest nha bác
 
Python:
class Solution:
    def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
        n = len(nums)

        def process(len1, len2):
            maxLeft = [0]*n
            maxRight = [0]*n
            ans = 0
            right = n - 1
            sumSofar = 0
            for left in range(n - 1, -1, -1):
                sumSofar += nums[left]
                if right - left + 1 == len2:
                    total = maxRight[left + 1] if left + 1 < n else 0
                    maxRight[left] = max(sumSofar, total)
                    sumSofar -= nums[right]
                    right -= 1

            left = 0
            sumSofar = 0
            for right in range(n - len2):
                sumSofar += nums[right]
                if right - left + 1 == len1:
                    total = maxRight[right + 1] if right + 1 < n else 0

                    ans = max(ans, sumSofar + total)
                    sumSofar -= nums[left]
                    left += 1

            return ans

        return max(process(firstLen, secondLen), process(secondLen, firstLen))
 
Python:
class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        for i in range(len(arr) - 1):
            for j in range(i + 1, len(arr)):
                if arr[i] == arr[j] * 2 or arr[i] * 2 == arr[j]:
                    return True
        return False
 
Java:
class Solution {
    public boolean checkIfExist(int[] arr) {
        Set<Integer> hs = new HashSet();
        for(int num:arr){
            if(hs.contains(num*2)) return true;
            if(num%2==0 && hs.contains(num/2)) return true;
            hs.add(num);
        }
        return false;
    }
}
 
Swift:
class Solution {
    func checkIfExist(_ arr: [Int]) -> Bool {
        var setA:Set<Int> = []
        for num in arr {
            if setA.contains(num*2) { return true }
            if num & 1 == 0 && setA.contains(num/2) { return true }
            setA.insert(num)
        }
        return false
    }
}
 
Python:
class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        c = Counter(arr)
        for n in arr:
            if c[n] and c[2*n]:
                if n == 0:
                    if c[n] >= 2:
                        return True
                else:
                    return True

        return False
 
JavaScript:
var checkIfExist = function(arr) {
    const set = new Set();
    for(const num of arr){
        if(set.has(num * 2) || set.has(num / 2)) return true;
        set.add(num);
    }
    return false;
};
 
màu xanh đã trở lại trên mảnh đất của chúng ta :big_smile:
Java:
class Solution {
    public boolean checkIfExist(int[] arr) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i:arr){         
            if((i&1)==0 && set.contains(i/2) || set.contains(i*2))
                return true;
            set.add(i);
        }
        return false;
    }
}
 
C#:
public class Solution {
    public bool CheckIfExist(int[] arr) {
        var set = new HashSet<int>();
        var set2 = new HashSet<int>();
        for (int i = 0; i < arr.Length; i++)
        {
            if (set2.Contains(arr[i]) || set.Contains(arr[i]*2))
                return true;
            else
            {
                set2.Add(arr[i]*2);
                set.Add(arr[i]);
            }
        }
        return false;
    }
}
 
LC 1346 Java
Java:
class Solution {
    public boolean checkIfExist(int[] a) {
        for (int i = 0; i < a.length; i++) if (ls(a, (float) a[i] / 2, i)) return true; return false;
    }

    static boolean ls(int[] a, float t, int i) {
        for (int j = 0; j < a.length; j++) if ((float) a[j] == t) return j != i; return false;
    }
}
 
Toang quá

Xem tệp đính kèm 2809444
Bài 4 đánh dấu chẵn lẻ là ra ko cần quan tâm tâm root nào, cứ nghĩ là phải tính từ mỗi root. Dễ mà e cứ nghĩ phức tạp nên buông
Rank 2k :cry:

Code lại 5 phút là ra bài 4 rùi, vào forum sớm thì chắc clear :too_sad:
Xem tệp đính kèm 2809475
1733037087121.png
bác code gọn thật, vừa virtual xong, e implement hơi chậm nên ko làm live :cry:
trả bài contest:
Java:
class Solution {
    public int smallestNumber(int n) {
        int i =1;
        while(i<n){
            i<<=1;
            i+=1;
        }
        return i;
    }
}
Java:
class Solution {
    public int getLargestOutlier(int[] nums) {
        int sum =0 ;
        Map<Integer,Integer> hm  = new HashMap();
        for(int num:nums){
            hm.put(num, hm.getOrDefault(num,0)+1);
            sum+=num;
        }
        int ans = -1001;
        
      
        for(int num:nums){
           if(Math.abs(sum-num)%2==0){
               hm.put(num, hm.get(num)-1);
               if(hm.getOrDefault((sum-num)/2,0)>0){
                   ans=Math.max(ans,num);
               }
               hm.put(num, hm.get(num)+1);
           }
        }
        return ans;
    }
}
Java:
class Solution {
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
        Map<Integer,List<Integer>> graph1 = new HashMap<>();
        Map<Integer,List<Integer>> graph2 = new HashMap<>();
        int n =0;
        int m =0;
        for(int[] edge:edges1){
            graph1.putIfAbsent(edge[0], new ArrayList());
            graph1.putIfAbsent(edge[1], new ArrayList());
            graph1.get(edge[0]).add(edge[1]);
            graph1.get(edge[1]).add(edge[0]);
            n = Math.max(n, edge[0]+1);
            n = Math.max(n, edge[1]+1);
        }
        for(int[] edge:edges2){
            graph2.putIfAbsent(edge[0],new ArrayList());
            graph2.putIfAbsent(edge[1], new ArrayList());
            graph2.get(edge[0]).add(edge[1]);
            graph2.get(edge[1]).add(edge[0]);
            m = Math.max(m, edge[0]+1);
            m = Math.max(m, edge[1]+1);
        }
         int[] c2 = new int[m];
        int maxc2 = 0;
        for(int i=0;i<m;i++){
            c2[i]= solver(graph2, i,k-1);
            maxc2 =Math.max(maxc2,c2[i]);
        }
        int[] connections = new int[n];
        for(int i=0;i<n;i++){
             connections[i]=solver(graph1, i,k) + maxc2;
        }
        return connections;
    }
    public int solver(Map<Integer,List<Integer>> graph, int v, int k){
        if(k<0) return 0;
        Queue<Integer> q = new LinkedList();
        Set<Integer>visited= new HashSet();
        int dist=0;
        q.add(v);
        visited.add(v);
        while(!q.isEmpty() && dist<k){
            int len = q.size();
            for(int i =0;i<len;i++){
                int node = q.poll();
                for(int child:graph.get(node)){
                    if(!visited.contains(child)){
                        q.add(child);

                        visited.add(child);
                    }
                }
            }
            dist++;
            
        }

        return visited.size();
    }
}
Java:
class Solution {
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2) {
        Map<Integer, List<Integer>> graph1 = new HashMap<>();
        Map<Integer, List<Integer>> graph2 = new HashMap<>();
        int n = 0;
        int m = 0;
        for (int[] edge : edges1) {
            graph1.putIfAbsent(edge[0], new ArrayList());
            graph1.putIfAbsent(edge[1], new ArrayList());
            graph1.get(edge[0]).add(edge[1]);
            graph1.get(edge[1]).add(edge[0]);
            n = Math.max(n, edge[0] + 1);
            n = Math.max(n, edge[1] + 1);
        }
        for (int[] edge : edges2) {
            graph2.putIfAbsent(edge[0], new ArrayList());
            graph2.putIfAbsent(edge[1], new ArrayList());
            graph2.get(edge[0]).add(edge[1]);
            graph2.get(edge[1]).add(edge[0]);
            m = Math.max(m, edge[0] + 1);
            m = Math.max(m, edge[1] + 1);
        }

        int maxc2 = solver(graph2, 0);
        maxc2 = Math.max(maxc2, m - maxc2);
        int[] connections = new int[n];
        connections[0] = solver(graph1, 0);
        Queue<Integer> q = new LinkedList();
        Set<Integer> visited = new HashSet();
        q.add(0);
        visited.add(0);
        int dist=0;
        while (!q.isEmpty()) {
            int len = q.size();
            for (int i = 0; i < len; i++) {
                int node = q.poll();
                for (int child : graph1.get(node)) {
                    if (!visited.contains(child)) {
                        q.add(child);
                        if (dist % 2 == 1){
                            connections[child] = connections[0] + maxc2;
                        }else{
                            connections[child] = n-connections[0]+maxc2;
                        }
                        
                        visited.add(child);
                    }
                }
            }dist++;

        }
        connections[0]+=maxc2;
        return connections;
    }

    public int solver(Map<Integer, List<Integer>> graph, int v) {
        Queue<Integer> q = new LinkedList();
        Set<Integer> visited = new HashSet();
        int dist = 0;
        q.add(v);
        visited.add(v);
        int cnt = 0;
        while (!q.isEmpty()) {
            int len = q.size();
            if (dist % 2 == 0)
                cnt += len;
            for (int i = 0; i < len; i++) {
                int node = q.poll();
                for (int child : graph.get(node)) {
                    if (!visited.contains(child)) {
                        q.add(child);

                        visited.add(child);
                    }
                }
            }
            dist++;

        }
        return cnt;
    }
}
 
Xem tệp đính kèm 2809649bác code gọn thật, vừa virtual xong, e implement hơi chậm nên ko làm live :cry:
trả bài contest:
Java:
class Solution {
    public int smallestNumber(int n) {
        int i =1;
        while(i<n){
            i<<=1;
            i+=1;
        }
        return i;
    }
}
Java:
class Solution {
    public int getLargestOutlier(int[] nums) {
        int sum =0 ;
        Map<Integer,Integer> hm  = new HashMap();
        for(int num:nums){
            hm.put(num, hm.getOrDefault(num,0)+1);
            sum+=num;
        }
        int ans = -1001;
       
     
        for(int num:nums){
           if(Math.abs(sum-num)%2==0){
               hm.put(num, hm.get(num)-1);
               if(hm.getOrDefault((sum-num)/2,0)>0){
                   ans=Math.max(ans,num);
               }
               hm.put(num, hm.get(num)+1);
           }
        }
        return ans;
    }
}
Java:
class Solution {
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
        Map<Integer,List<Integer>> graph1 = new HashMap<>();
        Map<Integer,List<Integer>> graph2 = new HashMap<>();
        int n =0;
        int m =0;
        for(int[] edge:edges1){
            graph1.putIfAbsent(edge[0], new ArrayList());
            graph1.putIfAbsent(edge[1], new ArrayList());
            graph1.get(edge[0]).add(edge[1]);
            graph1.get(edge[1]).add(edge[0]);
            n = Math.max(n, edge[0]+1);
            n = Math.max(n, edge[1]+1);
        }
        for(int[] edge:edges2){
            graph2.putIfAbsent(edge[0],new ArrayList());
            graph2.putIfAbsent(edge[1], new ArrayList());
            graph2.get(edge[0]).add(edge[1]);
            graph2.get(edge[1]).add(edge[0]);
            m = Math.max(m, edge[0]+1);
            m = Math.max(m, edge[1]+1);
        }
         int[] c2 = new int[m];
        int maxc2 = 0;
        for(int i=0;i<m;i++){
            c2[i]= solver(graph2, i,k-1);
            maxc2 =Math.max(maxc2,c2[i]);
        }
        int[] connections = new int[n];
        for(int i=0;i<n;i++){
             connections[i]=solver(graph1, i,k) + maxc2;
        }
        return connections;
    }
    public int solver(Map<Integer,List<Integer>> graph, int v, int k){
        if(k<0) return 0;
        Queue<Integer> q = new LinkedList();
        Set<Integer>visited= new HashSet();
        int dist=0;
        q.add(v);
        visited.add(v);
        while(!q.isEmpty() && dist<k){
            int len = q.size();
            for(int i =0;i<len;i++){
                int node = q.poll();
                for(int child:graph.get(node)){
                    if(!visited.contains(child)){
                        q.add(child);

                        visited.add(child);
                    }
                }
            }
            dist++;
           
        }

        return visited.size();
    }
}
Java:
class Solution {
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2) {
        Map<Integer, List<Integer>> graph1 = new HashMap<>();
        Map<Integer, List<Integer>> graph2 = new HashMap<>();
        int n = 0;
        int m = 0;
        for (int[] edge : edges1) {
            graph1.putIfAbsent(edge[0], new ArrayList());
            graph1.putIfAbsent(edge[1], new ArrayList());
            graph1.get(edge[0]).add(edge[1]);
            graph1.get(edge[1]).add(edge[0]);
            n = Math.max(n, edge[0] + 1);
            n = Math.max(n, edge[1] + 1);
        }
        for (int[] edge : edges2) {
            graph2.putIfAbsent(edge[0], new ArrayList());
            graph2.putIfAbsent(edge[1], new ArrayList());
            graph2.get(edge[0]).add(edge[1]);
            graph2.get(edge[1]).add(edge[0]);
            m = Math.max(m, edge[0] + 1);
            m = Math.max(m, edge[1] + 1);
        }

        int maxc2 = solver(graph2, 0);
        maxc2 = Math.max(maxc2, m - maxc2);
        int[] connections = new int[n];
        connections[0] = solver(graph1, 0);
        Queue<Integer> q = new LinkedList();
        Set<Integer> visited = new HashSet();
        q.add(0);
        visited.add(0);
        int dist=0;
        while (!q.isEmpty()) {
            int len = q.size();
            for (int i = 0; i < len; i++) {
                int node = q.poll();
                for (int child : graph1.get(node)) {
                    if (!visited.contains(child)) {
                        q.add(child);
                        if (dist % 2 == 1){
                            connections[child] = connections[0] + maxc2;
                        }else{
                            connections[child] = n-connections[0]+maxc2;
                        }
                       
                        visited.add(child);
                    }
                }
            }dist++;

        }
        connections[0]+=maxc2;
        return connections;
    }

    public int solver(Map<Integer, List<Integer>> graph, int v) {
        Queue<Integer> q = new LinkedList();
        Set<Integer> visited = new HashSet();
        int dist = 0;
        q.add(v);
        visited.add(v);
        int cnt = 0;
        while (!q.isEmpty()) {
            int len = q.size();
            if (dist % 2 == 0)
                cnt += len;
            for (int i = 0; i < len; i++) {
                int node = q.poll();
                for (int child : graph.get(node)) {
                    if (!visited.contains(child)) {
                        q.add(child);

                        visited.add(child);
                    }
                }
            }
            dist++;

        }
        return cnt;
    }
}
code python đương nhiên ngắn hơn java rồi, vào contest thi luôn chứ fen cho nó lên điểm
 
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