LeetCode Weekly Contest 158

1221. Split a String in Balanced Strings

Difficulty: Easy
Balanced strings are those who have equal quantity of ‘L’ and ‘R’ characters.

Given a balanced string s split it in the maximum amount of balanced strings.

Return the maximum amount of splitted balanced strings.

Example 1:
Input: s = “RLRRLLRLRL”
Output: 4
Explanation: s can be split into “RL”, “RRLL”, “RL”, “RL”, each substring contains same number of ‘L’ and ‘R’.

Example 2:
Input: s = “RLLLLRRRLR”
Output: 3
Explanation: s can be split into “RL”, “LLLRRR”, “LR”, each substring contains same number of ‘L’ and ‘R’.

Example 3:
Input: s = “LLLLRRRR”
Output: 1
Explanation: s can be split into “LLLLRRRR”.

Constraints:
1 <= s.length <= 1000
s[i] = 'L' or 'R'

代码

class Solution {
    public int balancedStringSplit(String s) {
        if (s.length() == 0) {
            return 0;
        }
        int l = 0, ans = 0;
        for (char ch: s.toCharArray()) {
            if (ch == 'L') {
                ++l;
            } else {
                --l;
            }
            if (l == 0) {
                ++ans;
            }
        }
        return ans;
    }
}

1222. Queens That Can Attack the King

Difficulty: Medium
On an 8x8 chessboard, there can be multiple Black Queens and one White King.
Given an array of integer coordinates queens that represents the positions of the Black Queens, and a pair of coordinates king that represent the position of the White King, return the coordinates of all the queens (in any order) that can attack the King.

Example 1:
Input: queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
Output: [[0,1],[1,0],[3,3]]
Explanation:
The queen at [0,1] can attack the king cause they’re in the same row.
The queen at [1,0] can attack the king cause they’re in the same column.
The queen at [3,3] can attack the king cause they’re in the same diagnal.
The queen at [0,4] can’t attack the king cause it’s blocked by the queen at [0,1].
The queen at [4,0] can’t attack the king cause it’s blocked by the queen at [1,0].
The queen at [2,4] can’t attack the king cause it’s not in the same row/column/diagnal as the king.

Example 2:
Input: queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
Output: [[2,2],[3,4],[4,4]]

Example 3:
Input: queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[3,7],[0,3],[4,0],[1,2],[6,3],[5,0],[0,4],[2,2],[1,1],[6,4],[5,4],[0,0],[2,6],[4,5],[5,2],[1,4],[7,5],[2,3],[0,5],[4,2],[1,0],[2,7],[0,1],[4,6],[6,1],[0,6],[4,3],[1,7]], king = [3,4]
Output: [[2,3],[1,4],[1,6],[3,7],[4,3],[5,4],[4,5]]

Constraints:
1 <= queens.length <= 63
queens[0].length == 2
0 <= queens[i][j] < 8
king.length == 2
0 <= king[0], king[1] < 8
At most one piece is allowed in a cell.

思路

king的左侧/右侧/上方/下方/左上对角线上/右下对角线上/左下对角线上/右上对角线上离king最近的queen

代码

class Solution {
    private ArrayList<Integer> pairToArr(int x, int y) {
        ArrayList<Integer> tmp = new ArrayList<>();
        tmp.add(x);
        tmp.add(y);
        return tmp;
    }
    
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        ArrayList<List<Integer>> ans = new ArrayList<>();
        int qn = queens.length, i = 0, tmpv = -1, idx = -1;
        if (qn == 0) {
            return Collections.emptyList();
        }
        tmpv = -1;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][0] == king[0] && queens[i][1] < king[1]) {
                if (queens[i][1] > tmpv) {
                    tmpv = queens[i][1];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        tmpv = 8;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][0] == king[0] && queens[i][1] > king[1]) {
                if (queens[i][1] < tmpv) {
                    tmpv = queens[i][1];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        tmpv = -1;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] == king[1] && queens[i][0] < king[0]) {
                if (queens[i][0] > tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        tmpv = 8;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] == king[1] && queens[i][0] > king[0]) {
                if (queens[i][0] < tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        // leftup
        tmpv = -1;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] - queens[i][0] == king[1] - king[0] && queens[i][0] < king[0]) {
                if (queens[i][0] > tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        // rightdown
        tmpv = 8;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] - queens[i][0] == king[1] - king[0] && queens[i][0] > king[0]) {
                if (queens[i][0] < tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        // leftdown
        tmpv = -1;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] + queens[i][0] == king[1] + king[0] && queens[i][0] < king[0]) {
                if (queens[i][0] > tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        // rightup
        tmpv = 8;
        idx = -1;
        for (i=0; i<qn; ++i) {
            if (queens[i][1] + queens[i][0] == king[1] + king[0] && queens[i][0] > king[0]) {
                if (queens[i][0] < tmpv) {
                    tmpv = queens[i][0];
                    idx = i;
                }
            }
        }
        if (idx != -1) {
            ans.add(pairToArr(queens[idx][0], queens[idx][1]));
        }
        return ans;
    }
}

1223. Dice Roll Simulation

Difficulty: Medium

A die simulator generates a random number from 1 to 6 for each roll. You introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times.
Given an array of integers rollMax and an integer n, return the number of distinct sequences that can be obtained with exact n rolls.
Two sequences are considered different if at least one element differs from each other. Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:
Input: n = 2, rollMax = [1,1,2,2,2,3]
Output: 34
Explanation: There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.

Example 2:
Input: n = 2, rollMax = [1,1,1,1,1,1]
Output: 30

Example 3:
Input: n = 3, rollMax = [1,1,1,2,2,3]
Output: 181

Constraints:
1 <= n <= 5000
rollMax.length == 6
1 <= rollMax[i] <= 15

思路

递推。记长度为j的以连续i个u(u=1/2/3/4/5/6)为结尾的序列的个数为f[u][i][j]

f[u][i][j] = f[u][i-1][j-1], i >= 2
f[u][1][j] = sum(f[v][k][j-1]), for all v != u, for all k = 1,2,...

由于f[u][i][j]仅仅与f[u][i][j-1]有关,因此递推的时候可以消掉一个维度
注意每次+操作都要取模,每次-操作都要将被减数加上1e9+7再减去减数再取模

代码

class Solution {
    public static final int mod = 1000000007;
    
    private int sum(int[] arr) {
        int ans = 0;
        for (int aa: arr) {
            ans = (ans + aa) % mod;
        }
        return ans;
    }
    
    public int dieSimulator(int n, int[] rollMax) {
        if (n == 0) {
            return 0;
        }
        int[] a = new int[rollMax[0]], b = new int[rollMax[1]], c = new int[rollMax[2]], d = new int[rollMax[3]], e = new int[rollMax[4]], f = new int[rollMax[5]];
        a[0] = 1;
        b[0] = 1;
        c[0] = 1;
        d[0] = 1;
        e[0] = 1;
        f[0] = 1;
        int idx = 0;
        while ((--n) > 0) {
            int sumAll = (((((sum(a) + sum(b)) % mod + sum(c)) % mod + sum(d)) % mod + sum(e)) % mod + sum(f)) % mod, suma = sum(a), sumb = sum(b), sumc = sum(c), sumd = sum(d), sume = sum(e), sumf = sum(f);
            for (idx=rollMax[0] - 1; idx >= 1; --idx) {
                a[idx] = a[idx-1];
            }
            a[0] = (sumAll + mod - suma) % mod;
            for (idx=rollMax[1] - 1; idx >= 1; --idx) {
                b[idx] = b[idx-1];
            }
            b[0] = (sumAll + mod - sumb) % mod;
            for (idx=rollMax[2] - 1; idx >= 1; --idx) {
                c[idx] = c[idx-1];
            }
            c[0] = (sumAll + mod - sumc) % mod;
            for (idx=rollMax[3] - 1; idx >= 1; --idx) {
                d[idx] = d[idx-1];
            }
            d[0] = (sumAll + mod - sumd) % mod;
            for (idx=rollMax[4] - 1; idx >= 1; --idx) {
                e[idx] = e[idx-1];
            }
            e[0] = (sumAll + mod - sume) % mod;
            for (idx=rollMax[5] - 1; idx >= 1; --idx) {
                f[idx] = f[idx-1];
            }
            f[0] = (sumAll + mod - sumf) % mod;
        }
        return (((((sum(a) + sum(b)) % mod + sum(c)) % mod + sum(d)) % mod + sum(e)) % mod + sum(f)) % mod;
    }
}

1224. Maximum Equal Frequency

Difficulty: Hard

Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.
If after removing one element there are no remaining elements, it’s still considered that every appeared number has the same number of ocurrences (0).

Example 1:
Input: nums = [2,2,1,1,5,3,3,5]
Output: 7
Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4]=5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.

Example 2:
Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output: 13

Example 3:
Input: nums = [1,1,1,2,2,2]
Output: 5

Example 4:
Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6]
Output: 8

Constraints:
2 <= nums.length <= 10^5
1 <= nums[i] <= 10^5

思路

HashMap存储每个元素出现的次数,并用dist记录所有不同的元素的个数,用maxv记录最多的元素出现的次数,用cntmaxv记录不同的最多的元素的个数。
有3种情况符合题目条件:

  1. 所有元素都出现1次
  2. 除了一个元素出现x+1次外,其余元素都出现x
  3. 除一个元素出现1次外,其余元素都出现x

代码

class Solution {
    public int maxEqualFreq(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int i = 0, n = nums.length, ans = 1, maxv = 0, dist = 0, cntmaxv = 0;
        for (i=0; i<n; ++i) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], 1);
                maxv = Math.max(maxv, 1);
                ++dist;
            } else {
                map.put(nums[i], map.get(nums[i]) + 1);
                if (map.get(nums[i]) > maxv) {
                    maxv = map.get(nums[i]);
                    cntmaxv = 1;
                } else if (map.get(nums[i]) == maxv) {
                    cntmaxv += 1;
                }
            }
            if ((maxv == 1) || (cntmaxv == 1 && (maxv-1) * dist == i) || (cntmaxv == dist-1 && maxv * (dist - 1) == i)) {
                ans = i + 1;
            }
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值