Skip to content

Commit 71deb02

Browse files
Initial Commit
0 parents  commit 71deb02

File tree

12 files changed

+282
-0
lines changed

12 files changed

+282
-0
lines changed

Design Parking System/Problem.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
2+
3+
Implement the ParkingSystem class:
4+
5+
ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
6+
bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.
7+
8+
9+
10+
Example 1:
11+
12+
Input
13+
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
14+
[[1, 1, 0], [1], [2], [3], [1]]
15+
Output
16+
[null, true, true, false, false]
17+
18+
Explanation
19+
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
20+
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
21+
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
22+
parkingSystem.addCar(3); // return false because there is no available slot for a small car
23+
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

Design Parking System/main.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class ParkingSystem {
2+
/**
3+
* @param Integer $big
4+
* @param Integer $medium
5+
* @param Integer $small
6+
*/
7+
function __construct($big, $medium, $small) {
8+
$this->big_slots = $big;
9+
$this->med_slots = $medium;
10+
$this->sma_slots = $small;
11+
return null;
12+
}
13+
14+
/**
15+
* @param Integer $carType
16+
* @return Boolean
17+
*/
18+
function addCar($carType) {
19+
switch($carType){
20+
case 1:
21+
if($this->big_slots<1){ return false; }
22+
else{
23+
$this->big_slots-=1;
24+
return true;
25+
}
26+
case 2:
27+
if($this->med_slots<1){ return false; }
28+
else{
29+
$this->med_slots-=1;
30+
return true;
31+
}
32+
case 3:
33+
if($this->sma_slots<1){ return false; }
34+
else{
35+
$this->sma_slots-=1;
36+
return true;
37+
}
38+
39+
}
40+
}
41+
}
42+
43+
/**
44+
* Your ParkingSystem object will be instantiated and called as such:
45+
* $obj = ParkingSystem($big, $medium, $small);
46+
* $ret_1 = $obj->addCar($carType);
47+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
2+
3+
Return the answer in an array.
4+
5+
Example 1:
6+
7+
Input: nums = [8,1,2,2,3]
8+
Output: [4,0,1,1,3]
9+
Explanation:
10+
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
11+
For nums[1]=1 does not exist any smaller number than it.
12+
For nums[2]=2 there exist one smaller number than it (1).
13+
For nums[3]=2 there exist one smaller number than it (1).
14+
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
15+
Example 2:
16+
17+
Input: nums = [6,5,4,8]
18+
Output: [2,1,0,3]
19+
Example 3:
20+
21+
Input: nums = [7,7,7,7]
22+
Output: [0,0,0,0]
23+
24+
25+
Constraints:
26+
27+
2 <= nums.length <= 500
28+
0 <= nums[i] <= 100
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer[] $nums
5+
* @return Integer[]
6+
*/
7+
function smallerNumbersThanCurrent($nums) {
8+
$_arr = array();
9+
foreach($nums as &$num){
10+
$count = 0;
11+
for($i=0; $i<count($nums);$i+=1){
12+
if($num > $nums[$i]) { $count+=1; }
13+
}
14+
$_arr[] = $count;
15+
}
16+
return $_arr;
17+
}
18+
}

Jewels and Stones/Problem.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
2+
3+
Letters are case sensitive, so "a" is considered a different type of stone from "A".
4+
5+
Example 1:
6+
7+
Input: jewels = "aA", stones = "aAAbbbb"
8+
Output: 3
9+
Example 2:
10+
11+
Input: jewels = "z", stones = "ZZ"
12+
Output: 0
13+

Jewels and Stones/main.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $jewels
5+
* @param String $stones
6+
* @return Integer
7+
*/
8+
function numJewelsInStones($jewels, $stones) {
9+
$count = 0;
10+
$jewels = str_split($jewels);
11+
$stones = str_split($stones);
12+
for($i=0;$i<=count($jewels);$i+=1){
13+
for($j=0;$j<count($stones);$j+=1){
14+
if($jewels[$i] == $stones[$j]){
15+
$count+=1;
16+
}
17+
}
18+
}
19+
return $count;
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).
2+
3+
The valid times are those inclusively between 00:00 and 23:59.
4+
5+
Return the latest valid time you can get from time by replacing the hidden digits.
6+
7+
8+
Example 1:
9+
10+
Input: time = "2?:?0"
11+
Output: "23:50"
12+
Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
13+
Example 2:
14+
15+
Input: time = "0?:3?"
16+
Output: "09:39"
17+
Example 3:
18+
19+
Input: time = "1?:22"
20+
Output: "19:22"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $time
5+
* @return String
6+
*/
7+
function maximumTime($time) {
8+
$time=str_split($time);
9+
for($i=0; $i<count($time); $i++){
10+
if($time[$i] == '?'){
11+
echo $i;
12+
switch($i){
13+
case 0:
14+
$time[$i] = ($time[$i+1] >= 4) ? 1 : 2;
15+
break;
16+
case 1:
17+
$time[$i] = ($time[$i-1] == 2) ? 3 : 9;
18+
break;
19+
case 3:
20+
$time[$i] = 5;
21+
break;
22+
case 4:
23+
$time[$i] = 9;
24+
break;
25+
}
26+
27+
}
28+
29+
}
30+
return implode($time);
31+
}
32+
}

Number of Good Pairs/Problem.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Given an array of integers nums.
2+
3+
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
4+
5+
Return the number of good pairs.
6+
7+
Example 1:
8+
9+
Input: nums = [1,2,3,1,1,3]
10+
Output: 4
11+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
12+
Example 2:
13+
14+
Input: nums = [1,1,1,1]
15+
Output: 6
16+
Explanation: Each pair in the array are good.
17+
Example 3:
18+
19+
Input: nums = [1,2,3]
20+
Output: 0
21+

Number of Good Pairs/main.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer[] $nums
5+
* @return Integer
6+
*/
7+
function numIdenticalPairs($nums) {
8+
$count = 0;
9+
for($i = 0; $i < count($nums); $i+=1){
10+
for($j=$i+1; $j <= count($nums); $j+=1){
11+
if($nums[$j] == $nums[$i]){
12+
$count+=1;
13+
}
14+
}
15+
}
16+
//echo $count;
17+
return $count;
18+
}
19+
}

Shuffle String/Problem.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Given a string s and an integer array indices of the same length.
2+
3+
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
4+
5+
Return the shuffled string.
6+
7+
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
8+
Output: "leetcode"
9+
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
10+
Example 2:
11+
12+
Input: s = "abc", indices = [0,1,2]
13+
Output: "abc"
14+
Explanation: After shuffling, each character remains in its position.
15+
Example 3:
16+
17+
Input: s = "aiohn", indices = [3,1,4,2,0]
18+
Output: "nihao"
19+
Example 4:
20+
21+
Input: s = "aaiougrt", indices = [4,0,2,6,7,3,1,5]
22+
Output: "arigatou"
23+
Example 5:
24+
25+
Input: s = "art", indices = [1,0,2]
26+
Output: "rat"
27+

Shuffle String/main.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution{
2+
function restoreString($s, $indices) {
3+
$s = str_split($s);
4+
$str_arr = [];
5+
for($i=0; $i<count($indices);$i+=1){
6+
$str_arr[$indices[$i]] = $s[$i];
7+
}
8+
//$str_arr = leetcdoe
9+
//why not $str_arr = leetcode?
10+
ksort($str_arr);
11+
return implode($str_arr);
12+
}
13+
}

0 commit comments

Comments
 (0)