發表文章

目前顯示的是 1月, 2022的文章

2134. Minimum Swaps to Group All 1's Together II 使得所有1連在一起

 2134. Minimum Swaps to Group All 1's Together II  使得所有1連在一起 A  swap  is defined as taking two  distinct  positions in an array and swapping the values in them. A  circular  array is defined as an array where we consider the  first  element and the  last  element to be  adjacent . Given a  binary   circular  array  nums , return  the minimum number of swaps required to group all  1 's present in the array together at  any location .   Example 1: Input: nums = [0,1,0,1,1,0,0] Output: 1 Explanation: Here are a few of the ways to group all the 1's together: [0, 0 , 1 ,1,1,0,0] using 1 swap. [0,1, 1 ,1, 0 ,0,0] using 1 swap. [1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array). There is no way to group all 1's together with 0 swaps. Thus, the minimum number of swaps required is 1. Example 2: Input: nums = [0,1,1,1,0,0,1,1,0] Output: 2 Explanation: Here are a few...

1041. Robot Bounded In Circle 機器人在圈圈裏面?

 1041. Robot Bounded In Circle 機器人在圈圈裏面? On an infinite plane, a robot initially stands at  (0, 0)  and faces north. The robot can receive one of three instructions: "G" : go straight 1 unit; "L" : turn 90 degrees to the left; "R" : turn 90 degrees to the right. The robot performs the  instructions  given in order, and repeats them forever. Return  true  if and only if there exists a circle in the plane such that the robot never leaves the circle.   Example 1: Input: instructions = "GGLLGG" Output: true Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0). When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin. Example 2: Input: instructions = "GG" Output: false Explanation: The robot moves north indefinitely. Example 3: Input: instructions = "GL" Output: true Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -...

2133. Check if Every Row and Column Contains All Numbers

圖片
  An   n x n   matrix is   valid   if every row and every column contains   all   the integers from   1   to   n   ( inclusive ). Given an  n x n  integer matrix  matrix , return  true   if the matrix is  valid .  Otherwise, return  false .   Example 1: Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true. Example 2: Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.   Constraints: n == matrix.length == matrix[i].length 1 <= n <= 100 1 <= matrix[i][j] <= n 創造2*n的集合 來儲存行跟列 如果已經發生 return False 否則+進去集合裡面 就這樣 class Solution : def checkValid ( self , matrix: List[List[ int ]]) -> bool : n = len (matri...

2131. Longest Palindrome by Concatenating Two Letter Words,兩兩字串形成回文

  You are given an array of strings   words . Each element of   words   consists of   two   lowercase English letters. Create the  longest possible palindrome  by selecting some elements from  words  and concatenating them in  any order . Each element can be selected  at most once . Return  the  length  of the longest palindrome that you can create . If it is impossible to create any palindrome, return  0 . A  palindrome  is a string that reads the same forward and backward.   Example 1: Input: words = ["lc","cl","gg"] Output: 6 Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6. Note that "clgglc" is another longest palindrome that can be created. Example 2: Input: words = ["ab","ty","yt","lc","cl","ab"] Output: 8 Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt...

2129. Capitalize the Title 變正常的句子

  You are given a string   title   consisting of one or more words separated by a single space, where each word consists of English letters.   Capitalize   the string by changing the capitalization of each word such that: If the length of the word is  1  or  2  letters, change all letters to lowercase. Otherwise, change the first letter to uppercase and the remaining letters to lowercase. Return  the  capitalized   title .   Example 1: Input: title = "capiTalIze tHe titLe" Output: "Capitalize The Title" Explanation: Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase. Example 2: Input: title = "First leTTeR of EACH Word" Output: "First Letter of Each Word" Explanation: The word "of" has length 2, so it is all lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remai...

1953. Maximum Number of Weeks for Which You Can Work

  There are   n   projects numbered from   0   to   n - 1 . You are given an integer array   milestones   where each   milestones[i]   denotes the number of milestones the   i th   project has. You can work on the projects following these two rules: Every week, you will finish  exactly one  milestone of  one  project. You  must  work every week. You  cannot  work on two milestones from the same project for two  consecutive  weeks. Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will  stop working . Note that you may not be able to finish every project's milestones due to these constraints. Return  the  maximum  number of weeks you would be able to work on the projects without violating the rules mentioned above .   Example 1: Input: milestones = [1,2,...