-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b9c490
commit bee1157
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// https://leetcode.com/problems/lemonade-change/ | ||
|
||
void main(List<String> args) { | ||
var bills = [5, 5, 10, 5, 20]; | ||
print(lemonadeChange(bills)); | ||
} | ||
|
||
lemonadeChange(bills) { | ||
int fives = 0; | ||
int tens = 0; | ||
for (int i = 0; i < bills.length; i++) { | ||
if (bills[i] == 5) { | ||
fives++; | ||
} else if (bills[i] == 10) { | ||
if (fives < 0) return false; | ||
fives--; | ||
tens++; | ||
} else if (bills[i] == 20) { | ||
if (tens == 0) { | ||
if (fives < 3) return false; | ||
fives -= 3; | ||
} else { | ||
if (fives < 1) return false; | ||
tens--; | ||
fives--; | ||
} | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// https://leetcode.com/problems/lemonade-change/ | ||
|
||
|
||
class Solution { | ||
public boolean lemonadeChange(int[] bills) { | ||
int fives = 0; | ||
int tens = 0; | ||
for (int i = 0; i < bills.length; i++) { | ||
if (bills[i] == 5) { | ||
fives++; | ||
} else if (bills[i] == 10) { | ||
if (fives < 0) return false; | ||
fives--; | ||
tens++; | ||
} else if (bills[i] == 20) { | ||
if (tens == 0) { | ||
if (fives < 3) return false; | ||
fives -= 3; | ||
} else { | ||
if (fives < 1) return false; | ||
tens--; | ||
fives--; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// https://leetcode.com/problems/lemonade-change/ | ||
|
||
var lemonadeChange = function (bills) { | ||
let fives = 0; | ||
let tens = 0; | ||
for (let i = 0; i < bills.length; i++) { | ||
if (bills[i] == 5) { | ||
fives++; | ||
} else if (bills[i] == 10) { | ||
if (fives < 0) return false; | ||
fives--; | ||
tens++; | ||
} else if (bills[i] == 20) { | ||
if (tens == 0) { | ||
if (fives < 3) return false; | ||
fives -= 3; | ||
} else { | ||
if (fives < 1) return false; | ||
tens--; | ||
fives--; | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
console.log(lemonadeChange([5, 5, 10, 5, 20])); |