Skip to content

Commit 029e6ca

Browse files
committed
🚀 01-Nov-2020
1 parent 4801355 commit 029e6ca

8 files changed

+526
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
2+
3+
Return the decimal value of the number in the linked list.
4+
5+
6+
7+
Example 1:
8+
9+
10+
Input: head = [1,0,1]
11+
Output: 5
12+
Explanation: (101) in base 2 = (5) in base 10
13+
Example 2:
14+
15+
Input: head = [0]
16+
Output: 0
17+
Example 3:
18+
19+
Input: head = [1]
20+
Output: 1
21+
Example 4:
22+
23+
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
24+
Output: 18880
25+
Example 5:
26+
27+
Input: head = [0,0]
28+
Output: 0
29+
30+
31+
Constraints:
32+
33+
The Linked List is not empty.
34+
Number of nodes will not exceed 30.
35+
Each node's value is either 0 or 1.
36+
Hide Hint #1
37+
Traverse the linked list and store all values in a string or array. convert the values obtained to decimal value.
38+
Hide Hint #2
39+
You can solve the problem in O(1) memory using bits operation. use shift left operation ( << ) and or operation ( | ) to get the decimal value in one operation.
40+
41+
42+
43+
44+
45+
46+
47+
48+
/**
49+
* Definition for singly-linked list.
50+
* struct ListNode {
51+
* int val;
52+
* ListNode *next;
53+
* ListNode() : val(0), next(nullptr) {}
54+
* ListNode(int x) : val(x), next(nullptr) {}
55+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
56+
* };
57+
*/
58+
class Solution {
59+
public:
60+
int getDecimalValue(ListNode* head) {
61+
int len=0;
62+
ListNode *curr=head;
63+
while(curr){
64+
len++;
65+
curr=curr->next;
66+
}
67+
curr=head;
68+
len--;
69+
int res=0;
70+
71+
while(curr){
72+
res+=curr->val*pow(2, len);
73+
len--;
74+
curr=curr->next;
75+
}
76+
return res;
77+
}
78+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
2+
3+
A substring is a contiguous sequence of characters within a string.
4+
5+
6+
7+
Example 1:
8+
9+
Input: s = "aa"
10+
Output: 0
11+
Explanation: The optimal substring here is an empty substring between the two 'a's.
12+
Example 2:
13+
14+
Input: s = "abca"
15+
Output: 2
16+
Explanation: The optimal substring here is "bc".
17+
Example 3:
18+
19+
Input: s = "cbzxy"
20+
Output: -1
21+
Explanation: There are no characters that appear twice in s.
22+
Example 4:
23+
24+
Input: s = "cabbac"
25+
Output: 4
26+
Explanation: The optimal substring here is "abba". Other non-optimal substrings include "bb" and "".
27+
28+
29+
Constraints:
30+
31+
1 <= s.length <= 300
32+
s contains only lowercase English letters.
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
int maxLengthBetweenEqualCharacters(string s) {
41+
vector<int> first(26, -1);
42+
vector<int> last(26, -1);
43+
44+
int n=s.length();
45+
46+
for(int i=0;i<n;i++){
47+
if(first[s[i]-'a']==-1)
48+
first[s[i]-'a']=i;
49+
}
50+
51+
for(int i=0;i<n;i++){
52+
last[s[i]-'a']=i;
53+
}
54+
55+
int maxi=-1;
56+
57+
for(int i=0;i<26;i++){
58+
maxi=max(maxi, last[i]-first[i]-1);
59+
}
60+
61+
return maxi;
62+
}
63+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.
2+
3+
You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed. The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released.
4+
5+
The tester wants to know the key of the keypress that had the longest duration. The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0].
6+
7+
Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key may not have had the same duration.
8+
9+
Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.
10+
11+
12+
13+
Example 1:
14+
15+
Input: releaseTimes = [9,29,49,50], keysPressed = "cbcd"
16+
Output: "c"
17+
Explanation: The keypresses were as follows:
18+
Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
19+
Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).
20+
Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).
21+
Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).
22+
The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
23+
'c' is lexicographically larger than 'b', so the answer is 'c'.
24+
Example 2:
25+
26+
Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
27+
Output: "a"
28+
Explanation: The keypresses were as follows:
29+
Keypress for 's' had a duration of 12.
30+
Keypress for 'p' had a duration of 23 - 12 = 11.
31+
Keypress for 'u' had a duration of 36 - 23 = 13.
32+
Keypress for 'd' had a duration of 46 - 36 = 10.
33+
Keypress for 'a' had a duration of 62 - 46 = 16.
34+
The longest of these was the keypress for 'a' with duration 16.
35+
36+
37+
Constraints:
38+
39+
releaseTimes.length == n
40+
keysPressed.length == n
41+
2 <= n <= 1000
42+
1 <= releaseTimes[i] <= 109
43+
releaseTimes[i] < releaseTimes[i+1]
44+
keysPressed contains only lowercase English letters.
45+
46+
47+
48+
49+
50+
51+
class Solution {
52+
public:
53+
char slowestKey(vector<int>& releaseTimes, string keysPressed) {
54+
int n=releaseTimes.size();
55+
int maxi=releaseTimes[0];
56+
int index=0;
57+
for(int i=1;i<n;i++){
58+
if(releaseTimes[i]-releaseTimes[i-1] >=maxi){
59+
maxi=releaseTimes[i]-releaseTimes[i-1] ;
60+
index=i;
61+
}
62+
}
63+
return keysPressed[index];
64+
}
65+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
2+
3+
For example, these are arithmetic sequences:
4+
5+
1, 3, 5, 7, 9
6+
7, 7, 7, 7
7+
3, -1, -5, -9
8+
The following sequence is not arithmetic:
9+
10+
1, 1, 2, 5, 7
11+
You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
12+
13+
Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
14+
15+
16+
17+
Example 1:
18+
19+
Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
20+
Output: [true,false,true]
21+
Explanation:
22+
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
23+
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
24+
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.
25+
Example 2:
26+
27+
Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
28+
Output: [false,true,false,false,true,true]
29+
30+
31+
Constraints:
32+
33+
n == nums.length
34+
m == l.length
35+
m == r.length
36+
2 <= n <= 500
37+
1 <= m <= 500
38+
0 <= l[i] < r[i] < n
39+
-105 <= nums[i] <= 105
40+
41+
42+
43+
44+
45+
46+
47+
class Solution {
48+
public:
49+
50+
bool isArithmetic(vector<int> tmp){
51+
int n=tmp.size();
52+
sort(tmp.begin(), tmp.end());
53+
if(n<=1) return false;
54+
int cd=tmp[1]-tmp[0];
55+
for(int i=0;i<n-1;i++){
56+
if(tmp[i+1]-tmp[i]!=cd) return false;
57+
}
58+
59+
return true;
60+
}
61+
62+
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
63+
vector<bool> res;
64+
int n=nums.size();
65+
int m=l.size();
66+
for(int i=0;i<m;i++){
67+
int start=l[i];
68+
int end=r[i];
69+
vector<int> tmp;
70+
for(int j=start;j<=end;j++)
71+
tmp.push_back(nums[j]);
72+
73+
res.push_back(isArithmetic(tmp));
74+
75+
}
76+
77+
return res;
78+
79+
}
80+
};

competitive programming/leetcode/198. House Robber.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,33 @@ Step 3. Recursive + memo (top-down).
5757
5858
5959
//Sourece: https://leetcode.com/problems/house-robber/discuss/156523/From-good-to-great.-How-to-approach-most-of-DP-problems.
60+
61+
*/
62+
63+
64+
65+
66+
67+
68+
69+
70+
// Optimized
71+
72+
class Solution {
73+
public:
74+
75+
int robUtil(vector<int> &nums, int l, int r){
76+
int prev=0, curr=0;
77+
for(int i=l;i<=r;i++){
78+
int tmp=max(prev+nums[i], curr);
79+
prev=curr;
80+
curr=tmp;
81+
}
82+
return curr;
83+
}
84+
85+
int rob(vector<int>& nums) {
86+
int n=nums.size();
87+
return robUtil(nums, 0, n-1);
88+
}
89+
};

0 commit comments

Comments
 (0)