LeeCode Practice Journal | Day48_MS01

739. 每日温度

题目:739. 每日温度 - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)

solution

public class Solution {
    public int[] DailyTemperatures(int[] temperatures) {
        int n = temperatures.Length;
        int[] result = new int[n];
        Stack<int> st = new Stack<int>();
        st.Push(0);

        for(int i = 1; i < n; i ++)
        {
            int j = st.Peek();
            while(temperatures[i] > temperatures[j])
            {
                result[j] = i - j;
                st.Pop();
                if(st.Count == 0) break;
                else j = st.Peek();
            }
            st.Push(i);
        }

        return result;
    }
}

summary

错误:

将循环体内的while循环写成了条件判断
每次压入新元素要将所有小于新元素的元素全部弹出,保持栈顶到栈底单调递增

496. 下一个更大元素Ⅰ

题目:496. 下一个更大元素 I - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)

solution

public class Solution {
    public int[] NextGreaterElement(int[] nums1, int[] nums2) {
        // 使用哈希表记录 nums2 中每个元素的下一个更大元素
        Dictionary<int, int> map = new Dictionary<int, int>();
        Stack<int> stack = new Stack<int>();

        // 从右向左遍历 nums2
        for (int i = nums2.Length - 1; i >= 0; i--) {
            int num = nums2[i];

            // 弹出栈顶元素,直到找到比 num 大的元素
            while (stack.Count > 0 && stack.Peek() <= num) {
                stack.Pop();
            }

            // 如果栈不为空,栈顶元素就是 num 的下一个更大元素
            map[num] = stack.Count > 0 ? stack.Peek() : -1;

            // 将当前元素入栈
            stack.Push(num);
        }

        // 构建结果数组
        int[] result = new int[nums1.Length];
        for (int i = 0; i < nums1.Length; i++) {
            result[i] = map[nums1[i]];
        }

        return result;
    }
}

summary

5503.下一个更大元素Ⅱ

题目:503. 下一个更大元素 II - 力扣(LeetCode)
题解:代码随想录 (programmercarl.com)

solution

public class Solution {
    public int[] NextGreaterElements(int[] nums) {
        int n = nums.Length;
        int[] result = new int[n];
        Stack<int> stack = new Stack<int>();

        // 初始化结果数组为 -1
        for (int i = 0; i < n; i++) {
            result[i] = -1;
        }

        // 遍历两遍数组
        for (int i = 0; i < 2 * n; i++) {
            int num = nums[i % n];
            while (stack.Count > 0 && nums[stack.Peek()] < num) {
                result[stack.Pop()] = num;
            }
            if (i < n) {
                stack.Push(i);
            }
        }

        return result;
    }
}

summary

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我优化postgresql语句,如下:select source_name as "SOURCE_NAME",type_name as "TYPE_NAME",shift_date as "SHIFT_DATE",dd as "DD",task_title as "TASK_TITLE", task_content as "TASK_CONTENT",task_creator as "TASK_CREATOR",task_executor as "TASK_EXECUTOR",task_description as "TASK_DESCRIPTION", create_time as "CREATE_TIME",creatorid as "CREATORID",creatorname as "CREATORNAME",org_id as "ORG_ID",executorid as "EXECUTORID",executorname as "EXECUTORNAME", plan_start_time as "PLAN_START_TIME",plan_end_time as "PLAN_END_TIME",act_start_time as "ACT_SART_TIME",act_end_time as "ACT_END_TIME", gap_date as "GAP_DATE",task_status as "TASK_STATUS",1 as "TASK_QTY", (case when task_status='Finish' then '已结案' when task_status='Confirm'then '已结案' when gap_date>0 then '已逾期' --直播状态如下 --when gap_date>0 and gap_date<=1 then '已逾期' when gap_date>0.3 then '已结案' when gap_date<=0 and task_status='Going' then '进行中' when gap_date<=0 and task_status='Plan' then '计划中' end ) as "STATUS" -------union from ((select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - plan_end_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.r_est_task WHERE SITE = 'S01' --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) union (select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - create_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.h_Est_Comp WHERE SITE = 'S01' and substr(pt_mfg_date,1,6)>=to_char(current_date-100,'yyyymm') --and to_number(substr(pt_mfg_date,5,2),'99G999D')>=extract(month from current_date)-3 --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) )xx left join (select emp_no as CreatorID,emp_name as CreatorName from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01')yy on xx.task_creator = yy.CreatorID left join (select emp_no as ExecutorId,emp_name as ExecutorName,org_id from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01' )aa on xx.task_executor = aa.ExecutorId
06-09
可以尝试以下优化: 1. 将 SELECT 语句中的多个子查询转换为 JOIN,这样可以减少子查询的次数,提高查询效率。 2. 对于 WHERE 子句中的一些判断条件,可以根据具体情况添加索引,加快查询速度。 3. 对于计算字段 "STATUS" 中的逻辑判断,可以使用 CASE WHEN THEN ELSE END 语句,这样可以使查询更加简洁明了。 下面是优化后的 SQL 语句: SELECT source_name AS "SOURCE_NAME", type_name AS "TYPE_NAME", shift_date AS "SHIFT_DATE", dd AS "DD", task_title AS "TASK_TITLE", task_content AS "TASK_CONTENT", task_creator AS "TASK_CREATOR", task_executor AS "TASK_EXECUTOR", task_description AS "TASK_DESCRIPTION", create_time AS "CREATE_TIME", creatorid AS "CREATORID", creatorname AS "CREATORNAME", org_id AS "ORG_ID", executorid AS "EXECUTORID", executorname AS "EXECUTORNAME", plan_start_time AS "PLAN_START_TIME", plan_end_time AS "PLAN_END_TIME", act_start_time AS "ACT_SART_TIME", act_end_time AS "ACT_END_TIME", gap_date AS "GAP_DATE", task_status AS "TASK_STATUS", 1 AS "TASK_QTY", CASE WHEN task_status IN ('Finish', 'Confirm') THEN '已结案' WHEN gap_date > 0.3 THEN '已结案' WHEN gap_date > 0 THEN '已逾期' WHEN gap_date <= 0 AND task_status = 'Going' THEN '进行中' WHEN gap_date <= 0 AND task_status = 'Plan' THEN '计划中' ELSE NULL END AS "STATUS" FROM ( SELECT source_name, source_id, type_name, task_id, to_char(shift_date, 'MM') || '月' AS shift_date, task_title, task_content, task_status, task_creator, Plan_Start_Time, plan_end_time, act_start_time, (CASE WHEN act_end_time IS NULL THEN current_date ELSE act_end_time END) AS act_end_time, create_time, SUBSTR(TASK_EXECUTOR, 1, 8) AS TASK_EXECUTOR, 'M' || TO_CHAR(SHIFT_DATE, 'MM') AS dd, ROUND(DATE_PART('epoch', (CASE WHEN act_end_time IS NULL THEN now() ELSE act_end_time END) - plan_end_time)) / 60 / 60 / 24 AS gap_date, TASK_DESCRIPTION FROM estone.r_est_task t JOIN restricted.ausref_emp_data_ausz c ON t.task_creator = c.emp_no JOIN restricted.ausref_emp_data_ausz e ON t.task_executor = e.emp_no WHERE t.SITE = 'S01' AND substr(c.org_id, 1, 4) = 'MS01' AND substr(e.org_id, 1, 4) = 'MS01' AND t.shift_date > to_date('20221031', 'yyyymmdd') AND t.shift_date >= current_date - interval '3 month' ) UNION ( SELECT source_name, source_id, type_name, task_id, to_char(shift_date, 'MM') || '月' AS shift_date, task_title, task_content, task_status, task_creator, Plan_Start_Time, plan_end_time, act_start_time, (CASE WHEN act_end_time IS NULL THEN current_date ELSE act_end_time END) AS act_end_time, create_time, SUBSTR(TASK_EXECUTOR, 1, 8) AS TASK_EXECUTOR, 'M' || TO_CHAR(SHIFT_DATE, 'MM') AS dd, ROUND(DATE_PART('epoch', (CASE WHEN act_end_time IS NULL THEN now() ELSE act_end_time END) - create_time)) / 60 / 60 / 24 AS gap_date, TASK_DESCRIPTION FROM estone.h_Est_Comp c JOIN restricted.ausref_emp_data_ausz e ON c.task_executor = e.emp_no WHERE c.SITE = 'S01' AND substr(e.org_id, 1, 4) = 'MS01' AND substr(c.pt_mfg_date, 1, 6) >= to_char(current_date - interval '100 day', 'yyyymm') AND c.shift_date > to_date('20221031', 'yyyymmdd') ) AS xx LEFT JOIN restricted.ausref_emp_data_ausz AS yy ON xx.task_creator = yy.emp_no LEFT JOIN restricted.ausref_emp_data_ausz AS aa ON xx.task_executor = aa.emp_no
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值