Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f251be3

Browse files
20200818
1 parent cfc97c8 commit f251be3

5 files changed

+128
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=1103 lang=java
3+
*
4+
* [1103] Distribute Candies to People
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] distributeCandies(int candies, int num_people) {
10+
int[] res = new int[num_people];
11+
int cur = 1, pos = 0;
12+
while (candies > 0) {
13+
int n = Math.min(cur++, candies);
14+
candies -= n;
15+
res[pos++] += n;
16+
if (pos == num_people) {
17+
pos = 0;
18+
}
19+
}
20+
return res;
21+
}
22+
}
23+
// @lc code=end
24+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=1312 lang=java
3+
*
4+
* [1312] Minimum Insertion Steps to Make a String Palindrome
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
区间dp, 从小区间开始dp
11+
dp[i][j]: [i,j]范围内构成palindrome最小插入的字符数
12+
dp[i][j] = s[i] == s[j] ? dp[i+1][j-1] : min(dp[i+1][j],d[i][j-1]) + 1
13+
time: O(n^2)
14+
space: O(n^2)
15+
*/
16+
public int minInsertions(String s) {
17+
if (s == null || s.length() < 2) return 0;
18+
int n = s.length();
19+
int[][] dp = new int[n][n];
20+
for (int size = 2; size <= n; ++size) {
21+
for (int i = 0, j = size - 1; j < n; ++i, ++j) {
22+
dp[i][j] = s.charAt(i) == s.charAt(j) ? dp[i + 1][j - 1] : Math.min(dp[i + 1][j], dp[i][j - 1]) + 1;
23+
}
24+
}
25+
return dp[0][n - 1];
26+
}
27+
}
28+
// @lc code=end
29+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=777 lang=java
3+
*
4+
* [777] Swap Adjacent in LR String
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
X看作空格,R看作向右走,L看作向左走,其中不能跨过L/R
11+
LR 频率不想等 返回false
12+
如果对于非space的两个位置的字母不相同,则返回false(会跨过L,R)
13+
如果L的index更小则返回false
14+
如果R的index更大则返回false
15+
time: O(n)
16+
space: O(1)
17+
*/
18+
public boolean canTransform(String start, String end) {
19+
if (start.length() != end.length()) return false;
20+
int i = 0, j = 0;
21+
while (i < start.length() || j < end.length()) {
22+
while (i < start.length() && start.charAt(i) == 'X') ++i;
23+
while (j < end.length() && end.charAt(j) == 'X') ++j;
24+
if (i == start.length() && j == end.length()) return true;
25+
if (i == start.length() || j == end.length()) return false;
26+
if (start.charAt(i) != end.charAt(j)) return false;
27+
if (start.charAt(i) == 'L' && i < j) return false;
28+
if (start.charAt(i) == 'R' && i > j) return false;
29+
i++;
30+
j++;
31+
}
32+
return true;
33+
}
34+
}
35+
// @lc code=end
36+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=967 lang=java
3+
*
4+
* [967] Numbers With Same Consecutive Differences
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
dfs
11+
注意: list.toArray()只能返回Object[], 如果要返回int[]用stream或者一个一个添加
12+
time: O(10 * 2^N)
13+
space: O(n)
14+
*/
15+
public int[] numsSameConsecDiff(int N, int K) {
16+
if (N == 0) return new int[]{0};
17+
List<Integer> res = new ArrayList<>();
18+
dfs(res, N, K, 0);
19+
return res.stream().mapToInt(i -> i).toArray();
20+
}
21+
22+
private void dfs(List<Integer> res, int N, int K, int cur) {
23+
if (N == 0) {
24+
res.add(cur);
25+
return;
26+
}
27+
for (int i = 0; i < 10; ++i) {
28+
if (cur == 0 && i == 0 && N != 1) continue; // 0 is valid
29+
if (cur == 0) {
30+
dfs(res, N - 1, K, i); // first digit随意
31+
} else if (Math.abs(cur % 10 - i) == K) {
32+
dfs(res, N - 1, K, cur * 10 + i);
33+
}
34+
}
35+
}
36+
}
37+
// @lc code=end
38+

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
[1320. Minimum Distance to Type a Word Using Two Fingers](https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/) | [dfs+memorization](https://github.com/Yukinichi/leetcode/blob/master/Java/1320.minimum-distance-to-type-a-word-using-two-fingers.java)
4848
[221. Maximal Square](https://leetcode.com/problems/maximal-square/) | [从对应边长找状态转移方程](https://github.com/Yukinichi/leetcode/blob/master/Java/221.maximal-square.java)
4949
[1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [LCS问题](https://github.com/Yukinichi/leetcode/blob/master/Java/1143.longest-common-subsequence.java)
50+
[1312. Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/) | [区间dp(小区间->大区间)](https://github.com/Yukinichi/leetcode/blob/master/Java/1312.minimum-insertion-steps-to-make-a-string-palindrome.java)
5051

5152

5253
## Fenwick Tree / Binary Indexed Tree(BIT)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /