You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/find-pivot-index/README.md
+22-15Lines changed: 22 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,14 +11,14 @@
11
11
12
12
## 724. Find Pivot Index (Easy)
13
13
14
-
<p>Given an array of integers <code>nums</code>, write a method that returns the "pivot" index of this array.
15
-
</p><p>
16
-
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
17
-
</p><p>
18
-
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
19
-
</p>
20
-
21
-
<p><b>Example 1:</b><br />
14
+
<p>Given an array of integers <code>nums</code>, write a method that returns the "pivot" index of this array.</p>
15
+
16
+
<p>We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.</p>
17
+
18
+
<p>If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.</p>
19
+
20
+
<p><b>Example 1:</b></p>
21
+
22
22
<pre>
23
23
<b>Input:</b>
24
24
nums = [1, 7, 3, 6, 5, 6]
@@ -27,22 +27,29 @@ nums = [1, 7, 3, 6, 5, 6]
27
27
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
28
28
Also, 3 is the first index where this occurs.
29
29
</pre>
30
-
</p>
31
30
32
-
<p><b>Example 2:</b><br />
31
+
<p> </p>
32
+
33
+
<p><b>Example 2:</b></p>
34
+
33
35
<pre>
34
36
<b>Input:</b>
35
37
nums = [1, 2, 3]
36
38
<b>Output:</b> -1
37
39
<b>Explanation:</b>
38
40
There is no index that satisfies the conditions in the problem statement.
39
41
</pre>
40
-
</p>
41
42
42
-
<p><b>Note:</b>
43
-
<li>The length of <code>nums</code> will be in the range <code>[0, 10000]</code>.</li>
44
-
<li>Each element <code>nums[i]</code> will be an integer in the range <code>[-1000, 1000]</code>.</li>
45
-
</p>
43
+
<p> </p>
44
+
45
+
<p><b>Note:</b></p>
46
+
47
+
<ul>
48
+
<li>The length of <code>nums</code> will be in the range <code>[0, 10000]</code>.</li>
49
+
<li>Each element <code>nums[i]</code> will be an integer in the range <code>[-1000, 1000]</code>.</li>
Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.
37
43
38
-
Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.</p>
44
+
<p> </p>
45
+
For the sample data above, your query should return the following result.
46
+
47
+
<p> </p>
39
48
40
-
For the sample data above, your query should return the following result.</p>
41
49
<pre>
42
50
|accept_rate|
43
51
|-----------|
44
52
| 0.80|
45
-
</pre></p>
53
+
</pre>
46
54
55
+
<p> </p>
47
56
<b>Note:</b>
48
-
<li>The accepted requests are not necessarily from the table <code>friend_request</code>. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.</li>
49
-
<li>It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.</li>
50
-
<li>If there is no requests at all, you should return 0.00 as the accept_rate. </li>
51
-
</p>
52
57
53
-
<b>Explanation:</b> There are 4 unique accepted requests, and there are 5 requests in total. So the rate is 0.80.</p>
58
+
<ul>
59
+
<li>The accepted requests are not necessarily from the table <code>friend_request</code>. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.</li>
60
+
<li>It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.</li>
61
+
<li>If there is no requests at all, you should return 0.00 as the accept_rate.</li>
62
+
</ul>
63
+
64
+
<p> </p>
65
+
<b>Explanation:</b> There are 4 unique accepted requests, and there are 5 requests in total. So the rate is 0.80.
66
+
67
+
<p> </p>
68
+
<b>Follow-up:</b>
54
69
55
-
<b>Follow-up:</b></br>
56
-
<li>Can you write a query to return the accept rate but for every month?</li>
57
-
<li>How about the cumulative accept rate for every day?</li>
70
+
<ul>
71
+
<li>Can you write a query to return the accept rate but for every month?</li>
72
+
<li>How about the cumulative accept rate for every day?</li>
Copy file name to clipboardExpand all lines: problems/friend-requests-ii-who-has-the-most-friends/README.md
+17-12Lines changed: 17 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,12 @@
11
11
12
12
## 602. Friend Requests II: Who Has the Most Friends (Medium)
13
13
14
-
In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.</p>
14
+
In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.
15
+
<p> </p>
15
16
Table <code>request_accepted</code> holds the data of friend acceptance, while <b>requester_id</b> and <b>accepter_id</b> both are the id of a person.
16
-
</p>
17
+
18
+
<p> </p>
19
+
17
20
<pre>
18
21
| requester_id | accepter_id | accept_date|
19
22
|--------------|-------------|------------|
@@ -22,23 +25,25 @@ Table <code>request_accepted</code> holds the data of friend acceptance, while <
22
25
| 2 | 3 | 2016-06-08 |
23
26
| 3 | 4 | 2016-06-09 |
24
27
</pre>
25
-
26
28
Write a query to find the the people who has most friends and the most friends number. For the sample data above, the result is:
29
+
27
30
<pre>
28
31
| id | num |
29
32
|----|-----|
30
33
| 3 | 3 |
31
34
</pre>
32
-
33
35
<b>Note:</b>
34
-
<li>It is guaranteed there is only 1 people having the most friends.</li>
35
-
<li>The friend request could only been accepted once, which mean there is no multiple records with the same <b>requester_id</b> and <b>accepter_id</b> value.
36
-
</p>
37
-
38
-
<b>Explanation:</b></br>
39
-
The person with id '3' is a friend of people '1', '2' and '4', so he has 3 friends in total, which is the most number than any others.</p>
40
-
41
-
<b>Follow-up:</b></br> In the real world, multiple people could have the same most number of friends, can you find all these people in this case?
36
+
37
+
<ul>
38
+
<li>It is guaranteed there is only 1 people having the most friends.</li>
39
+
<li>The friend request could only been accepted once, which mean there is no multiple records with the same <b>requester_id</b> and <b>accepter_id</b> value.
40
+
<p> </p>
41
+
<b>Explanation:</b><br />
42
+
The person with id '3' is a friend of people '1', '2' and '4', so he has 3 friends in total, which is the most number than any others.
43
+
<p> </p>
44
+
<b>Follow-up:</b><br />
45
+
In the real world, multiple people could have the same most number of friends, can you find all these people in this case?</li>
<p>Get the highest answer rate question from a table <code>survey_log</code> with these columns: <b>uid</b>, <b>action</b>, <b>question_id</b>, <b>answer_id</b>, <b>q_num</b>, <b>timestamp</b>.</p>
18
15
19
-
<p>
20
-
uid means user id; action has these kind of values: "show", "answer", "skip";
21
-
answer_id is not null when action column is "answer", while is null for "show" and "skip";
22
-
q_num is the numeral order of the question in current session.
23
-
</p>
16
+
<p>uid means user id; action has these kind of values: "show", "answer", "skip"; answer_id is not null when action column is "answer", while is null for "show" and "skip"; q_num is the numeral order of the question in current session.</p>
24
17
25
18
<p>Write a sql query to identify the question which has the highest answer rate.</p>
Copy file name to clipboardExpand all lines: problems/investments-in-2016/README.md
+5-4Lines changed: 5 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,8 +14,8 @@
14
14
<p>Write a query to print the sum of all total investment values in 2016 (<b>TIV_2016</b>), to a scale of 2 decimal places, for all policy holders who meet the following criteria:</p>
15
15
16
16
<ol>
17
-
<li>Have the same <b>TIV_2015</b> value as one or more other policyholders.</li>
18
-
<li>Are not located in the same city as any other policyholder (i.e.: the (latitude, longitude) attribute pairs must be unique).</li>
17
+
<li>Have the same <b>TIV_2015</b> value as one or more other policyholders.</li>
18
+
<li>Are not located in the same city as any other policyholder (i.e.: the (latitude, longitude) attribute pairs must be unique).</li>
19
19
</ol>
20
20
21
21
<p><b>Input Format:</b><br />
@@ -31,7 +31,7 @@ The <b><i>insurance</i></b> table is described as follows:</p>
31
31
| LON | NUMERIC(5,2) |
32
32
</pre>
33
33
34
-
<p>where <b>PID</b> is the policyholder's policy ID, <b>TIV_2015</b> is the total investment value in 2015, <b>TIV_2016</b> is the total investment value in 2016, <b>LAT</b> is the latitude of the policy holder's city, and <b>LON</b> is the longitude of the policy holder's city.</p>
34
+
<p>where <b>PID</b> is the policyholder's policy ID, <b>TIV_2015</b> is the total investment value in 2015, <b>TIV_2016</b> is the total investment value in 2016, <b>LAT</b> is the latitude of the policy holder's city, and <b>LON</b> is the longitude of the policy holder's city.</p>
35
35
36
36
<p><b>Sample Input</b></p>
37
37
@@ -45,6 +45,7 @@ The <b><i>insurance</i></b> table is described as follows:</p>
45
45
</pre>
46
46
47
47
<p><b>Sample Output</b></p>
48
+
48
49
<pre>
49
50
| TIV_2016 |
50
51
|----------|
@@ -55,7 +56,7 @@ The <b><i>insurance</i></b> table is described as follows:</p>
55
56
56
57
<pre>
57
58
The first record in the table, like the last record, meets both of the two criteria.
58
-
The <b>TIV_2015</b> value '10' is as the same as the third and forth record, and its location unique.
59
+
The <b>TIV_2015</b> value '10' is as the same as the third and forth record, and its location unique.
59
60
60
61
The second record does not meet any of the two criteria. Its <b>TIV_2015</b> is not like any other policyholders.
Copy file name to clipboardExpand all lines: problems/k-inverse-pairs-array/README.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,41 +11,41 @@
11
11
12
12
## 629. K Inverse Pairs Array (Hard)
13
13
14
-
<p>
15
-
Given two integers <code>n</code> and <code>k</code>, find how many different arrays consist of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> inverse pairs.
16
-
</p>
17
-
<p>
18
-
We define an inverse pair as following:
19
-
For <code>i<sub>th</sub></code> and <code>j<sub>th</sub></code> element in the array, if <code>i</code> < <code>j</code> and <code>a[i]</code> > <code>a[j]</code> then it's an inverse pair; Otherwise, it's not.
20
-
</p>
21
-
22
-
<p>
23
-
Since the answer may be very large, the answer should be modulo 10<sup>9</sup> + 7.
24
-
</p>
25
-
26
-
<p><b>Example 1:</b><br />
14
+
<p>Given two integers <code>n</code> and <code>k</code>, find how many different arrays consist of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> inverse pairs.</p>
15
+
16
+
<p>We define an inverse pair as following: For <code>i<sub>th</sub></code> and <code>j<sub>th</sub></code> element in the array, if <code>i</code> < <code>j</code> and <code>a[i]</code> > <code>a[j]</code> then it's an inverse pair; Otherwise, it's not.</p>
17
+
18
+
<p>Since the answer may be very large, the answer should be modulo 10<sup>9</sup> + 7.</p>
19
+
20
+
<p><b>Example 1:</b></p>
21
+
27
22
<pre>
28
23
<b>Input:</b> n = 3, k = 0
29
24
<b>Output:</b> 1
30
25
<b>Explanation:</b>
31
26
Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pair.
32
27
</pre>
33
-
</p>
34
28
35
-
<p><b>Example 2:</b><br />
29
+
<p> </p>
30
+
31
+
<p><b>Example 2:</b></p>
32
+
36
33
<pre>
37
34
<b>Input:</b> n = 3, k = 1
38
35
<b>Output:</b> 2
39
36
<b>Explanation:</b>
40
37
The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
41
38
</pre>
42
-
</p>
43
39
44
-
<p><b>Note:</b><br>
40
+
<p> </p>
41
+
42
+
<p><b>Note:</b></p>
43
+
45
44
<ol>
46
-
<li>The integer <code>n</code> is in the range [1, 1000] and <code>k</code> is in the range [0, 1000].</li>
45
+
<li>The integer <code>n</code> is in the range [1, 1000] and <code>k</code> is in the range [0, 1000].</li>
0 commit comments