Thảo luận các easy problems trên leetcode.com
Ref: https://leetcode.com/problemset/all/?difficulty=EASY&page=1
Thành tích
| Top | Thành viên | Hoàn thành |
|---|---|---|
| 1 | Quang | 1, 2 |
| 2 | Thịnh Lớn | 3, 6 |
| 3 | Quý | 4 |
| 4 | Khoa | 5 |
Vấn đề
- https://leetcode.com/problems/second-largest-digit-in-a-string/
Member: Quang
Submission: https://leetcode.com/submissions/detail/787843585/
Language: Java
class Solution {
public int secondHighest(String s) {
int len = s.length();
int max = -1;
int secondMax = -1;
for(int i = 0; i < len; ++i){
final char ch = s.charAt(i);
if(ch >= '0' && ch <= '9') {
int num = ch - '0';
if(max == -1 && secondMax == -1) {
max = num;
secondMax = num;
} else if(max < num){
secondMax = max;
max = num;
} else if(num < max) {
if(max == secondMax) secondMax = num;
else if(secondMax < num) secondMax = num;
}
}
}
return max == secondMax ? -1 : secondMax;
}
}
- https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/
Member: Quang
Submission: https://leetcode.com/submissions/detail/795397090/
Language: PHP
class Solution {
/**
* @param Integer[] $arr
* @param Integer $m
* @param Integer $k
* @return Boolean
*/
function containsPattern($arr, $m, $k) {
$splitItem = 0;
for ($i = $m; $i < count($arr); $i++) {
if($arr[$i] !== $arr[$i - $m]) {
$splitItem = 0;
} else {
if(++$splitItem === $m * ($k - 1)) {
return true;
}
}
}
return false;
}
}
- https://leetcode.com/problems/reverse-vowels-of-a-string/
Member: Thịnh Lớn
Submission: https://leetcode.com/submissions/detail/798642454/
Language: PHP
class Solution {
/**
* @param String $s
* @return String
*/
function reverseVowels($s) {
$len = strlen($s);
if ($len < 2) {
return $s;
}
$vowels = [];
for($i = 0; $i < $len; $i++) {
if ($s[$i] === 'a'
|| $s[$i] === 'e'
|| $s[$i] === 'i'
|| $s[$i] === 'o'
|| $s[$i] === 'u'
|| $s[$i] === 'A'
|| $s[$i] === 'E'
|| $s[$i] === 'I'
|| $s[$i] === 'O'
|| $s[$i] === 'U'
)
{
$vowels[] = $i;
}
}
$vowelsLen = count($vowels);
if ($vowelsLen < 2) {
return $s;
}
for($i = 0; $i < $vowelsLen/2; $i++) {
$middle = $s[$vowels[$i]];
$s[$vowels[$i]] = $s[$vowels[$vowelsLen - $i - 1]];
$s[$vowels[$vowelsLen - $i - 1]] = $middle;
}
return $s;
}
}
- https://leetcode.com/problems/count-the-number-of-consistent-strings/
Member: Qúy
Submission: https://leetcode.com/submissions/detail/800328356/
Language: PHP
// WAY 1 - readable way
class Solution {
/**
* @param String $allowed
* @param String[] $words
* @return Integer
*/
function countConsistentStrings($allowed, $words) {
$count = 0;
$test1 = count_chars($allowed,1);
foreach($words as $word){
$test2 = count_chars($word,1);
if(empty(array_diff_key($test2, $test1))){
$count++;
}
}
return $count;
}
}
// WAY 2 - performance way ? Sometimes timerun as same as WAY 1
class Solution {
/**
* @param String $allowed
* @param String[] $words
* @return Integer
*/
function countConsistentStrings($allowed, $words) {
$count = count($words);
foreach($words as $word){
for($i = 0; $i < strlen($word); $i++){
if(strpos($allowed,$word[$i]) === false) { // avoid position = 0
$count--;
break;
}
}
}
return $count;
}
}
- Other fancy solution in Python
Member: a Huy
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
return sum([1 for word in words if set(word).issubset(set(allowed))])
- https://leetcode.com/problems/the-latest-login-in-2020/
Member: Khoa
Submission: https://leetcode.com/submissions/detail/803507835/
Language: SQL
# 1
select user_id, max(time_stamp) as last_stamp
from Logins
where year(time_stamp) = 2020
group by user_id;
# 2
select L1.user_id, L1.time_stamp as last_stamp
from Logins L1
left join Logins L2
on L2.user_id = L1.user_id
and L2.time_stamp > L1.time_stamp
and year(L2.time_stamp) = 2020
where year(L1.time_stamp) = 2020
and L2.user_id is null;
# 3
select L1.user_id, L1.time_stamp as last_stamp
from Logins L1
where year(L1.time_stamp) = 2020
and not exists (select 1
from Logins L2
where L2.user_id = L1.user_id
and L2.time_stamp > L1.time_stamp
and year(L2.time_stamp) = 2020
order by time_stamp desc
limit 1);
- https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/
Member: Thịnh Lớn
Submission: https://leetcode.com/submissions/detail/804326676/
Language: PHP
class Solution {
/**
* @param String[] $patterns
* @param String $word
* @return Integer
*/
function numOfStrings($patterns, $word) {
$sum = 0;
foreach($patterns as $pattern) {
if (strpos($word, $pattern) !== false) $sum++;
}
return $sum;
}
}
-
https://leetcode.com/problems/article-views-i/
-
To be continued