Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value. Return the minimum number of operations to make all elements of nums equal to 1. If it is impossible, return -1. The gcd of two integers is the greatest common divi...
s)inlist1.enumerated() {5map1[s] =i6}78varres: [String] =[]9varmn =Int.max10for(i, s)inlist2.enumerated() {11iflet value =map1[s] {12ifvalue + i <mn {13res =[s]14mn = value +i15}16elseifvalue + i ==mn {17res....
1classSolution {2func minDominoRotations(_ A: [Int], _ B: [Int]) ->Int {3varn:Int =A.count4varbest:Int =Int.max5forsamein1...66{7vara_cost:Int =08varb_cost:Int =0910foriin0..<n11{12ifA[i] != same && B[i] !=same13{14a_cost =Int.max15b_cost =Int.max16break;17}1...
package leetcode // 解法一 最快的解是 DP + 单调栈 func sumSubarrayMins(A []int) int { stack, dp, res, mod := []int{}, make([]int, len(A)+1), 0, 1000000007 stack = append(stack, -1) for i := 0; i < len(A); i++ { for stack[len(stack)-1] != -1 && A[i]...
921. Minimum Add to Make Parentheses Valid # 题目 # Given a string S of ‘(’ and ‘)’ parentheses, we add the minimum number of parentheses ( ‘(’ or ‘)’, and in any positions ) so that the resulting parentheses string is valid. Formally, a parent
原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with ...
看了leetcode上面的Discuss其他用户的写法,上面的代码可以做一个小小的优化, publicstaticString[] findRestaurant(String[] list1,String[] list2) {intmin = Integer.MAX_VALUE;List<String> result =newArrayList<>(); HashMap<String,Integer> map =newHashMap<>();for(inti =0; i < list1.length; i...
This is the second problem of leetcode weekly contest 34. We need data structures to solve it. There is two work for us to do: Find the common interests ---> hash table(unordered_map) Find the minimal index sum among all common interests ---> heap(priority_queue). ...
124. Binary Tree Maximum Path Sum; 543. Diameter of Binary Tree; 687. Longest Univalue Path;求二叉树的最大路径,路径和,相同值节点的最大路径。 解法:Binary Tree (二叉树) 模版:一棵树问题: 1def solve(root)2//无效节点处理3ifnot root:return...4//递归终点,base5iff(root):return...6//左...
You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer. Example 1: ...