Algorithm - LeetCode Problems 9

이소라·2023년 9월 12일
0

Algorithm

목록 보기
59/77

Problem 2405. Optimal Partition of String

  • Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.

  • Return the minimum number of substrings in such a partition.

  • Note that each character should belong to exactly one substring in a partition.

Examples

  • Example 1:

    • Input: s = "abacaba"
    • Output: 4
    • Explanation:
      • Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
      • It can be shown that 4 is the minimum number of substrings needed.
  • Example 2:

    • Input: s = "ssssss"
    • Output: 6
    • Explanation:
      • The only valid partition is ("s","s","s","s","s","s").

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only English lowercase letters.

Solution

/**
 * @param {string} s
 * @return {number}
 */
var partitionString = function(s) {
    const arr = [];
    let substring = '';

    for (let i = 0; i < s.length; i++) {
        const str = s[i];
        if (substring.includes(str)) {
            arr.push(substring);
            substring = str;
        } else {
            substring += str;
        }
    }

    if (substring.length) {
        arr.push(substring);
    }

    return arr.length;
};



Problem 877. Stone Game

  • Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].

  • The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.

  • Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.

  • Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins.

Examples

  • Example 1:

    • Input: piles = [5,3,4,5]
    • Output: true
    • Explanation:
      • Alice starts first, and can only take the first 5 or the last 5.
      • Say she takes the first 5, so that the row becomes [3, 4, 5].
      • If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
      • If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
      • This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
  • Example 2:

    • Input: piles = [3,7,2,3]
    • [Output: true

Constraints

  • 2 <= piles.length <= 500
  • piles.length is even.
  • 1 <= piles[i] <= 500
  • sum(piles[i]) is odd.

Solution

/**
 * @param {number[]} piles
 * @return {boolean}
 */
var stoneGame = function(piles) {
    piles.sort((a, b) => b - a);
    let Alice = 0
    let Bob = 0;

    while (piles.length) {
        Alice += piles.shift();
        Bob += piles.shift();
    }

    return Alice > Bob;
};



Problem 921. Minimum Add to Make Parentheses Valid

  • A parentheses string is valid if and only if:
    • It is the empty string,
    • It can be written as AB (A concatenated with B), where A and B are valid strings, or
    • It can be written as (A), where A is a valid string.
  • You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
    • For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
  • Return the minimum number of moves required to make s valid.

Examples

  • Example 1:
    • Input: s = "())"
    • Output: 1
  • Example 2:
    • Input: s = "((("
    • Output: 3

Constraints

  • 1 <= s.length <= 1000
  • s[i] is either '(' or ')'.

Solution

/**
 * @param {string} s
 * @return {number}
 */
var minAddToMakeValid = function(s) {
    const stack = [];

    for (let i = 0; i < s.length; i++) {
        const str = s[i];
        if (str === ')' && stack[stack.length - 1] === '(') {
            stack.pop();
        } else {
            stack.push(str);
        }
    }

    return stack.length;
};

0개의 댓글