From 8b1ae512de9276e4e0017c70206bce1d7acee7e6 Mon Sep 17 00:00:00 2001 From: Di WANG Date: Tue, 24 Oct 2023 00:06:02 -0700 Subject: [PATCH] 198 house robber --- cpp/198-House-Robber.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cpp/198-House-Robber.cpp diff --git a/cpp/198-House-Robber.cpp b/cpp/198-House-Robber.cpp new file mode 100644 index 0000000..33ab49d --- /dev/null +++ b/cpp/198-House-Robber.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int rob(vector& nums) { + int rob(vector& nums) { +if (nums.empty()) return 0; +int n = nums.size(); +if (n == 1) return nums[0]; +int pre2 = 0, pre1 = 0, cur; +for (int i = 0; i < n; ++i) { +cur = max(pre2 + nums[i], pre1); +pre2 = pre1; +pre1 = cur; +} +return cur; +} + } +}; \ No newline at end of file