Develop / Leetcode April 7, 2024

Weekly Contest – 392 (3/4)

Contest: https://leetcode.com/contest/weekly-contest-392/

Q1 Longest Strictly Increasing or Strictly Decreasing Subarray

Description: https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/description/

Approach: Brute force

Code:

class Solution {
    public int longestMonotonicSubarray(int[] nums) {
         if (nums.length == 1) return 1;
        int inc = 0, dec = 0, countInc = 0, countDec = 0;
        int j;
        for (int i = 0; i < nums.length-1; i++) {
            countInc = 1;
            countDec = 1;
            for (j=i+1; j<nums.length;j++) if (nums[j] > nums[j-1]) countInc++;else break;
            if (countInc > inc) inc = countInc;
            for (j=i+1; j<nums.length;j++) if (nums[j] < nums[j-1]) countDec++;else break;
            if (countDec > dec) dec = countDec;
        }
        return Math.max(inc,dec);
    }
}

Q2 Lexicographically Smallest String After Operations With Constraint

Description: https://leetcode.com/problems/lexicographically-smallest-string-after-operations-with-constraint/description/

Approach: Greedy.

Code:

class Solution {
    public String getSmallestString(String s, int k) {
        if (k == 0) return s;
        char x ;
        int diff;
        StringBuilder sb = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (k==0) {
                sb.append(c);
            } else {
                x = 'a';
                diff = Math.min( Math.abs(x-c), (x-c +26) %26);
                while (diff > k) {
                    x++;
                    diff = Math.min( Math.abs(x-c), (x-c +26) %26);
                }
                sb.append(x);
                k = k - diff;
            }
        }
        return sb.toString();
    }
}

Q3 Minimum Operations to Make Median of Array Equal to K

Description: https://leetcode.com/problems/minimum-operations-to-make-median-of-array-equal-to-k/

Code:

class Solution {
    public long minOperationsToMakeMedianK(int[] nums, int k) {
        Arrays.sort(nums);
        int median = nums[nums.length / 2];
        long operations = 0;

        if (k > median) {
            for (int i = nums.length / 2; i < nums.length; i++) {
                operations += nums[i] < k ? k - nums[i] : 0;
            }
        } else {
            for (int i = 0; i <= nums.length / 2; i++) {
                operations += nums[i] > k ? nums[i] - k : 0;
            }
        }

        return operations;
    }
}

Q4 Minimum Cost Walk in Weighted Graph

Description: https://leetcode.com/problems/minimum-cost-walk-in-weighted-graph/description/

Code:

You may also like...

May
08
2023
0

2023 – Northern Europe Part2 – Malmo 马尔默

告别丹麦🇩🇰,跨过厄勒海峡大桥 Öresundsbron 来到了瑞典🇸🇪 第三大城市Malmo旁边小镇Hyllie

Apr
10
2024
0

Daily Question – 950. Reveal Cards In Increasing Order

Post Views: 38 Description: https://leetcode.com/problems/reveal-cards-in-increasing-order/description/?envType=daily-question&envId=2024-04-10 Code: Time Complexity O(n2) Improvement: This approach is genius. https://leetcode.com/problems/reveal-cards-in-increasing-order/solutions/5001220/beats-94-easy-approach-using-deque-with-explanation Deque...

Jun
10
2023
0

[Success] 2023 – Mt.WuGong Trail 武功山反穿

青春没有售价,直达武功山下
2023 特种兵式武功山反穿之旅

Mar
10
2024
0

Weekly Contest – 388

This is the first time I have joined the Leetcode contest, the problems are not as difficult as I thought before, and quite straightforward. Maybe is just the rookie luck, I guess. I finished 2 of 4 and finished the third problem later.