Skip to content

Commit 28289b6

Browse files
committed
add some Solution
1 parent e56538f commit 28289b6

File tree

8 files changed

+484
-0
lines changed

8 files changed

+484
-0
lines changed

#1232/Solution.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean checkStraightLine(int[][] coordinates) {
3+
if (coordinates.length <= 1) {
4+
return true;
5+
}
6+
int m1 = coordinates[1][1] - coordinates[0][1]; // y2 - y1
7+
int m2 = coordinates[1][0] - coordinates[0][0]; // x2 - x1
8+
9+
int i = 2;
10+
while (i < coordinates.length) {
11+
if ((coordinates[i][1] - coordinates[0][1]) * m2 != (coordinates[i][0] - coordinates[0][0]) * m1) {
12+
return false;
13+
}
14+
i++;
15+
}
16+
return true;
17+
}
18+
}

#1233/Solution.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
public List<String> removeSubfolders(String[] folder) {
4+
Arrays.sort(folder);
5+
Set<String> set = new HashSet<>();
6+
for (int i = 0; i < folder.length; i++) {
7+
String str = folder[i];
8+
boolean contain = false;
9+
for (int j = 1; j < str.length(); j++) {
10+
if (str.charAt(j) == '/' && set.contains(str.substring(0, j))) {
11+
contain = true;
12+
break;
13+
}
14+
}
15+
if (!contain) {
16+
set.add(str);
17+
}
18+
}
19+
return new ArrayList<>(set);
20+
}
21+
}

#1234/Solution.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Solution {
2+
public int balancedString(String s) {
3+
int len=s.length();
4+
int[] a=new int[5];
5+
for(int i=0;i<s.length();i++)
6+
a[change(s.charAt(i))]++;
7+
if(judge(a,len)) return 0;
8+
int l=1,r=len,ans=len;
9+
while(l<=r)
10+
{
11+
int mid=l+(r-l)/2;
12+
if(check(s,a,mid))
13+
{
14+
ans=mid;
15+
r=mid-1;
16+
}
17+
else
18+
l=mid+1;
19+
}
20+
return ans;
21+
}
22+
23+
private boolean check(String s,int[] a,int len)
24+
{
25+
int length=s.length();
26+
int[] b=new int[5];
27+
for(int i=0;i<4;i++) b[i]=a[i];
28+
for(int i=0;i<len-1;i++)
29+
b[change(s.charAt(i))]--;
30+
for(int i=len-1;i<s.length();i++)
31+
{
32+
b[change(s.charAt(i))]--;
33+
int sum=0;
34+
boolean flag=true;
35+
for(int j=0;j<4;j++)
36+
{
37+
if(b[j]>length/4) {flag=false;break;}
38+
else sum+=length/4-b[j];
39+
}
40+
if(flag && sum<=len) return true;
41+
b[change(s.charAt(i-len+1))]++;
42+
}
43+
return false;
44+
}
45+
46+
private int change(char c)
47+
{
48+
if(c=='Q') return 0;
49+
if(c=='W') return 1;
50+
if(c=='E') return 2;
51+
if(c=='R') return 3;
52+
return -1;
53+
}
54+
55+
private boolean judge(int[] a,int len)
56+
{
57+
if(a[0]==a[1] && a[1]==a[2] && a[2]==a[3] && a[1]==len/4)
58+
return true;
59+
return false;
60+
}
61+
62+
}

#1235/Solution.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution {
2+
class Job implements Comparable<Job> {
3+
int start;
4+
int end;
5+
int profit;
6+
public Job(int start, int end, int profit) {
7+
this.start = start;
8+
this.end = end;
9+
this.profit = profit;
10+
}
11+
@Override
12+
public int compareTo(Job o) {
13+
int c = end - o.end;
14+
if (c != 0) {
15+
return c;
16+
}
17+
return start - o.start;
18+
}
19+
}
20+
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
21+
Job[] jobs = new Job[profit.length];
22+
for (int i = 0; i < profit.length; i++) {
23+
jobs[i] = new Job(startTime[i], endTime[i], profit[i]);
24+
}
25+
Arrays.sort(jobs);
26+
27+
profit = new int[profit.length];
28+
profit[0] = jobs[0].profit;
29+
for (int i = 1; i < profit.length; i++) {
30+
int lastMaxSum = foundMaxProfitBeforeStart(jobs, jobs[i].start, profit, i - 1);
31+
profit[i] = Math.max(profit[i - 1], lastMaxSum + jobs[i].profit);
32+
}
33+
34+
return profit[profit.length - 1];
35+
}
36+
37+
private int foundMaxProfitBeforeStart(Job[] jobs, int time, int[] profit, int right) {
38+
if (jobs.length < 60) {
39+
return foundMaxProfitBeforeStart1(jobs, time, profit, right);
40+
} else {
41+
return foundMaxProfitBeforeStart2(jobs, time, profit, right);
42+
}
43+
}
44+
45+
private int foundMaxProfitBeforeStart1(Job[] jobs, int time, int[] profit, int right) {
46+
for (int i = right; i >= 0; i--) {
47+
if (jobs[i].end <= time) {
48+
return profit[i];
49+
}
50+
}
51+
return 0;
52+
}
53+
54+
private int foundMaxProfitBeforeStart2(Job[] jobs, int time, int[] profit, int right) {
55+
int left = 0;
56+
while (left < right) {
57+
int mid = (left + right) / 2;
58+
59+
if (jobs[mid].end > time) {
60+
right = mid - 1;
61+
} else {
62+
if (mid == right || jobs[mid + 1].end > time) {
63+
return profit[mid];
64+
} else {
65+
left = mid + 1;
66+
}
67+
}
68+
}
69+
// left 不会超出边界,所以从left出发计算比较方便
70+
if (jobs[left].end > time) {
71+
left = left - 1;
72+
}
73+
return left == -1 ? 0 : profit[left];
74+
}
75+
}

#1237/Solution.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
public List<List<Integer>> findSolution(CustomFunction func, int z) {
3+
List<List<Integer>> list = new ArrayList<>();
4+
5+
int minX = 1;
6+
int maxX = maxX(func, z);
7+
for (int i = minX; i <= maxX; i++) {
8+
int j = findY(func, i, z);
9+
if (j != 0) {
10+
List<Integer> r = new ArrayList<>();
11+
r.add(i);
12+
r.add(j);
13+
list.add(r);
14+
}
15+
}
16+
17+
return list;
18+
}
19+
20+
21+
private int maxX(CustomFunction func, int z) {
22+
int left = 1;
23+
int right = 1000;
24+
while (left < right) {
25+
int mid = (left + right) / 2;
26+
int r = func.f(mid, 1);
27+
if (r == z) {
28+
return mid;
29+
} if (r > z) {
30+
right = mid;
31+
} else {
32+
left = mid + 1;
33+
}
34+
}
35+
if (func.f(left, 1) < z) {
36+
return left + 1;
37+
}
38+
return left;
39+
}
40+
41+
42+
private int findY(CustomFunction func, int i, int z) {
43+
int left = 1;
44+
int right = 1000;
45+
while (left < right) {
46+
int mid = (left + right) / 2;
47+
int r = func.f(i, mid);
48+
if (r == z) {
49+
return mid;
50+
} if (r > z) {
51+
right = mid;
52+
} else {
53+
left = mid + 1;
54+
}
55+
}
56+
return 0;
57+
}
58+
}

#1238/Solution.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<Integer> circularPermutation(int n, int start) {
3+
List<Integer> list = new ArrayList<Integer>();
4+
5+
int count = (int) Math.pow(2, n);
6+
list.add(start);
7+
int last = start;
8+
for (int i = 1; i < count; i++) {
9+
int flag = 1;
10+
int j = i;
11+
while ((j & 1) == 0) {
12+
flag = flag << 1;
13+
j = j >> 1;
14+
}
15+
last = last ^ flag;
16+
list.add(last);
17+
}
18+
19+
return list;
20+
}
21+
}

#1239/Solution.java

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
class Solution {
2+
3+
public class Serie {
4+
int len;
5+
boolean[] roads;
6+
boolean self = false;
7+
boolean has = false;
8+
}
9+
10+
public int maxLength(List<String> arr) {
11+
Serie[] matrix = new Serie[arr.size()];
12+
int len = arr.size();
13+
14+
15+
outloop:
16+
for (int i = 0; i < len; i++) {
17+
String str1 = arr.get(i);
18+
matrix[i] = new Serie();
19+
matrix[i].len = str1.length();
20+
matrix[i].roads = new boolean[len];
21+
22+
23+
boolean[] words = new boolean[26];
24+
for (int k = 0; k < str1.length(); k++) {
25+
int c = str1.charAt(k) - 'a';
26+
if (words[c]) {
27+
continue outloop;
28+
}
29+
words[c] = true;
30+
}
31+
matrix[i].self = true;
32+
33+
for (int j = i + 1; j < len; j++) {
34+
matrix[i].roads[j] = canCombine(Arrays.copyOf(words, words.length), arr.get(j));
35+
if (matrix[i].roads[j]) {
36+
matrix[i].has = true;
37+
}
38+
}
39+
}
40+
41+
arr = null;
42+
List<Serie> combines = new ArrayList<>();
43+
int maxLen = 0;
44+
45+
int i = 0;
46+
for (; i < matrix.length; i++) {
47+
if (matrix[i].self) {
48+
if (matrix[i].len > maxLen) {
49+
maxLen = matrix[i].len;
50+
}
51+
if (matrix[i].has) {
52+
combines.add(matrix[i]);
53+
i++;
54+
break;
55+
}
56+
}
57+
}
58+
59+
60+
for (; i < matrix.length; i++) {
61+
int lsize = combines.size();
62+
Serie sei = matrix[i];
63+
if (!sei.self) {
64+
continue;
65+
}
66+
67+
if (sei.len > maxLen) {
68+
maxLen = sei.len;
69+
}
70+
combines.add(sei);
71+
72+
for (int j = 0; j < lsize; j++) {
73+
Serie sej = combines.get(j);
74+
75+
if (sej.roads[i]) {
76+
boolean[] roads = new boolean[len];
77+
boolean has = false;
78+
for (int k = 0; k < len; k++) {
79+
roads[k] = sei.roads[k] && sej.roads[k];
80+
if (roads[k]) {
81+
has = true;
82+
}
83+
}
84+
int nlen = sej.len + sei.len;
85+
if (nlen > maxLen) {
86+
maxLen = nlen;
87+
}
88+
if (has) {
89+
Serie nse = new Serie();
90+
nse.len = nlen;
91+
nse.roads = roads;
92+
combines.add(nse);
93+
}
94+
}
95+
}
96+
97+
}
98+
99+
return maxLen;
100+
}
101+
102+
private boolean canCombine(boolean[] words, String str) {
103+
for (int i = 0; i < str.length(); i++) {
104+
int c = str.charAt(i) - 'a';
105+
if (words[c]) {
106+
return false;
107+
}
108+
words[c] = true;
109+
}
110+
return true;
111+
}
112+
}

0 commit comments

Comments
 (0)