Skip to content

Commit 4801355

Browse files
committed
🚀 13-Oct-2020
1 parent cd8d3dc commit 4801355

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Given the head of a linked list, return the list after sorting it in ascending order.
2+
3+
Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?
4+
5+
6+
7+
Example 1:
8+
9+
10+
Input: head = [4,2,1,3]
11+
Output: [1,2,3,4]
12+
Example 2:
13+
14+
15+
Input: head = [-1,5,3,4,0]
16+
Output: [-1,0,3,4,5]
17+
Example 3:
18+
19+
Input: head = []
20+
Output: []
21+
22+
23+
Constraints:
24+
25+
The number of nodes in the list is in the range [0, 5 * 104].
26+
-105 <= Node.val <= 105
27+
28+
29+
30+
31+
32+
33+
34+
35+
/**
36+
* Definition for singly-linked list.
37+
* struct ListNode {
38+
* int val;
39+
* ListNode *next;
40+
* ListNode() : val(0), next(nullptr) {}
41+
* ListNode(int x) : val(x), next(nullptr) {}
42+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
43+
* };
44+
*/
45+
class Solution {
46+
public:
47+
48+
ListNode* merge(ListNode* a, ListNode* b){
49+
if(!a) return b;
50+
if(!b) return a;
51+
52+
ListNode *result=NULL;
53+
54+
if(a->val <= b->val){
55+
result=a;
56+
result->next=merge(a->next, b);
57+
} else {
58+
result=b;
59+
result->next=merge(a, b->next);
60+
}
61+
62+
return result;
63+
}
64+
65+
ListNode* getMiddle(ListNode* head){
66+
ListNode *slow=head, *fast=head, *prev=NULL;
67+
while(fast!=NULL && fast->next!=NULL){
68+
prev=slow;
69+
slow=slow->next;
70+
fast=fast->next->next;
71+
}
72+
if(prev) prev->next=NULL;
73+
return slow;
74+
}
75+
76+
ListNode* sortList(ListNode* head) {
77+
if(head==NULL || head->next==NULL) return head;
78+
79+
ListNode* mid=getMiddle(head);
80+
ListNode* left=sortList(head);
81+
ListNode* right=sortList(mid);
82+
83+
return merge(left, right);
84+
}
85+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
A string is a valid parentheses string (denoted VPS) if it meets one of the following:
2+
3+
It is an empty string "", or a single character not equal to "(" or ")",
4+
It can be written as AB (A concatenated with B), where A and B are VPS's, or
5+
It can be written as (A), where A is a VPS.
6+
We can similarly define the nesting depth depth(S) of any VPS S as follows:
7+
8+
depth("") = 0
9+
depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's
10+
depth("(" + A + ")") = 1 + depth(A), where A is a VPS.
11+
For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.
12+
13+
Given a VPS represented as string s, return the nesting depth of s.
14+
15+
16+
17+
Example 1:
18+
19+
Input: s = "(1+(2*3)+((8)/4))+1"
20+
Output: 3
21+
Explanation: Digit 8 is inside of 3 nested parentheses in the string.
22+
Example 2:
23+
24+
Input: s = "(1)+((2))+(((3)))"
25+
Output: 3
26+
Example 3:
27+
28+
Input: s = "1+(2*3)/(2-1)"
29+
Output: 1
30+
Example 4:
31+
32+
Input: s = "1"
33+
Output: 0
34+
35+
36+
Constraints:
37+
38+
1 <= s.length <= 100
39+
s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
40+
It is guaranteed that parentheses expression s is a VPS.
41+
42+
43+
44+
45+
class Solution {
46+
public:
47+
int maxDepth(string s) {
48+
int open=0;
49+
int maxi=0;
50+
for(auto &ch:s){
51+
if(ch=='(') open++;
52+
else if(ch==')') open--;
53+
if(open>maxi) maxi=open;
54+
}
55+
return maxi;
56+
}
57+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.
2+
3+
The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once.
4+
5+
The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.
6+
7+
Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.
8+
9+
10+
11+
Example 1:
12+
13+
14+
15+
Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
16+
Output: 4
17+
Explanation: The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.
18+
Example 2:
19+
20+
21+
22+
Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
23+
Output: 5
24+
Explanation: There are 5 roads that are connected to cities 1 or 2.
25+
Example 3:
26+
27+
Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
28+
Output: 5
29+
Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
30+
31+
32+
Constraints:
33+
34+
2 <= n <= 100
35+
0 <= roads.length <= n * (n - 1) / 2
36+
roads[i].length == 2
37+
0 <= ai, bi <= n-1
38+
ai != bi
39+
Each pair of cities has at most one road connecting them.
40+
41+
42+
43+
44+
45+
46+
47+
48+
class Solution {
49+
public:
50+
int maximalNetworkRank(int n, vector<vector<int>>& roads) {
51+
vector<vector<bool> > grid(n, vector<bool> (n, false));
52+
for(auto &it: roads){
53+
grid[it[0]][it[1]]=true;
54+
grid[it[1]][it[0]]=true;
55+
}
56+
57+
vector<int> edges(n, 0);
58+
59+
for(int i=0;i<n;i++){
60+
for(int j=0;j<n;j++){
61+
if(grid[i][j]==1)
62+
edges[i]++;
63+
}
64+
}
65+
66+
int maxi=0;
67+
68+
69+
for(int i=0;i<n;i++){
70+
for(int j=i+1;j<n;j++){
71+
if(grid[i][j]==1)
72+
maxi=max(maxi, edges[i]+edges[j]-1);
73+
else maxi=max(maxi, edges[i]+edges[j]);
74+
}
75+
}
76+
77+
return maxi;
78+
}
79+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Given the head of a linked list, return the list after sorting it in ascending order.
2+
3+
Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?
4+
5+
6+
7+
Example 1:
8+
9+
10+
Input: head = [4,2,1,3]
11+
Output: [1,2,3,4]
12+
Example 2:
13+
14+
15+
Input: head = [-1,5,3,4,0]
16+
Output: [-1,0,3,4,5]
17+
Example 3:
18+
19+
Input: head = []
20+
Output: []
21+
22+
23+
Constraints:
24+
25+
The number of nodes in the list is in the range [0, 5 * 104].
26+
-105 <= Node.val <= 105
27+
28+
29+
30+
31+
32+
33+
34+
35+
/**
36+
* Definition for singly-linked list.
37+
* struct ListNode {
38+
* int val;
39+
* ListNode *next;
40+
* ListNode() : val(0), next(nullptr) {}
41+
* ListNode(int x) : val(x), next(nullptr) {}
42+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
43+
* };
44+
*/
45+
class Solution {
46+
public:
47+
48+
ListNode* merge(ListNode* a, ListNode* b){
49+
if(!a) return b;
50+
if(!b) return a;
51+
52+
ListNode *result=NULL;
53+
54+
if(a->val <= b->val){
55+
result=a;
56+
result->next=merge(a->next, b);
57+
} else {
58+
result=b;
59+
result->next=merge(a, b->next);
60+
}
61+
62+
return result;
63+
}
64+
65+
ListNode* getMiddle(ListNode* head){
66+
ListNode *slow=head, *fast=head, *prev=NULL;
67+
while(fast!=NULL && fast->next!=NULL){
68+
prev=slow;
69+
slow=slow->next;
70+
fast=fast->next->next;
71+
}
72+
if(prev) prev->next=NULL;
73+
return slow;
74+
}
75+
76+
ListNode* sortList(ListNode* head) {
77+
if(head==NULL || head->next==NULL) return head;
78+
79+
ListNode* mid=getMiddle(head);
80+
ListNode* left=sortList(head);
81+
ListNode* right=sortList(mid);
82+
83+
return merge(left, right);
84+
}
85+
};

0 commit comments

Comments
 (0)