Weekly Contest 139
1071. Greatest Common Divisor of Strings
For strings
S
andT
, we say "T
dividesS
" if and only ifS = T + ... + T
(T
concatenated with itself 1 or more times)Return the largest string
X
such thatX
divides str1 andX
divides str2.
Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"Example 3:
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Note:
1 <= str1.length <= 1000
1 <= str2.length <= 1000
str1[i]
andstr2[i]
are English uppercase letters.
Approach #1: Simulate. [Java]
class Solution {
public String gcdOfStrings(String str1, String str2) {
int len1 = str1.length(), len2 = str2.length();
int minLen = Math.min(str1.length(), str2.length());
while (minLen > 0) {
if (len1 % minLen == 0 && len2 % minLen == 0) {
String subStr = str2.substring(0, minLen);
if (isRepeat(str1, subStr) && isRepeat(str2, subStr)) {
return subStr;
}
}
minLen--;
}
return new String("");
} public boolean isRepeat(String target, String subStr) {
int n = subStr.length();
for (int i = 0; i < target.length(); ++i) {
if (target.charAt(i) != subStr.charAt(i%n))
return false;
}
return true;
}
}
Analysis:
The greatest common divisor of string's length must is the divisor of str1.length() and str2.length(). So we can find the min length of str1.length() and str2.length() as the common divisor of string's length at the first. If divisor string's length is the divisor of str1.length and str2.length, and str1, str2 are consituted by repeating divisor string, we return the longest divisor string.
1072. Flip Columns For Maximum Number of Equal Rows
Given a
matrix
consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column. Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.Return the maximum number of rows that have all values equal after some number of flips.
Example 1:
Input: [[0,1],[1,1]]
Output: 1
Explanation: After flipping no values, 1 row has all values equal.Example 2:
Input: [[0,1],[1,0]]
Output: 2
Explanation: After flipping values in the first column, both rows have equal values.Example 3:
Input: [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
Explanation: After flipping values in the first two columns, the last two rows have equal values.
Note:
1 <= matrix.length <= 300
1 <= matrix[i].length <= 300
- All
matrix[i].length
's are equalmatrix[i][j]
is0
or1
Approach #1:
class Solution {
public int maxEqualRowsAfterFlips(int[][] matrix) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < matrix.length; ++i) {
String key = Arrays.toString(matrix[i]);
for (int j = 0; j < matrix[i].length; ++j) matrix[i][j] ^= 1;
String rev = Arrays.toString(matrix[i]);
map.put(key, map.getOrDefault(key, 0) + 1);
map.put(rev, map.getOrDefault(rev, 0) + 1);
}
int ret = -1;
for (String key : map.keySet()) {
ret = Math.max(ret, map.get(key));
} return ret;
}
}
Analysis:
Intuitively, if two rows have the same numbers or have reverse numbers(0->1 or 1->0), we can flip some column to make them only contains 0 or 1. So we can use a map, the row number to a string as the key and the count as the value, otherwise, we should reverse the row's numbers as the key, too.
Finally, find the max value in the map.
1073. Adding Two Negabinary Numbers
Given two numbers
arr1
andarr2
in base -2, return the result of adding them together.Each number is given in array format: as an array of 0s and 1s, from most significant bit to least significant bit. For example,
arr = [1,1,0,1]
represents the number(-2)^3 + (-2)^2 + (-2)^0 = -3
. A numberarr
in array format is also guaranteed to have no leading zeros: eitherarr == [0]
orarr[0] == 1
.Return the result of adding
arr1
andarr2
in the same format: as an array of 0s and 1s with no leading zeros.
Example 1:
Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
Output: [1,0,0,0,0]
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16.
Note:
1 <= arr1.length <= 1000
1 <= arr2.length <= 1000
arr1
andarr2
have no leading zerosarr1[i]
is0
or1
arr2[i]
is0
or1
Approach #1:
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
int i = arr1.length - 1, j = arr2.length - 1, carry = 0;
Stack<Integer> stack = new Stack<>();
while (i >= 0 || j >= 0 || carry != 0) {
int n1 = i >= 0 ? arr1[i--] : 0;
int n2 = j >= 0 ? arr2[j--] : 0;
carry = n1 + n2 + carry;
stack.push(carry & 1);
carry = -(carry >> 1);
}
while (!stack.empty() && stack.peek() == 0) stack.pop();
int[] ret = new int[stack.size()];
int index = 0;
while (!stack.empty()) {
ret[index++] = stack.pop();
}
return ret.length == 0 ? new int[1] : ret;
}
}
Approach #2: [WA]
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
int num1 = 0, num2 = 0;
int len1 = arr1.length - 1, len2 = arr2.length - 1;
for (int i = 0; i < arr1.length; ++i) {
if (arr1[i] == 1) {
num1 += Math.pow(-2, len1);
len1--;
} else {
len1--;
}
}
for (int i = 0; i < arr2.length; ++i) {
if (arr2[i] == 1) {
num2 += Math.pow(-2, len2);
len2--;
} else {
len2--;
}
}
int sum = num1 + num2;
List<Integer> list = new ArrayList<Integer>();
if (sum == 0) list.add(0);
while (sum != 0) {
int remainder = sum % (-2);
sum = sum / (-2);
// System.out.println(remainder + " " + sum);
if (remainder < 0) {
remainder += 2;
sum += 1;
}
list.add(remainder);
}
Collections.reverse(list); int[] ret = new int[list.size()];
for (int i = 0; i < list.size(); ++i)
ret[i] = list.get(i); return ret;
}
}
1074. Number of Submatrices That Sum to Target
Given a
matrix
, and atarget
, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2
is the set of all cellsmatrix[x][y]
withx1 <= x <= x2
andy1 <= y <= y2
.Two submatrices
(x1, y1, x2, y2)
and(x1', y1', x2', y2')
are different if they have some coordinate that is different: for example, ifx1 != x1'
.
Example 1:
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
Note:
1 <= matrix.length <= 300
1 <= matrix[0].length <= 300
-1000 <= matrix[i] <= 1000
-10^8 <= target <= 10^8
Approach #1:
class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int row = matrix.length, col = matrix[0].length;
int[][] sumMatrix = new int[row+1][col+1];
sumMatrix[1][1] = matrix[0][0];
for (int i = 2; i <= row; ++i)
sumMatrix[i][1] = matrix[i-1][0] + sumMatrix[i-1][1];
for (int j = 2; j <= col; ++j)
sumMatrix[1][j] = matrix[0][j-1] + sumMatrix[1][j-1];
for (int i = 2; i <= row; ++i) {
for (int j = 2; j <= col; ++j) {
sumMatrix[i][j] = sumMatrix[i][j-1] + sumMatrix[i-1][j] - sumMatrix[i-1][j-1] + matrix[i-1][j-1]; }
} int count = 0;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; ++j) {
count += countTarget(i, j, target, sumMatrix);
}
} return count;
} public int countTarget(int x, int y, int target, int[][] sumMatrix) {
int subCount = 0, sum = 0;
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
sum = sumMatrix[x][y] - sumMatrix[i][y] - sumMatrix[x][j] + sumMatrix[i][j];
if (sum == target) subCount++;
}
}
return subCount;
}
}
Analysis:
Firstly, we calculate the sum of a sub-matrix from [0, 0] to [i, j].
Secondly, traveling all the points in the sub-matrix as the start point and claculate the sum, if the sum equal to the target, the count number increase one.
Weekly Contest 139的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- 【死磕JVM】五年 整整五年了 该知道JVM加载机制了!
类加载 Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程 和那些编译时需要连接工作的语言不 ...
- 肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!
写在前面 不少小伙伴让我整理下有关SpringCloud和SpringCloudAlibaba的知识点,经过3天的收集和整理,冰河整理出这份4万字的SpringCloud与SpringCloudAli ...
- 原始提货单OBL
转: 原始提货单OBL 什么是原始提货单OBL? 原始提货单Original Bill of Lading,简称OBL.是货运单据或运输合同,可作为货物标题和装运收据.该文件确认承运人已收到货物.签发 ...
- #progma pack(x)说明
1.字节对齐(内存相关) 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数 ...
- Spring笔记(10) - 日志体系
一.概况 在项目开发当中,日志对于我们开发或运维人员来说,是一个必不可少的工具.在线下我们可以通过 debug 来查找排除问题,但对于线上系统来说,我们只能通过日志分析来查找问题,我们可以通过日志打印 ...
- Chrome OS超便捷安装指南
Chrome OS是一款Google开发的基于PC的操作系统. Google Chrome OS是一款基于Linux的开源操作系统.Google在自己的官方博客表示,初期,这一操作系统将定位于上网本. ...
- mysql 使用sleep操作 update 来停止一段时间执行语句 [骚操作]
update mytestTable inner join(select '2' as id, sleep(5)) a on mytestTable.id=a.id set mytestTable.n ...
- C语言II博客作业02
这个作业属于那个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-4 这个作业要求在哪里 https://edu.cnblogs.com/campus/zs ...
- MongoDB4.2 分片扫盲说明
说明: 在扫盲MongoDB相关的一些知识的时候,顺手做下笔记.本文将说明分片相关的内容.在比较早之前已经对这些有过说明,可以看MongoDB 分片的原理.搭建.应用.分片(sharding)是指将数 ...
- CMU15-455 Lab2 - task4 Concurrency Index -并发B+树索引算法的实现
最近在做 CMU-15-445 Database System,lab2 是需要完成一个支持并发操作的B+树,最后一部分的 Task4 是完成并发的索引这里对这部分加锁的思路和完成做一个总结,关于 B ...