Skip to content

Commit 4b1deb9

Browse files
committed
add Leetcode problems
1 parent ad8b075 commit 4b1deb9

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
4+
int ans =0;
5+
int n = arr.size();
6+
for(int i=0; i<n; i++){
7+
for(int j=i+1; j<n; j++){
8+
for(int k=j+1; k<n; k++){
9+
if(abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) ans++;
10+
}
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public bool DetectCapitalUse(string word) {
3+
bool first = (word[0] >= 'A' && word[0] <= 'Z');
4+
int capital = 0;
5+
for(int i=1; i<word.Length; i++){
6+
if(word[i] >= 'A' && word[i] <= 'Z') capital++;
7+
}
8+
9+
if(capital == 0) return true;
10+
if(first && capital == word.Length-1) return true;
11+
return false;
12+
}
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
public:
3+
typedef long long ll;
4+
const int M = 1000000007;
5+
6+
ll solve(vector<int>& v, vector<int>& w){
7+
vector<ll> pv, pw;
8+
for(int x : v){
9+
if(pv.size() == 0) pv.push_back(x);
10+
else pv.push_back(pv.back()+x);
11+
}
12+
for(int x : w){
13+
if(pw.size() == 0) pw.push_back(x);
14+
else pw.push_back(pw.back()+x);
15+
}
16+
ll ans = 0;
17+
ll last_match = -1;
18+
for(int i=0; i<v.size(); i++){
19+
// putting v[i]
20+
// check whether it exists on w
21+
ll it = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
22+
if(it >= w.size() || w[it] != v[i]){
23+
// no match, just put v[i] on ans
24+
ans += v[i];
25+
} else {
26+
//matched
27+
if(last_match == -1){
28+
last_match = it;
29+
ans += v[i];
30+
}
31+
else {
32+
ll it2 = lower_bound(v.begin(), v.end(), w[last_match]) - v.begin();
33+
ll pfirst = pv[i] - (it2 > -1 ? pv[it2] : 0);
34+
ll psecond = pw[it] - (last_match > -1 ? pw[last_match] : 0);
35+
//cout << pfirst << " " << psecond << endl;
36+
if(psecond > pfirst) ans += max(pfirst, psecond) - min(pfirst, psecond);
37+
ans += v[i];
38+
last_match = it;
39+
}
40+
}
41+
//cout << "after " << v[i] << " ans is " << ans << endl;
42+
}
43+
//cout << "current is " << ans << endl;
44+
//adjust ending
45+
if(last_match != -1){
46+
ll it2 = lower_bound(v.begin(), v.end(), w[last_match]) - v.begin();
47+
ll pfirst = pv.back() - (it2 > -1 ? pv[it2] : 0);
48+
ll psecond = pw.back() - (last_match > -1 ? pw[last_match] : 0);
49+
//cout << pfirst << " " << psecond << endl;
50+
if(psecond > pfirst) ans += max(pfirst, psecond) - min(pfirst, psecond);
51+
}
52+
return ans;
53+
}
54+
55+
int maxSum(vector<int>& nums1, vector<int>& nums2) {
56+
return max(solve(nums1, nums2), solve(nums2, nums1))%M;
57+
}
58+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode MergeKLists(ListNode[] lists) {
14+
ListNode head = new ListNode(-1);
15+
ListNode cur = head;
16+
while(true){
17+
int mn = -1;
18+
for(int i=0; i<lists.Length; i++){
19+
if(lists[i] == null) continue;
20+
if(mn == -1) mn = i;
21+
else if(lists[mn].val > lists[i].val){
22+
mn = i;
23+
}
24+
}
25+
if(mn == -1) break;
26+
cur.next = new ListNode(lists[mn].val);
27+
lists[mn] = lists[mn].next;
28+
cur = cur.next;
29+
}
30+
return head.next;
31+
}
32+
}

0 commit comments

Comments
 (0)