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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- Python处理不平衡数据
参考文献 所谓的不平衡数据集指的是数据集各个类别的样本量极不均衡.以二分类问题为例,假设正类的样本数量远大于负类的样本数量,通常情况下通常情况下把多数类样本的比例接近100:1这种情况下的数据称为不平 ...
- ViewPager 高度自适应
public class ContentViewPager extends ViewPager { public ContentViewPager(Context context) { super(c ...
- ADT基础(二)—— Tree,Heap and Graph
ADT基础(二)-- Tree,Heap and Graph 1 Tree(二叉树) 先根遍历 (若二叉树为空,则退出,否则进行下面操作) 访问根节点 先根遍历左子树 先根遍历右子树 退出 访问顺序为 ...
- Markdown基础使用方法
Markdown基础使用方法 标题的几种用法 * 选中标题(Ctrl+1~Crtl+6),分别为标题1-6.* #+空格+内容 为一级标题##+空格+内容为二级标题:以此类推. 字体快捷键及使用方法 ...
- HDOJ-6685(暴力+思维)
Rikka With Coin HDOJ-6685 主要的思想如下: 首先10元的硬币最多只会用一个,如果用了两个,直接替换成一个10元.一个20元一定不亏. 20元的硬币最多只会用三个,如果用了四个 ...
- C#深度复制和浅度复制
C#深度复制和浅度复制 复制一个值变量很简单,新建一个变量然后将原来的变量赋值过去就行,但是复制一个引用变量这种方法是不行的,如果不明白为什么可以先看看这篇解释 引用类型变量和值类型变量在赋值时的不同 ...
- MindSpore:基于本地差分隐私的 Bandit 算法
摘要:本文将先简单介绍Bandit 问题和本地差分隐私的相关背景,然后介绍基于本地差分隐私的 Bandit 算法,最后通过一个简单的电影推荐场景来验证 LDP LinUCB 算法. Bandit问题是 ...
- ArrayList 、Vector 和 LinkedList 有什么区别?
ArrayList.Vector .LinkedList 类均在java.util 包中,均为可伸缩数组,即可以动态改变长度的数组. ArrayList 和 Vector 都是基于存储元素的 Obje ...
- jqgrid 实现表格随select框内容改变而刷新
要实现的功能如下:当选择框选择数据源由原始数据切换到组合后数据时,界面左侧jqgrid表格随之改变.效果如下: 实现代码: 界面顶部select选择框:要点是用localStory将选择框的选择信息记 ...
- Vite2+Electron仿抖音|vite2.x+electron12+vant3短视频|直播|聊天
整合vite2+electron12跨平台仿抖音电脑版实战Vite2-ElectronDouYin. 基于vite2.0+electron12+vant3+swiper6+v3popup等技术跨端仿制 ...