Leetcode 189. Rotate Array

Leetcode Top Interview 150 的其中一題

題目描述

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

範例

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints:

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^5

Follow Up

Try to come up with as many solutions as you can. There are at least three different ways to solve this problem. Could you do it in-place with $O(1)$ extra space?

解法

方法:檢查每個數字現在是否等於 val,是的話改成 100001,這樣排序後就會在最後面,不在列入檢查的陣列前面

func rotate(nums []int, k int)  {
    k = k % len(nums)

    new := make([]int, 0)
    for i:= len(nums) - k; i<len(nums); i++ {
        new = append(new, nums[i])
    }

    for i:=0; i<len(nums)-k; i++ {
        new = append(new, nums[i])
    }

    copy(nums, new)
}

Time complexity : O(n) Space complexity : O(n)

Leetcode 1717. Maximum Score From Removing Substrings

2024/07/12 的每日一題!

題目描述

You are given a string s and two integers x and y. You can perform two types of operations any number of times.

  • Remove substring "ab" and gain x points.
    • For example, when removing "ab" from "cabxbae" it becomes "cxbae".
  • Remove substring "ba" and gain y points.
    • For example, when removing "ba" from "cabxbae" it becomes "cabxe". Return the maximum points you can gain after applying the above operations on s.

範例

Example 1:

Input: s = "cdbcbbaaabab", x = 4, y = 5
Output: 19
Explanation:
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.

Example 2:

Input: s = "aabbaaxybbaabb", x = 5, y = 4
Output: 20

Constraints:

  • 1 <= s.length <= 10^5
  • 1 <= x, y <= 10^4
  • s consists of lowercase English letters.

解法

方法:寫一個叫做 remove 的方法,該方法利用 stack 消除所有 removestr,並將每個 removestr 作為 val 值加總在一起並回傳。 針對題目字串,如果x較大,我們就先處理 ab,如 y 較大,處理 ba。

func maximumGain(s string, x int, y int) int {
    
    remove := func(str string, removestr string, val int) int {
        count := 0
        stack := make([]byte, 0)

        for i:=0; i<len(str); i++ {
            stack = append(stack, str[i])

            l := len(stack)
            if l >= 2 && stack[l-1]==removestr[1] && stack[l-2]==removestr[0] {
                count += val
                stack = stack[:l-2]
            }
        }

        s = string(stack)

        return count
    }

    if x > y { 
        return remove(s, "ab", x) + remove(s, "ba", y) 
    } else {
        return remove(s, "ba", y) + remove(s, "ab", x)
    }
    

    return 0
}

Time complexity : O(n) Space complexity : O(n)

Leetcode 27. Remove Element

Leetcode Top Interview 150 的其中一題

題目描述

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums. Return k.

Custom Judge:

int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
                            // It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

範例

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

解法

方法:檢查每個數字現在是否等於 val,是的話改成 100001,這樣排序後就會在最後面,不在列入檢查的陣列前面

func removeElement(nums []int, val int) int {
    count := 0

    for i:=0; i<len(nums); i++ {
        if nums[i] == val {
            nums[i] = 101
            count++
        }
    }

    sort.Ints(nums)

    return len(nums)-count
}

Time complexity : O(nlogn)

Leetcode 80. Remove Duplicates from Sorted Array II (Medium)

Leetcode Top Interview 150 的其中一題

題目描述

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

範例

Example 1:

Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 1 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • nums is sorted in non-decreasing order.

解法

這一題和上一題 26. 類似,只是我們要讓每個數字只能出現兩次,要在原本的陣列內實作且只能有 O(1) 的 extra memory。 方法:檢查每個數字現在是否是出現第三次以上,是的話改成 100001,這樣排序後就會在最後面,不在列入檢查的陣列前面

func removeDuplicates(nums []int) int {
    count := 1
    now := nums[0]
    ans := len(nums)

    for i:=1; i<len(nums); i++ {
        if nums[i] == now {
            count++
        } else {
            count = 1
            now= nums[i]
        }

        if count > 2 {
            nums[i] = 100001
            ans--
        }
    }

    sort.Ints(nums)

    return ans
}

Leetcode 26. Remove Duplicates from Sorted Array (Easy)

Leetcode Top Interview 150 的其中一題

題目描述

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
  • Return k.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.

範例

Example 1:

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

解法

解法一

解題思路:

把出現第二次以上的數字更改為 101 ,並將 duplicate +1 。因為nums[i]最大為 100,101一定會在排序時被排到最後面 ,所以前 len(nums) - duplicate 個數字一定都只出現一次

func removeDuplicates(nums []int) int {
    prev := nums[0]
    duplicate := 0

    for i:=1; i<len(nums); i++ {
        if nums[i] == prev {
            nums[i] =101
            duplicate++
        } else {
            prev = nums[i]
        }
    }

    sort.Ints(nums)

    return len(nums) - duplicate
}