From 1b5b09b6023b98e2a00d3506a8c5053b73c6a149 Mon Sep 17 00:00:00 2001 From: Siva Prakash Date: Sat, 14 Sep 2024 10:56:21 +0530 Subject: [PATCH 1/2] Removed cpp files --- .gitignore | 1 - .../Climbing Stairs/ClimbingStairs.cpp | 30 --------- .../Coin Change/CoinChange.cpp | 37 ----------- .../Min Cost Climbing Stairs/MinClimb.cpp | 30 --------- .../Duplicate Integer/Duplicate.cpp | 35 ---------- .../EncodeDecode.cpp | 50 -------------- .../Group Anagrams/GroupAnagrams.cpp | 48 -------------- Array & Hashing/Is Anagram/Anagram.cpp | 34 ---------- .../ProductExceptSelf.cpp | 41 ------------ .../Top K Elements in List/KElements.cpp | 66 ------------------- Array & Hashing/Two Sum/TwoSum.cpp | 41 ------------ README.md | 9 ++- format.sh | 24 ------- 13 files changed, 4 insertions(+), 442 deletions(-) delete mode 100644 1-D Dynamic Programming/Climbing Stairs/ClimbingStairs.cpp delete mode 100644 1-D Dynamic Programming/Coin Change/CoinChange.cpp delete mode 100644 1-D Dynamic Programming/Min Cost Climbing Stairs/MinClimb.cpp delete mode 100644 Array & Hashing/Duplicate Integer/Duplicate.cpp delete mode 100644 Array & Hashing/Encode and Decode Strings/EncodeDecode.cpp delete mode 100644 Array & Hashing/Group Anagrams/GroupAnagrams.cpp delete mode 100644 Array & Hashing/Is Anagram/Anagram.cpp delete mode 100644 Array & Hashing/Product of Array Except Self/ProductExceptSelf.cpp delete mode 100644 Array & Hashing/Top K Elements in List/KElements.cpp delete mode 100644 Array & Hashing/Two Sum/TwoSum.cpp diff --git a/.gitignore b/.gitignore index de30d92..dced74f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .vscode *.class -*.out *.jar \ No newline at end of file diff --git a/1-D Dynamic Programming/Climbing Stairs/ClimbingStairs.cpp b/1-D Dynamic Programming/Climbing Stairs/ClimbingStairs.cpp deleted file mode 100644 index 6219e2c..0000000 --- a/1-D Dynamic Programming/Climbing Stairs/ClimbingStairs.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -using namespace std; -class Solution { - public: - int climbStairs(int n) { - if (n < 0) return 0; - if (n == 0) return 1; - if (n == 1) return 1; - if (n == 2) return 2; - int n1 = 1, n2 = 2, current; - for (int i = 2; i < n; i++) { - current = n1 + n2; - n1 = n2; - n2 = current; - } - return current; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - cout << (Solution().climbStairs(n)) << endl; - } - return 0; -} \ No newline at end of file diff --git a/1-D Dynamic Programming/Coin Change/CoinChange.cpp b/1-D Dynamic Programming/Coin Change/CoinChange.cpp deleted file mode 100644 index 2d18f03..0000000 --- a/1-D Dynamic Programming/Coin Change/CoinChange.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -using namespace std; - -class Solution { - public: - int coinChange(vector& coins, int amount) { - vector minCoins(amount + 1, INT_MAX - 1); - minCoins[0] = 0; - for (int c : coins) { - for (int i = 1; i <= amount; i++) { - if (c <= i) { - minCoins[i] = min(minCoins[i], minCoins[i - c] + 1); - } - } - } - return minCoins[amount] == INT_MAX - 1 ? -1 : minCoins[amount]; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n, amount; - cin >> n; - vector coins(n); - for (int i = 0; i < n; i++) { - cin >> coins[i]; - } - cin >> amount; - cout << (Solution().coinChange(coins, amount)) << endl; - } - return 0; -} \ No newline at end of file diff --git a/1-D Dynamic Programming/Min Cost Climbing Stairs/MinClimb.cpp b/1-D Dynamic Programming/Min Cost Climbing Stairs/MinClimb.cpp deleted file mode 100644 index b0f3924..0000000 --- a/1-D Dynamic Programming/Min Cost Climbing Stairs/MinClimb.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -using namespace std; - -class Solution { - public: - int minCostClimbingStairs(vector& cost) { - int n = cost.size(); - for (int i = 2; i < n; i++) { - cost[i] += min(cost[i - 1], cost[i - 2]); - } - return min(cost[n - 1], cost[n - 2]); - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n, amount; - cin >> n; - vector cost(n); - for (int i = 0; i < n; i++) { - cin >> cost[i]; - } - cout << (Solution().minCostClimbingStairs(cost)) << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Duplicate Integer/Duplicate.cpp b/Array & Hashing/Duplicate Integer/Duplicate.cpp deleted file mode 100644 index 8aacb8c..0000000 --- a/Array & Hashing/Duplicate Integer/Duplicate.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -using namespace std; - -class Solution { - public: - bool hasDuplicate(vector &nums) { - unordered_map mp; - for (int x : nums) { - if (mp[x] == 1) { - return true; - } else { - mp[x]++; - } - } - return false; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - vector nums(n); - for (int i = 0; i < n; i++) { - cin >> nums[i]; - } - cout << (Solution().hasDuplicate(nums) ? "true" : "false") << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Encode and Decode Strings/EncodeDecode.cpp b/Array & Hashing/Encode and Decode Strings/EncodeDecode.cpp deleted file mode 100644 index 1fae9fb..0000000 --- a/Array & Hashing/Encode and Decode Strings/EncodeDecode.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include -using namespace std; - -class Solution { - public: - string encode(const vector &strs) { - string encodedString; - for (const string &str : strs) { - encodedString += to_string(str.length()) + "#" + str; - } - return encodedString; - } - - vector decode(const string str) { - vector list; - int i = 0; - while (i < str.length()) { - int j = i; - while (str[j] != '#') j++; - int length = stoi(str.substr(i, j - i)); - i = j + 1 + length; - list.push_back(str.substr(j + 1, length)); - } - return list; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - cin.ignore(); - vector strs(n); - for (int i = 0; i < n; ++i) { - getline(cin, strs[i]); - } - string encodedString = obj.encode(strs); - vector result = obj.decode(encodedString); - for (const string &str : result) { - cout << str << " "; - } - cout << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Group Anagrams/GroupAnagrams.cpp b/Array & Hashing/Group Anagrams/GroupAnagrams.cpp deleted file mode 100644 index b605ff8..0000000 --- a/Array & Hashing/Group Anagrams/GroupAnagrams.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include -#include -using namespace std; - -class Solution { - public: - vector> groupAnagrams(vector& strs) { - unordered_map> ans; - for (string& s : strs) { - string key = s; - sort(key.begin(), key.end()); - ans[key].push_back(s); - } - - vector> result; - for (auto& entry : ans) { - result.push_back(entry.second); - } - - return result; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - vector strs(n); - for (int i = 0; i < n; i++) { - cin >> strs[i]; - } - vector> result = Solution().groupAnagrams(strs); - for (vector x : result) { - for (string y : x) { - cout << y << " "; - } - cout << endl; - } - cout << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Is Anagram/Anagram.cpp b/Array & Hashing/Is Anagram/Anagram.cpp deleted file mode 100644 index 776ce9a..0000000 --- a/Array & Hashing/Is Anagram/Anagram.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -using namespace std; - -class Solution { - public: - bool isAnagram(const string &s, const string &t) { - if (s.length() != t.length()) return false; - unordered_map countMap; - for (char c : s) { - countMap[c]++; - } - for (char c : t) { - if (countMap.find(c) == countMap.end() || countMap[c] == 0) return false; - countMap[c]--; - } - return true; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - cin.ignore(); - while (tc-- > 0) { - string s, t; - getline(cin, s); - getline(cin, t); - cout << (obj.isAnagram(s, t) ? "true" : "false") << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Product of Array Except Self/ProductExceptSelf.cpp b/Array & Hashing/Product of Array Except Self/ProductExceptSelf.cpp deleted file mode 100644 index 0a0b7a7..0000000 --- a/Array & Hashing/Product of Array Except Self/ProductExceptSelf.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -using namespace std; - -class Solution { - public: - vector productExceptSelf(vector& nums) { - int n = nums.size(); - vector leftProduct(n, 1); - vector rightProduct(n, 1); - vector result(n); - - for (int i = 1; i < n; i++) - leftProduct[i] = leftProduct[i - 1] * nums[i - 1]; - - for (int i = n - 2; i >= 0; i--) - rightProduct[i] = rightProduct[i + 1] * nums[i + 1]; - - for (int i = 0; i < n; i++) result[i] = leftProduct[i] * rightProduct[i]; - - return result; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - vector nums(n); - for (int i = 0; i < n; i++) { - cin >> nums[i]; - } - vector result = Solution().productExceptSelf(nums); - for (int x : result) cout << x << " "; - cout << endl; - } - return 0; -} \ No newline at end of file diff --git a/Array & Hashing/Top K Elements in List/KElements.cpp b/Array & Hashing/Top K Elements in List/KElements.cpp deleted file mode 100644 index 205dbd8..0000000 --- a/Array & Hashing/Top K Elements in List/KElements.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include - -using namespace std; - -class Solution { - public: - vector topKFrequent(const vector &nums, int k) { - unordered_map frequencyMap; - for (int num : nums) { - frequencyMap[num]++; - } - - auto cmp = [](const pair &a, const pair &b) { - return a.second > b.second; - }; - priority_queue, vector>, decltype(cmp)> - minHeap(cmp); - - for (const auto &entry : frequencyMap) { - minHeap.push(entry); - if (minHeap.size() > k) { - minHeap.pop(); - } - } - - vector result(k); - int index = 0; - while (!minHeap.empty()) { - result[index++] = minHeap.top().first; - minHeap.pop(); - } - - return result; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - - while (tc-- > 0) { - int n; - cin >> n; - vector nums(n); - - for (int i = 0; i < n; ++i) { - cin >> nums[i]; - } - - int k; - cin >> k; - - vector result = obj.topKFrequent(nums, k); - - for (int x : result) { - cout << x << " "; - } - cout << endl; - } - - return 0; -} diff --git a/Array & Hashing/Two Sum/TwoSum.cpp b/Array & Hashing/Two Sum/TwoSum.cpp deleted file mode 100644 index e7b95cb..0000000 --- a/Array & Hashing/Two Sum/TwoSum.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -using namespace std; - -class Solution { - public: - vector twoSum(const vector &nums, int target) { - unordered_map map; - for (int i = 0; i < nums.size(); ++i) { - int complement = target - nums[i]; - if (map.find(complement) != map.end()) { - return {map[complement], i}; - } - map[nums[i]] = i; - } - return {}; - } -}; - -int main() { - Solution obj; - int tc; - cin >> tc; - while (tc-- > 0) { - int n; - cin >> n; - vector nums(n); - for (int i = 0; i < n; ++i) { - cin >> nums[i]; - } - int target; - cin >> target; - vector result = obj.twoSum(nums, target); - for (int x : result) { - cout << x << " "; - } - cout << endl; - } - return 0; -} diff --git a/README.md b/README.md index 5777f1a..ebff683 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # Neetcode 150 -![Java](https://custom-icon-badges.herokuapp.com/badge/Java-E34F26?logo=java&logoColor=white) -![C++](https://custom-icon-badges.herokuapp.com/badge/C++-00599C?logo=cpp2&logoColor=white) -![Ubuntu](https://img.shields.io/badge/Ubuntu-E95420.svg?logo=Ubuntu&logoColor=white) -![Vim](https://img.shields.io/badge/Neovim-57A143?logo=Neovim&logoColor=white) +![Java](https://img.shields.io/badge/Java-E34F26?logo=openjdk&logoColor=white) +![Neovim](https://img.shields.io/badge/Neovim-57A143?logo=Neovim&logoColor=white) +![Ubuntu](https://img.shields.io/badge/Ubuntu-E95420?logo=Ubuntu&logoColor=white) -This repository contains Neetcode 150 problems source code in C++ and Java programming languages. \ No newline at end of file +This repository contains Neetcode 150 problems source code in Java programming language. \ No newline at end of file diff --git a/format.sh b/format.sh index 22ccbd6..1b280df 100755 --- a/format.sh +++ b/format.sh @@ -1,18 +1,5 @@ #!/bin/bash -# Check if clang-format is installed -if command -v clang-format >/dev/null 2>&1; then - echo "clang-format is already installed." -else - echo "Installing clang-format..." - sudo apt-get update - sudo apt-get install -y clang-format - if [ $? -ne 0 ]; then - echo "Error: Failed to install clang-format." - exit 1 - fi -fi - # Check if google-java-format JAR is already downloaded if [ -f google-java-format.jar ]; then echo -e "\ngoogle-java-format JAR already downloaded." @@ -25,17 +12,6 @@ else fi fi -# Format C++ files -echo -e "\nFormatting C++ files..." -find . -type f -name "*.cpp" | while read -r file; do - echo "Formatting $file..." - clang-format -style=google -i "$file" - if [ $? -ne 0 ]; then - echo "Error: Failed to format $file." - exit 1 - fi -done - # Format Java files echo -e "\nFormatting Java files..." find . -type f -name "*.java" | while read -r file; do From 6e389baa81bbf4e55402ad5fc26a1df2b60e6574 Mon Sep 17 00:00:00 2001 From: Siva Prakash Date: Sat, 14 Sep 2024 10:59:30 +0530 Subject: [PATCH 2/2] Added Binary Search folder --- Binary Search/Binary Search/Search.java | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Binary Search/Binary Search/Search.java diff --git a/Binary Search/Binary Search/Search.java b/Binary Search/Binary Search/Search.java new file mode 100644 index 0000000..2136080 --- /dev/null +++ b/Binary Search/Binary Search/Search.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +class Solution { + public int search(int[] nums, int target) { + int left = 0, right = nums.length - 1, mid; + while (left <= right) { + mid = (left + right) / 2; + if (nums[mid] == target) return mid; + else if (nums[mid] >= target) right = mid - 1; + else left = mid + 1; + } + return -1; + } +} + +public class Search { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Solution obj = new Solution(); + int tc = sc.nextInt(); + while (tc-- > 0) { + int n = sc.nextInt(); + int nums[] = new int[n]; + for (int i = 0; i < n; i++) { + nums[i] = sc.nextInt(); + } + int target = sc.nextInt(); + System.out.println(obj.search(nums, target)); + } + sc.close(); + } +}