头像

hpstory

专注C++++




离线:10小时前


最近来访(211)
用户头像
snouuy
用户头像
violet_garden
用户头像
吃花椒的冰
用户头像
18985082146
用户头像
Ref
用户头像
kaichiao
用户头像
Oliver_31
用户头像
CNRaider
用户头像
99的手速加上1的运气
用户头像
雨剑
用户头像
Fluent
用户头像
爱算法爱生活
用户头像
MudNewBie
用户头像
GRID
用户头像
Adam_
用户头像
不高兴的兽奶
用户头像
AisingioroHao
用户头像
xiaoYun_succ
用户头像
春秋_2
用户头像
天之彼方


hpstory
1天前

C# 代码

public class FreqStack {
    private Dictionary<int, Stack<int>> stack;
    private Dictionary<int, int> count;
    private int index;

    public FreqStack() {
        this.stack = new Dictionary<int, Stack<int>>();
        this.count = new Dictionary<int, int>();
        this.index = 0;
    }

    public void Push(int val) {
        if (!count.ContainsKey(val)){
            count.Add(val, 0);            
        }

        count[val]++;

        index = Math.Max(index, count[val]);
        if (!stack.ContainsKey(count[val])){
            stack.Add(index, new Stack<int>());
        }

        stack[count[val]].Push(val);
    }

    public int Pop() {
        int result = stack[index].Pop();
        if (stack[index].Count == 0) index--;
        count[result]--;
        return result;
    }
}


活动打卡代码 LeetCode 895. 最大频率栈

hpstory
1天前
public class FreqStack {
    private Dictionary<int, Stack<int>> stack;
    private Dictionary<int, int> count;
    private int index;

    public FreqStack() {
        this.stack = new Dictionary<int, Stack<int>>();
        this.count = new Dictionary<int, int>();
        this.index = 0;
    }

    public void Push(int val) {
        if (!count.ContainsKey(val)){
            count.Add(val, 0);            
        }

        count[val]++;

        index = Math.Max(index, count[val]);
        if (!stack.ContainsKey(count[val])){
            stack.Add(index, new Stack<int>());
        }

        stack[count[val]].Push(val);
    }

    public int Pop() {
        int result = stack[index].Pop();
        if (stack[index].Count == 0) index--;
        count[result]--;
        return result;
    }
}



hpstory
2天前

C# 代码

public class Solution {
    public int NumFactoredBinaryTrees(int[] arr) {
        int mod = 1000000007;
        int n = arr.Length;
        Array.Sort(arr);
        long[] dp = new long[n];
        Array.Fill(dp, 1);
        Dictionary<int, int> dict = new Dictionary<int, int>();
        long result = 0;
        for (int i = 0; i < n; i++){
            if (!dict.ContainsKey(arr[i])) dict.Add(arr[i], i);
            for (int j = 0; j < i; j++){
                if (arr[i] % arr[j] == 0 && dict.ContainsKey(arr[i] / arr[j])){                    
                    dp[i] = dp[i] + dp[j] * dp[dict[arr[i] / arr[j]]];
                }
            }

            result = (result + dp[i]) % mod;
        }

        return (int)result;
    }
}



hpstory
2天前
public class Solution {
    public int NumFactoredBinaryTrees(int[] arr) {
        int mod = 1000000007;
        int n = arr.Length;
        Array.Sort(arr);
        long[] dp = new long[n];
        Array.Fill(dp, 1);
        Dictionary<int, int> dict = new Dictionary<int, int>();
        long result = 0;
        for (int i = 0; i < n; i++){
            if (!dict.ContainsKey(arr[i])) dict.Add(arr[i], i);
            for (int j = 0; j < i; j++){
                if (arr[i] % arr[j] == 0 && dict.ContainsKey(arr[i] / arr[j])){                    
                    dp[i] = dp[i] + dp[j] * dp[dict[arr[i] / arr[j]]];
                }
            }

            result = (result + dp[i]) % mod;
        }

        return (int)result;
    }
}



hpstory
2天前

C# 代码

public class Solution {
    public int CountSubarrays(int[] nums, int k) {
        int n = nums.Length;
        int[] s = new int[n];
        int t = 0;
        for (int i = 0; i < n; i++){
            if (nums[i] == k){
                s[i] = 0;
                t = i;
            }
            else if (nums[i] < k){
                s[i] = -1;
            }
            else {
                s[i] = 1;
            }
        }

        Dictionary<int, int> dict = new Dictionary<int, int>();
        // left, right模拟前后缀和数组
        // k自己也算一个结果,result初始化为1
        // k作为中位数时, 子数组在s中的和为0或者1
        int left = 0, right = 0;
        int result = 1;
        for (int i = t - 1; i >= 0; i--){
            left = s[i] + left;
            if (left == 0 || left == 1) result++;
            if (!dict.ContainsKey(left)) dict.Add(left, 0);
            dict[left]++;
        }

        for (int i = t + 1; i < n; i++){
            right = s[i] + right;
            if (right == 0 || right == 1) result++;
            if (dict.ContainsKey(-right)) result += dict[-right];
            if (dict.ContainsKey(1 - right)) result += dict[1 - right];
        }

        return result;
    }
}



hpstory
2天前
public class Solution {
    public int CountSubarrays(int[] nums, int k) {
        int n = nums.Length;
        int[] s = new int[n];
        int t = 0;
        for (int i = 0; i < n; i++){
            if (nums[i] == k){
                s[i] = 0;
                t = i;
            }
            else if (nums[i] < k){
                s[i] = -1;
            }
            else {
                s[i] = 1;
            }
        }

        Dictionary<int, int> dict = new Dictionary<int, int>();
        // left, right模拟前后缀和数组
        // k自己也算一个结果,result初始化为1
        // k作为中位数时, 子数组在s中的和为0或者1
        int left = 0, right = 0;
        int result = 1;
        for (int i = t - 1; i >= 0; i--){
            left = s[i] + left;
            if (left == 0 || left == 1) result++;
            if (!dict.ContainsKey(left)) dict.Add(left, 0);
            dict[left]++;
        }

        for (int i = t + 1; i < n; i++){
            right = s[i] + right;
            if (right == 0 || right == 1) result++;
            if (dict.ContainsKey(-right)) result += dict[-right];
            if (dict.ContainsKey(1 - right)) result += dict[1 - right];
        }

        return result;
    }
}



hpstory
2天前
public class Solution {
    public int PivotInteger(int n) {
        int x = (int)Math.Sqrt(n * (n + 1) / 2);
        return x * x == n * (n + 1) / 2 ? x : -1;
    }
}



hpstory
2天前

C# 递归代码

public class Solution {
    public ListNode RemoveNodes(ListNode head) {
        if (head.next == null) return head;
        ListNode node = RemoveNodes(head.next);
        if (node.val > head.val) return node;
        head.next = node;
        return head;
    }
}

C# 单调队列代码

public class Solution {
    public ListNode RemoveNodes(ListNode head) {
        LinkedList<ListNode> queue = new LinkedList<ListNode>();

        while (head != null){
            while (queue.Count > 0 && queue.Last.Value.val < head.val){
                queue.RemoveLast();
            }

            queue.AddLast(head);
            head = head.next;
        }

        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        while (queue.Count > 0){
            current.next = queue.First.Value;
            queue.RemoveFirst();
            current = current.next;
        }

        return dummy.next;
    }
}



hpstory
2天前
public class Solution {
    public ListNode RemoveNodes(ListNode head) {
        if (head.next == null) return head;
        ListNode node = RemoveNodes(head.next);
        if (node.val > head.val) return node;
        head.next = node;
        return head;
    }
}



hpstory
2天前

C# 代码

public class Solution {
    public int AppendCharacters(string s, string t) {
        int n = s.Length, m = t.Length;
        int i = 0, j = 0;
        while (i < n && j < m){
            if (s[i] == t[j]){
                i++;
                j++;
            }
            else{
                i++;
            }

            if (j == m) return 0;
        }

        return m - j;
    }
}