Skip to content

Commit 5d42127

Browse files
committed
leetcode
1 parent 63d1df4 commit 5d42127

File tree

4 files changed

+290
-40
lines changed

4 files changed

+290
-40
lines changed
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
- * Pascal's Triangle II *-
4+
5+
Given an integer rowIndex, return the rowIndex'th (0-indexed) row of the Pascal's triangle.
6+
7+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
8+
9+
10+
11+
12+
Example 1:
13+
14+
Input: rowIndex = 3
15+
Output: [1,3,3,1]
16+
Example 2:
17+
18+
Input: rowIndex = 0
19+
Output: [1]
20+
Example 3:
21+
22+
Input: rowIndex = 1
23+
Output: [1,1]
24+
25+
26+
Constraints:
27+
28+
0 <= rowIndex <= 33
29+
30+
31+
*/
32+
33+
class A {
34+
// Runtime: 417 ms, faster than 76.92% of Dart online submissions for Pascal's Triangle II.
35+
// Memory Usage: 140.6 MB, less than 73.08% of Dart online submissions for Pascal's Triangle II.
36+
List<int> getRow(int rowIndex) {
37+
List<int> array = List.filled(rowIndex + 1, 0);
38+
array[0] = 1;
39+
for (int i = 1; i <= rowIndex; i++) {
40+
for (int j = i; j > 0; j--) {
41+
array[j] = array[j] + array[j - 1];
42+
}
43+
}
44+
return array;
45+
}
46+
47+
List<int> getRowing(int rowIndex) {
48+
// Runtime: 464 ms, faster than 50.00% of Dart online submissions for Pascal's Triangle II.
49+
// Memory Usage: 140.1 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
50+
List<int> anotherArray = List.filled(rowIndex + 1, 1);
51+
for (var i = 1; i <= rowIndex; i++) {
52+
for (var j = i - 1; j > 0; j--) {
53+
anotherArray[j] = anotherArray[j] + anotherArray[j - 1];
54+
}
55+
}
56+
return anotherArray;
57+
}
58+
}
59+
60+
class B {
61+
// Runtime: 460 ms, faster than 61.54% of Dart online submissions for Pascal's Triangle II.
62+
// Memory Usage: 140.2 MB, less than 80.77% of Dart online submissions for Pascal's Triangle II.
63+
List<int> getRow(int rowIndex) {
64+
List<int> arr = List.filled(rowIndex + 1, 0);
65+
arr[0] = 1;
66+
67+
for (int i = 1; i <= rowIndex; i++) {
68+
for (int j = i; j > 0; j--) {
69+
arr[j] += arr[j - 1];
70+
}
71+
}
72+
73+
List<int> res = [];
74+
for (int a in arr) {
75+
res.add(a);
76+
}
77+
return res;
78+
}
79+
}
80+
81+
class C {
82+
// Runtime: 484 ms, faster than 38.46% of Dart online submissions for Pascal's Triangle II.
83+
// Memory Usage: 140.1 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
84+
List<int> getRow(int rowIndex) {
85+
List<int> ans = List.filled(rowIndex + 1, 0);
86+
ans[0] = ans[rowIndex] = 1;
87+
for (int i = 1, up = rowIndex; i < rowIndex; i++, up--)
88+
ans[i] = ans[i - 1] * up ~/ i;
89+
return ans;
90+
}
91+
}
92+
93+
class D {
94+
// Runtime: 432 ms, faster than 73.08% of Dart online submissions for Pascal's Triangle II.
95+
// Memory Usage: 139.9 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
96+
List<int> getRow(int rowIndex) {
97+
// recursive
98+
void buildRow(List<int> pascal, int rowIndex, int temp, int up, int down) {
99+
if (down > rowIndex) return;
100+
temp = temp * up ~/ down;
101+
pascal.add(temp);
102+
buildRow(pascal, rowIndex, temp, --up, ++down);
103+
}
104+
105+
List<int> pascal = [];
106+
pascal.add(1);
107+
buildRow(pascal, rowIndex, 1, rowIndex, 1);
108+
return pascal;
109+
}
110+
}
111+
112+
class E {
113+
// Runtime: 458 ms, faster than 65.38% of Dart online submissions for Pascal's Triangle II.
114+
// Memory Usage: 138.1 MB, less than 100.00% of Dart online submissions for Pascal's Triangle II.
115+
List<int> getRow(int rowIndex) {
116+
List<int> row = [];
117+
row.insert(0, 1);
118+
for (int i = 1; i <= rowIndex; i++) {
119+
row.insert(i, row.elementAt(i - 1) * (rowIndex - i + 1) ~/ i);
120+
}
121+
return row;
122+
}
123+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
func getRow(r int) []int {
4+
var ans = make([]int, r+1)
5+
ans[0] = 1
6+
ans[r] = 1
7+
for i := 1; i <= r; i++ {
8+
ans[i] = ans[i-1] * (r - i + 1) / i
9+
}
10+
return ans
11+
}
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# ✅ Dart | simple fast easy and linear solution
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
// Runtime: 417 ms, faster than 76.92% of Dart online submissions for Pascal's Triangle II.
8+
// Memory Usage: 140.6 MB, less than 73.08% of Dart online submissions for Pascal's Triangle II.
9+
List<int> getRow(int rowIndex) {
10+
List<int> array = List.filled(rowIndex + 1, 0);
11+
array[0] = 1;
12+
for (int i = 1; i <= rowIndex; i++) {
13+
for (int j = i; j > 0; j--) {
14+
array[j] = array[j] + array[j - 1];
15+
}
16+
}
17+
return array;
18+
}
19+
// This is second solution not part to above both function are separate
20+
List<int> getRowing(int rowIndex) {
21+
// Runtime: 464 ms, faster than 50.00% of Dart online submissions for Pascal's Triangle II.
22+
// Memory Usage: 140.1 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
23+
List<int> anotherArray = List.filled(rowIndex + 1, 1);
24+
for (var i = 1; i <= rowIndex; i++) {
25+
for (var j = i - 1; j > 0; j--) {
26+
anotherArray[j] = anotherArray[j] + anotherArray[j - 1];
27+
}
28+
}
29+
return anotherArray;
30+
}
31+
}
32+
```
33+
34+
## Solution - 2
35+
36+
```dart
37+
class Solution {
38+
// Runtime: 460 ms, faster than 61.54% of Dart online submissions for Pascal's Triangle II.
39+
// Memory Usage: 140.2 MB, less than 80.77% of Dart online submissions for Pascal's Triangle II.
40+
List<int> getRow(int rowIndex) {
41+
List<int> arr = List.filled(rowIndex + 1, 0);
42+
arr[0] = 1;
43+
44+
for (int i = 1; i <= rowIndex; i++) {
45+
for (int j = i; j > 0; j--) {
46+
arr[j] += arr[j - 1];
47+
}
48+
}
49+
50+
List<int> res = [];
51+
for (int a in arr) {
52+
res.add(a);
53+
}
54+
return res;
55+
}
56+
}
57+
```
58+
59+
## Solution - 3
60+
61+
```dart
62+
class Solution {
63+
// Runtime: 484 ms, faster than 38.46% of Dart online submissions for Pascal's Triangle II.
64+
// Memory Usage: 140.1 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
65+
List<int> getRow(int rowIndex) {
66+
List<int> ans = List.filled(rowIndex + 1, 0);
67+
ans[0] = ans[rowIndex] = 1;
68+
for (int i = 1, up = rowIndex; i < rowIndex; i++, up--)
69+
ans[i] = ans[i - 1] * up ~/ i;
70+
return ans;
71+
}
72+
}
73+
```
74+
75+
## Solution - 4
76+
77+
```dart
78+
class Solution {
79+
// Runtime: 432 ms, faster than 73.08% of Dart online submissions for Pascal's Triangle II.
80+
// Memory Usage: 139.9 MB, less than 96.15% of Dart online submissions for Pascal's Triangle II.
81+
List<int> getRow(int rowIndex) {
82+
// recursive
83+
void buildRow(List<int> pascal, int rowIndex, int temp, int up, int down) {
84+
if (down > rowIndex) return;
85+
temp = temp * up ~/ down;
86+
pascal.add(temp);
87+
buildRow(pascal, rowIndex, temp, --up, ++down);
88+
}
89+
90+
List<int> pascal = [];
91+
pascal.add(1);
92+
buildRow(pascal, rowIndex, 1, rowIndex, 1);
93+
return pascal;
94+
}
95+
}
96+
```
97+
98+
## Solution - 5
99+
100+
```dart
101+
class Solution {
102+
// Runtime: 458 ms, faster than 65.38% of Dart online submissions for Pascal's Triangle II.
103+
// Memory Usage: 138.1 MB, less than 100.00% of Dart online submissions for Pascal's Triangle II.
104+
List<int> getRow(int rowIndex) {
105+
List<int> row = [];
106+
row.insert(0, 1);
107+
for (int i = 1; i <= rowIndex; i++) {
108+
row.insert(i, row.elementAt(i - 1) * (rowIndex - i + 1) ~/ i);
109+
}
110+
return row;
111+
}
112+
}
113+
```
114+
115+
### [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

README.md

+41-40
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,47 @@ This repo contain leetcode solution using DART and GO programming language. Most
44

55
## Solutions - DART
66

7-
* [Add Binary](AddBinary/add_binary.dart)
8-
* [Bag of Tokens](BagOfTokens/bag_of_tokens.dart)
9-
* [Balanced Binary Tree](BalancedBinaryTree\balanced_binary_tree.dart)
10-
* [Best Time to Buy and Sell Stock IV](BestTimeToBuyAndSellStock_IV/best_time_to_buy_and_sell_stock_IV.dart)
11-
* [Binary Tree In Order Trasversal](BinaryTreeInOrderTrasversel/binary_tree_inorder_trasversal.dart)
12-
* [Climbing Stairs](ClimbingStairs/climbing_stairs.dart)
13-
* [Convert Sorted Array To Binary Search Tree](ConvertSortedArrayToBinarySearchTree/convert_sorted_array_to_binary_search_tree.dart)
14-
* [Find Duplicate File in System](FindDuplicateFileInSystem/find_duplicate_file_in_system.dart)
15-
* [Find Original Array From Double Array](FindOriginalArrayFromDoubledArray/find_original_array_from_doubled_array.dart)
16-
* [Implement str Str](ImplementstrStr/implement_strStr.dart)
17-
* [Length of Last word](LengthOfLastWord/length_of_last_word.dart)
18-
* [Longest Common Prefix](LongestCommonPrefix/longest_common_prefix.dart)
19-
* [Maximum Depth of Binary tree](MaximumDepthOfBinaryTree/maximum_depth_of_binary_tree.dart)
20-
* [Maximum Length of Repeated SubArray](MaximumLengthofRepeatedSubarray/maximum_length_of_repeated_subarray.dart)
21-
* [Maximum Performance of a Team](MaximumPerformanceofaTeam/maximum_performance_of_a_team.dart)
22-
* [Merge Sorted Array](MergeSortedArray/merge_sorted_array.dart)
23-
* [Merge Two Sorted Lists](MergeTwoSortedLists/merge_two_sorted_lists.dart)
24-
* [Minimum Depth of Binary Tree](MinimumDepthofBinaryTree/minimum_depth_of_binary_tree.dart)
25-
* [Palindrome Number](PalindromeNumber/palindrome_number.dart)
26-
* [Pascal's Triangle](Pascal'sTriangle/pascals_triangle.dart)
27-
* [Path Sum](PathSum/path_sum.dart)
28-
* [Plus One](PlusOne/plus_one.dart)
29-
* [Pseudo-Palindrome Path In A Binary Tree](Pseudo-PalindromicPathsInABinaryTree/pseudo_palindromic_paths_in_a_binary_tree.dart)
30-
* [Remove Duplicates From Sorted Array](RemoveDuplicatesfromSortedArray/remove_duplicates_from_sorted_array.dart)
31-
* [Remove Duplicates from Sorted List](RemoveDuplicatesfromSortedList/remove_duplicates_from_sorted_list.dart)
32-
* [Remove Element](RemoveElement/remove_element.dart)
33-
* [Reverse Words In A String III](ReverseWordsInAString_III/reverse_words_in_a_string_III.dart)
34-
* [Roman To Integer](RomanToInteger/roman_to_integer.dart)
35-
* [Same Tree](SameTree/same_tree.dart)
36-
* [Search Insert Position](SearchInsertPosition/search_insert_position.dart)
37-
* [SqrtX](SqrtX/sqrt_x.dart)
38-
* [Sum of Even Number After Queries](SumofEvenNumbersAfterQueries/sum_of_even_numbers_after_queries.dart)
39-
* [Symmetric Tree](SymmetricTree/symmetric_tree.dart)
40-
* [The Number Of week Character In The Game](TheNumberOfWeekCharactersInTheGame/the_number_of_week_characters_in_the_game.dart)
41-
* [Trapping Rain Water](TrappingRainWater/trapping_rain_water.dart)
42-
* [Two Sum](TwoSum/twosum.dart)
43-
* [UFT-8 Validation](UTF-8Validation/uft_8_validation.dart)
44-
* [Valid Parenthesis](ValidParentheses/valid_parentheses.dart)
45-
* [Concatenation of Consecutive Binary Numbers](ConcatenationofConsecutiveBinaryNumbers/concatenation_of_consecutive_binary_numbers.dart)
46-
* [Path Sum II](PathSumII/path_sum_II.dart)
7+
- [Add Binary](AddBinary/add_binary.dart)
8+
- [Bag of Tokens](BagOfTokens/bag_of_tokens.dart)
9+
- [Balanced Binary Tree](BalancedBinaryTree\balanced_binary_tree.dart)
10+
- [Best Time to Buy and Sell Stock IV](BestTimeToBuyAndSellStock_IV/best_time_to_buy_and_sell_stock_IV.dart)
11+
- [Binary Tree In Order Trasversal](BinaryTreeInOrderTrasversel/binary_tree_inorder_trasversal.dart)
12+
- [Climbing Stairs](ClimbingStairs/climbing_stairs.dart)
13+
- [Convert Sorted Array To Binary Search Tree](ConvertSortedArrayToBinarySearchTree/convert_sorted_array_to_binary_search_tree.dart)
14+
- [Find Duplicate File in System](FindDuplicateFileInSystem/find_duplicate_file_in_system.dart)
15+
- [Find Original Array From Double Array](FindOriginalArrayFromDoubledArray/find_original_array_from_doubled_array.dart)
16+
- [Implement str Str](ImplementstrStr/implement_strStr.dart)
17+
- [Length of Last word](LengthOfLastWord/length_of_last_word.dart)
18+
- [Longest Common Prefix](LongestCommonPrefix/longest_common_prefix.dart)
19+
- [Maximum Depth of Binary tree](MaximumDepthOfBinaryTree/maximum_depth_of_binary_tree.dart)
20+
- [Maximum Length of Repeated SubArray](MaximumLengthofRepeatedSubarray/maximum_length_of_repeated_subarray.dart)
21+
- [Maximum Performance of a Team](MaximumPerformanceofaTeam/maximum_performance_of_a_team.dart)
22+
- [Merge Sorted Array](MergeSortedArray/merge_sorted_array.dart)
23+
- [Merge Two Sorted Lists](MergeTwoSortedLists/merge_two_sorted_lists.dart)
24+
- [Minimum Depth of Binary Tree](MinimumDepthofBinaryTree/minimum_depth_of_binary_tree.dart)
25+
- [Palindrome Number](PalindromeNumber/palindrome_number.dart)
26+
- [Pascal's Triangle](Pascal'sTriangle/pascals_triangle.dart)
27+
- [Path Sum](PathSum/path_sum.dart)
28+
- [Plus One](PlusOne/plus_one.dart)
29+
- [Pseudo-Palindrome Path In A Binary Tree](Pseudo-PalindromicPathsInABinaryTree/pseudo_palindromic_paths_in_a_binary_tree.dart)
30+
- [Remove Duplicates From Sorted Array](RemoveDuplicatesfromSortedArray/remove_duplicates_from_sorted_array.dart)
31+
- [Remove Duplicates from Sorted List](RemoveDuplicatesfromSortedList/remove_duplicates_from_sorted_list.dart)
32+
- [Remove Element](RemoveElement/remove_element.dart)
33+
- [Reverse Words In A String III](ReverseWordsInAString_III/reverse_words_in_a_string_III.dart)
34+
- [Roman To Integer](RomanToInteger/roman_to_integer.dart)
35+
- [Same Tree](SameTree/same_tree.dart)
36+
- [Search Insert Position](SearchInsertPosition/search_insert_position.dart)
37+
- [SqrtX](SqrtX/sqrt_x.dart)
38+
- [Sum of Even Number After Queries](SumofEvenNumbersAfterQueries/sum_of_even_numbers_after_queries.dart)
39+
- [Symmetric Tree](SymmetricTree/symmetric_tree.dart)
40+
- [The Number Of week Character In The Game](TheNumberOfWeekCharactersInTheGame/the_number_of_week_characters_in_the_game.dart)
41+
- [Trapping Rain Water](TrappingRainWater/trapping_rain_water.dart)
42+
- [Two Sum](TwoSum/twosum.dart)
43+
- [UFT-8 Validation](UTF-8Validation/uft_8_validation.dart)
44+
- [Valid Parenthesis](ValidParentheses/valid_parentheses.dart)
45+
- [Concatenation of Consecutive Binary Numbers](ConcatenationofConsecutiveBinaryNumbers/concatenation_of_consecutive_binary_numbers.dart)
46+
- [Path Sum II](PathSumII/path_sum_II.dart)
47+
- [Pascal's Triangle II](Pascal'sTriangle-II/pascals_riangle_II.dart)
4748

4849
## Reach me via
4950

0 commit comments

Comments
 (0)